468,272 Members | 2,194 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

XSLT Tips you don't learn at w3schools

jkmyoung
2,057 Expert 2GB
Here's a short list of useful xslt general tricks that aren't taught at w3schools.
  1. 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
    1. <item>
    2.   <link>www.google.ca</link>
    3.   <desc>Search Engine</desc>
    4. </item>
    5. <item>
    6.   <link>http://www.mytelus.com/ncp_news/</link>
    7.   <desc>news</desc>
    8. </item>
    9.  
    Old XSLT:
    Expand|Select|Wrap|Line Numbers
    1. <xsl:template match="item">
    2.   <a>
    3.     <xsl:attribute name="href">
    4.       <xsl:value-of select="link"/>
    5.     </xsl:attribute>
    6.     <xsl:value-of select="desc"/>
    7.   </a>
    8. </xsl:template>
    9.  
    New XSLT:
    Expand|Select|Wrap|Line Numbers
    1. <xsl:template match="item">
    2.   <a href="{link}"><xsl:value-of select="desc"/></a>
    3. </xsl:template>
    4.  
    Result:
    Expand|Select|Wrap|Line Numbers
    1. <a href="www.google.ca">Search Engine</a>
    2. <a href="http://www.mytelus.com/ncp_news/">news</a>
    3.  
    Now isn't that much nicer?

  2. 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
    1. <book>
    2.   <title>How to Win Friends and Influence People</title>
    3.   <author>Dale Carnegie</author>
    4. </book>
    5. <book>
    6.   <title>Harry Potter and the Philosopher's Stone</title>
    7.   <author>J.K. Rowling</author>
    8. </book>
    9. <book>
    10.   <title>Harry Potter and the Deathly Hallows /title>
    11.   <author>J.K. Rowling</author>
    12. </book>
    13.  
    Here we want to group the books by author, so we have xslt code like so:
    Expand|Select|Wrap|Line Numbers
    1. <xsl:key name="bookByAuthor" match="book" use="author">
    2.  
    3. <xsl:for-each select="//book[count(.|key('bookByAuthor', author)[1]) = 1]">
    4.   <author>
    5.     <name><xsl:value-of select="author"/></name>
    6.     <xsl:for-each select="key('bookByAuthor', author)">
    7.       <book><xsl:value-of select="title"/></book>
    8.     </xsl:for-each>
    9.   </author>
    10.  
    Output:
    Expand|Select|Wrap|Line Numbers
    1. <author>
    2.   <name>Dale Carnegie</name>
    3.   <book>How to Win Friends and Influence People</book>
    4. </author>
    5. <author>
    6.   <name>J.K. Rowling</name>
    7.   <book>Harry Potter and the Philosopher's Stone</book>
    8.   <book>Harry Potter and the Deathly Hallows</book>
    9. </author>
    10.  
  3. 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
    1. <stock symbol="SYM">
    2.   <price day="11022008" close="156">
    3.   <price day="12022008" close="135">
    4.   <price day="13022008" close="158">
    5.   <price day="14022008" close="158">
    6.   <price day="15022008" close="146">
    7. </stock>
    8.  
    In order to find the change from the day before we do the following:
    Expand|Select|Wrap|Line Numbers
    1. <xsl:template match="stock">
    2.   <table>
    3.     <tr>
    4.       <th>day</th>
    5.       <th>close</th>
    6.       <th>change</th>
    7.     </tr>
    8.     <xsl:apply-templates/>
    9.   </table>
    10. </xsl:template>
    11. <xsl:template match="price">
    12. <tr>
    13.   <td><xsl:value-of select="@day"></td>
    14.   <td><xsl:value-of select="@close"></td>
    15.   <td>
    16.     <xsl:choose>
    17.       <xsl:when test="position() = 1">-</xsl:when>
    18.       <xsl:otherwise>
    19.         <xsl:value-of select="@close - preceding-sibling::price[1]/@close"/>
    20.       </xsl:otherwise>
    21.     </xsl:choose>
    22.   </td>
    23. </tr>
    24.  
    So if the day we're checking isn't the first day, we subtract the value of the preceding price from this day's to see the change. Note the use of [1] after price. This tells the processor we only want the first price node right before the one we're working on. The nodes in axis have position based on the axis.

    Result:
    Expand|Select|Wrap|Line Numbers
    1. <table>
    2.   <tr><th>day</th><th>close</th><th>change</th></tr>
    3.   <tr><td>11022008</td><td>156</td><td>-</td></tr>
    4.   <tr><td>12022008</td><td>135</td><td>-21</td></tr>
    5.   <tr><td>13022008</td><td>158</td><td>23</td></tr>
    6.   <tr><td>14022008</td><td>158</td><td>0</td></tr>
    7.   <tr><td>15022008</td><td>146</td><td>-12</td></tr>
    8. </table>
    9.  
  4. 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
    1. <atta:root xmlns:atta="www.atta.com">
    2.    <atta:table>
    3.      <atta:row>
    4.         <atta:city>Vancouver</atta:city>
    5.         <atta:hnde>24</atta:hnde>
    6.      </atta:row>
    7.      <atta:row>
    8.         <atta:city>Montreal</atta:city>
    9.         <atta:hnde>36</atta:hnde>
    10.      </atta:row>
    11.    </atta:table>
    12.  </atta>
    13.  
    To access the 24 under Vancouver, the xpath could be something like:
    "/atta:root/atta:table/atta:row[atta:city = 'Vancouver']/atta:hnde"


  5. 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:
  1. XSLT Tutorial
  2. XSLT Reference
    Good reference for XSLT 1.0. In particular, axes.
  3. 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.
Feb 8 '08 #1
2 21669
Don't mislead your readers: descendant:: is not equivalent to //
See NOTE at http://www.w3.org/TR/xpath#path-abbrev
Aug 10 '08 #2
jkmyoung
2,057 Expert 2GB
Thanks, I've changed it to .// to avoid confusion.
Dec 8 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Derek Fountain | last post: by
5 posts views Thread by Brett conklin | last post: by
4 posts views Thread by Ringo Langly | last post: by
5 posts views Thread by Per Johansson | last post: by
3 posts views Thread by Jack Fox | last post: by
2 posts views Thread by astroboiii | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.