Inside Xslt

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:

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!-->

Категории