Part 2 of XPath Location Steps: Node Tests

Part 2 of XPath Location Steps: Node Tests

When constructing patterns, you can use names of nodes as node tests, or the wild card * to select any element node. For example, the expression child::*/child:: NAME selects all <NAME> elements that are grandchildren of the context node. In addition to node names and the wild card character, you can also use these node tests in XPath, just as you can in match patterns:

  • The comment() node test selects comment nodes.

  • The node() node test selects any type of node.

  • The processing-instruction() node test selects a processing instruction node. You specify the name of the processing instruction to select in the parentheses.

  • The text() node test selects a text node.

For example, the following stylesheet finds all comments in a document, using the comment() node test, and creates a new comment, <!--Warning:comment found!--> , for each one:

Listing 7.2 Matching Comments
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:output method="xml"/>    <xsl:template match="/">    <xsl:for-each select="descendant::comment()">        <xsl:comment>Warning: comment found!</xsl:comment>    </xsl:for-each>    </xsl:template>  </xsl:stylesheet> 

Heres the result document when you use this stylesheet on planets.xml:

 <?xml version="1.0" encoding="UTF-8"?>  <!--Warning: comment found!-->  <!--Warning: comment found!-->  <!--Warning: comment found!--> 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net