normalize-space function inconsistent results

This should cover W3C XML Schema, Relax NG and DTD related problems.
Richard_Wood
Posts: 16
Joined: Tue Jan 12, 2016 9:41 pm

normalize-space function inconsistent results

Post by Richard_Wood »

Can anyone explain why I get different results from normalize-space, depending on how I use it?
In one case it works as expected, in the other case it leaves a space on the end of the value:

Source data contains:

Code: Select all

<dataProvider>              MTY-MITAC                </dataProvider>

<sch:let name="refValue"
value="//Document/dataProvider/normalize-space()"/>
Produces: refValue = MTY-MITAC;

<sch:let name="refValue"
value="//Document/dataProvider[normalize-space()]"/>
Produces: refValue = MTY-MITAC ; (note the extra space at the end)

<sch:let name="refValue"
value="//Document/dataProvider[normalize-space(.)]"/>
Produces: refValue = MTY-MITAC ; (note the extra space at the end)
Radu
Posts: 9046
Joined: Fri Jul 09, 2004 5:18 pm

Re: normalize-space function inconsistent results

Post by Radu »

Hi Richard,

Whenever you are using "[" and "]" in an XPath expression it means that you are imposing a condition (the specification calls it a "predicate").
So this XPath means:

Code: Select all

//Document/dataProvider[normalize-space(.)]
Retrieve all "dataProvider" elements from any "Document" element with the condition that their normalized text content is not empty.

while this XPath means:

Code: Select all

//Document/dataProvider/normalize-space()
Retrieve all the normalized text content of the "dataProvider" elements which are directly inside any "Document" element.

So in the first XPath the result is not normalized at all because the normalization is inside a condition.

More about XPath predicates (conditions):

http://www.w3schools.com/xsl/xpath_syntax.asp

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply