What is the difference between normalize-space(.) and normalize-space(text())?

72,252

Well, the real question is: what's the difference between . and text()?

. is the current node. And if you use it where a string is expected (i.e. as the parameter of normalize-space()), the engine automatically converts the node to the string value of the node, which for an element is all the text nodes within the element concatenated. (Because I'm guessing the question is really about elements.)

text() on the other hand only selects text nodes that are the direct children of the current node.

So for example given the XML:

<a>Foo
    <b>Bar</b>
  lish
</a>

and assuming <a> is your current node, normalize-space(.) will return Foo Bar lish, but normalize-space(text()) will fail, because text() returns a nodeset of two text nodes (Foo and lish), which normalize-space() doesn't accept.

To cut a long story short, if you want to normalize all the text within an element, use .. If you want to select a specific text node, use text(), but always remember that despite its name, text() returns a nodeset, which is only converted to a string automatically if it has a single element.

Share:
72,252

Related videos on Youtube

Karim
Author by

Karim

Updated on July 09, 2022

Comments

  • Karim
    Karim almost 2 years

    I was writing an XPath expression, and I had a strange error which I fixed, but what is the difference between the following two XPath expressions?

    "//td[starts-with(normalize-space()),'Posted Date:')]"
    

    and

    "//td[starts-with(normalize-space(text()),'Posted Date:')]"  
    

    Mainly, what will the first XPath expression catch? Because I was getting a lot of strange results. So what does the text() make in the matching? Also, is there is a difference if I said normalize-space() & normalize-space(.)?

    • CJ7
      CJ7 over 7 years
      From my own testing normalize-space() and normalize-space(.) have the same effect.
  • Matthijs Bierman
    Matthijs Bierman almost 13 years
    Actually normalize-space(text()) will return an empty string, because it takes the text in the root. normalize-space(//text()) will return Foo, because it transforms your NodeSet by taking the first node and converting it to a String and running normalize-space on that.
  • biziclop
    biziclop almost 13 years
    @Matthijs Bierman Have you tried it? I have and it works exactly as I said. (In Xpath 2.0, I shall add and assuming that the context node is the <a> element.)
  • Matthijs Bierman
    Matthijs Bierman almost 13 years
    Yes, I have (I wasn't sure). But I tried in XPath 1.0. Standard JAXP, but with Xerces 2.11.0 :).
  • biziclop
    biziclop almost 13 years
    @Matthijs Bierman Weird, I'll experiment a bit with that. I know that my answer definitely was for Xpath 2.0 only.
  • Arup Rakshit
    Arup Rakshit almost 11 years
    How should I write this //text()/normalize-space(.),although my one in wrong!
  • mgibas
    mgibas about 10 years
    try text()[normalize-space()]
  • paul
    paul almost 9 years
    @MatthijsBierman where did you try? which website? I am asking so that we can try it hands on for better understanding.
  • slott
    slott almost 8 years
    I ended up with something like this normalize-space(s:td[2]/s:a[1]/text()[1]) And that worked like a charm