[2.0] Comments in XPath Expressions
Another addition to the XPath 2.0 syntax is the ability to add comments. Using delightfully
happy syntax, a comment begins with (:
and ends with :)
. We’ll use a stylesheet with a complicated
if
statement:
<?xml version="1.0"?> <!-- comments.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each select="cars/make"> <xsl:text>
 Car: </xsl:text> <xsl:value-of select="."/> <xsl:text> - </xsl:text> <xsl:value-of select="(: Most of our cars are from North America, so we look there first :) if (@geography = 'North America') then 'Domestic car' (: Next, see if the car is from Europe :) else if (@geography = 'Europe') then 'Import from Europe' (: Check for Asia :) else if (@geography = 'Asia') then "It's from Asia" (: If it's anything else, just say 'We don't know' :) else 'We don''t know!'"/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The stylesheet has three if
statements that check the value of the geography
attribute of an element. We’ve used
spacing and comments liberally here to make the code more legible. In
the last comment, you can see that we don’t have to escape quote marks
inside the comment, although we do have to handle them appropriately
outside the comment. For one quote that contains an apostrophe, we wrap
the text in double quotes ("It's from
Asia"
). For the next quote, we use a doubled
apostrophe ('We ...
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.