Tuesday, 12 February 2008

XPath expressions special characters in XSL

There is a common misunderstanding about the use of special characters by Xpath expressions in XSL and Xpath expressions in other languages, I'll try to help clarify some of the myths in this blog.

If you were using Java or C#, the actual expression would be either passed as a string parameter to a constructor method or any other method, or even just stored in a string variable to then be used as a parameter to one of those methods. Simple. Although, if you'd consider the following example, you'd have to escape the double quote characters with a back slash character.

String xPathExpr = "/root/my/node[@attribute='\"this & that value in quotes\"']";

In XML you have similar constraints, if you try to put a double quote character inside the value of an attribute, the XML parser thinks the value of the attribute ends on the first occurrence of the double quote character, therefore the document won't be a valid XML document. Unless of coarse you replace the character with the equivalent character reference ".

Because XSL documents are XML documents, some of these rules need to be taken into account. By default most XSLT Procesors allocate entities to at least 3 of those character references:
<!ENTITY amp CDATA "&#38;" -- ampersand, U+0026 ISOnum -->
<!ENTITY lt CDATA "&#60;" -- less-than sign, U+003C ISOnum -->
<!ENTITY gt CDATA "&#62;" -- greater-than sign, U+003E ISOnum -->

Meaning you can use those three by preceding the entity name with an "&" character and terminate the entity refference with an ";" character.
&amp; = &
&lt; = <
&gt; = >

So if you were to use the earlier XPath expression in XSL it'd look like this instead:

<xsl:variable name="myNode" select="/root/my/node[@attribute='&#34;this &amp; that value in quotes&#34;'"/>
<xsl:variable name="myNode" select="/root/my/node[@attribute='&#34;this &#38; that value in quotes&#34;'"/>

The characters > and < are often used in XPath expressions that evaluate numerical values.

<xsl:variable name="isMyOtherAttributeGreater" select="/root/my/node/@attribute &gt; /root/my/other/node/@otherAttribute"/>
This expression returns a Boolean value, of true when the value of @attribute of the first XPath expression is greater than value of the @otherAttribute in the second expression.

You could also combine < and > with the "=" character to construct the greater than or equal and the less than or equal evaluation expressions.
@attribute &gt;= @otherAttribute
@attribute &lt;= @otherAttribute

<xsl:variable name="myNode" select="/root/my/node[@attribute &gt;= ../node[1]/@otherAttribute]"/>

see following links for further info on entities and character encoding in XML:


Anup said...

Good post.

Glad to see you posting again after a while :)

Miguel deMelo said...

Thanks Anup, I'm goint to try to update the blog with a few topics I have recently been queried about.

Anonymous said...

Hope to see same more information in futere.

Anonymous said...

There are many interesting here. Hope to see some more in future

Jayakrishnan said...

how to do you escape '%' symbol in xpath
Please help me....

Miguel de Melo said...

why would you need to escape the % character ?

Kalpana said...

can you please tell me how to use [] (this square brackets) in XPath expression. i tried amny ways but it did not work.