[http://creativecommons.org/licenses/by/3.0/]
This work is licensed under a CC
Attribution 3.0 Unported License [http://creativecommons.org/licenses/by/3.0/]
Advanced XSLT processing includes better control of the input and output documents, which can be finely controlled in terms of how whitespace is treated. Another interesting feature of XSLT are keys, which allow shorthand notations for frequently used access paths to nodes, and provide XSLT processors with more information for performance optimizations. Instructions for creating all possible kinds of nodes in the output tree make it possible to write code which generates element or attribute names based on runtime evaluations.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>me: </xsl:text> <xsl:value-of select="count(document('document.xsl')/descendant::*)"/> <xsl:text>
http://dret.net/netdret/publications: </xsl:text> <xsl:value-of select="count(document('http://dret.net/netdret/publications')/descendant::*)"/> </xsl:template> </xsl:stylesheet>
<p>do <u>not</u> <em>throw</em> <b>away</b> these whitespace nodes!</p>
<xsl:strip-space elements="*"/> <xsl:preserve-space elements="xsl:text"/> <xsl:template match="/"> <xsl:text>xsl:text is used for outputting text.</xsl:text> <xsl:text> </xsl:text> <xsl:text>it also is the only element where whitespace nodes in the stylesheet are significant</xsl:text> <xsl:text>
</xsl:text> <xsl:value-of select="count(//text())"/> </xsl:template>
…Key)
<people> <entry id="dret" country="de"> <name> <given>Erik</given> <given>Thomas</given> <sur>Wilde</sur> </name> <email>dret@berkeley.edu</email> <affiliation country="us">iSchool/UCB</affiliation> <phone location="office" type="voice">+1-510-6432253</phone> <phone location="office" type="fax">+1-510-6425814</phone>
<xsl:key name="givenNameKey" match="name" use="given"/> <xsl:key name="affiliationKey" match="affiliation" use="."/> <xsl:key name="countryKey" match="entry | affiliation" use="@country"/>
|
|
key('givenNameKey', 'Thomas')
→ 2 nodes (1, 2)key('countryKey', 'us')
→ 3 nodes (1b, 3a, 3b)key('countryKey', 'us')/self::entry
→ 1 node (3a)$a[count(. | $b) = count($b)]: Find all nodes in $a
where the cardinality of $b
does not change when adding this node to it. This means the node must be in $b
, and it is in $a
to start with.
<xsl:template match="*"> <xsl:element name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}"> <xsl:apply-templates select="node() | @*"/> </xsl:element> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:template>