- Attribute Value Template
Official W3C explanation and example
This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself.
The most common usage of this is in creating hyperlinks.
Example:
Source XML
Expand|Select|Wrap|Line Numbers- <item>
- <link>www.google.ca</link>
- <desc>Search Engine</desc>
- </item>
- <item>
- <link>http://www.mytelus.com/ncp_news/</link>
- <desc>news</desc>
- </item>
Expand|Select|Wrap|Line Numbers- <xsl:template match="item">
- <a>
- <xsl:attribute name="href">
- <xsl:value-of select="link"/>
- </xsl:attribute>
- <xsl:value-of select="desc"/>
- </a>
- </xsl:template>
Expand|Select|Wrap|Line Numbers- <xsl:template match="item">
- <a href="{link}"><xsl:value-of select="desc"/></a>
- </xsl:template>
Expand|Select|Wrap|Line Numbers- <a href="www.google.ca">Search Engine</a>
- <a href="http://www.mytelus.com/ncp_news/">news</a>
- Muenchian Grouping
Jeni's XSLT Pages: Grouping Using the Muenchian Method
This is a way of grouping together elements by a particular value. It's especially useful in interpreting the data generated by database dumps, since many fields are repeated. This is only needed for xslt 1.0, as 2.0 has built in grouping.
Example:
Expand|Select|Wrap|Line Numbers- <book>
- <title>How to Win Friends and Influence People</title>
- <author>Dale Carnegie</author>
- </book>
- <book>
- <title>Harry Potter and the Philosopher's Stone</title>
- <author>J.K. Rowling</author>
- </book>
- <book>
- <title>Harry Potter and the Deathly Hallows /title>
- <author>J.K. Rowling</author>
- </book>
Expand|Select|Wrap|Line Numbers- <xsl:key name="bookByAuthor" match="book" use="author">
- <xsl:for-each select="//book[count(.|key('bookByAuthor', author)[1]) = 1]">
- <author>
- <name><xsl:value-of select="author"/></name>
- <xsl:for-each select="key('bookByAuthor', author)">
- <book><xsl:value-of select="title"/></book>
- </xsl:for-each>
- </author>
Expand|Select|Wrap|Line Numbers- <author>
- <name>Dale Carnegie</name>
- <book>How to Win Friends and Influence People</book>
- </author>
- <author>
- <name>J.K. Rowling</name>
- <book>Harry Potter and the Philosopher's Stone</book>
- <book>Harry Potter and the Deathly Hallows</book>
- </author>
- Axes
A lot of the time you need to select particular nodes, but don't have an easy way of selecting them. Solution? Use an axis! The most important ones are as follows:
1. following-sibling:: and preceding-sibling::
2. descendant:: (also written as .//)
3. ancestor::
See the link for a list of all the axes.
XSLT 1.0: axes
Here's a contrived example:
Expand|Select|Wrap|Line Numbers- <stock symbol="SYM">
- <price day="11022008" close="156">
- <price day="12022008" close="135">
- <price day="13022008" close="158">
- <price day="14022008" close="158">
- <price day="15022008" close="146">
- </stock>
Expand|Select|Wrap|Line Numbers- <xsl:template match="stock">
- <table>
- <tr>
- <th>day</th>
- <th>close</th>
- <th>change</th>
- </tr>
- <xsl:apply-templates/>
- </table>
- </xsl:template>
- <xsl:template match="price">
- <tr>
- <td><xsl:value-of select="@day"></td>
- <td><xsl:value-of select="@close"></td>
- <td>
- <xsl:choose>
- <xsl:when test="position() = 1">-</xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="@close - preceding-sibling::price[1]/@close"/>
- </xsl:otherwise>
- </xsl:choose>
- </td>
- </tr>
Result:
Expand|Select|Wrap|Line Numbers- <table>
- <tr><th>day</th><th>close</th><th>change</th></tr>
- <tr><td>11022008</td><td>156</td><td>-</td></tr>
- <tr><td>12022008</td><td>135</td><td>-21</td></tr>
- <tr><td>13022008</td><td>158</td><td>23</td></tr>
- <tr><td>14022008</td><td>158</td><td>0</td></tr>
- <tr><td>15022008</td><td>146</td><td>-12</td></tr>
- </table>
- Namespaces
Many times there are complaints that someone can't select an element because of a namespace prefix at the beginning of an element.
Expand|Select|Wrap|Line Numbers- <atta:root xmlns:atta="www.atta.com">
- <atta:table>
- <atta:row>
- <atta:city>Vancouver</atta:city>
- <atta:hnde>24</atta:hnde>
- </atta:row>
- <atta:row>
- <atta:city>Montreal</atta:city>
- <atta:hnde>36</atta:hnde>
- </atta:row>
- </atta:table>
- </atta>
"/atta:root/atta:table/atta:row[atta:city = 'Vancouver']/atta:hnde"
- Honourable mention: the node-set function.
Since this isn't strictly xslt, but relies on extension functions, I haven't included it. This treats xsl variables as node sets (automatic in XSLT 2.0). eg. either exsl:node-set() xmlns:exslt="http://exslt.org/common" OR
msxsl:node-set() xmlns:msxsl="urn:schemas-microsoft-com:xslt"
EXSLT - exsl:node-set
Support for the msxsl:node-set() Function
General Reference List:
- XSLT Tutorial
- XSLT Reference
Good reference for XSLT 1.0. In particular, axes. - XSLT Reference
XSL elements
XPath, XQuery, and XSLT Function Reference
XPath 2.0 functions.
If you have any other tips which you should be added here, feel free to respond.