472,993 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,993 developers and data experts.

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

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

Similar topics

5
by: Derek Fountain | last post by:
I've been wading through the O'Reilly XSLT book. All seemed OK and sensible until I got to the "programming" bit - variables, loops, conditions and so on. Do people actually use this stuff for...
5
by: Brett conklin | last post by:
I would like to not use the google groups as a crutch in my XSLT journey. What is the best xslt reference out there? Is it a book? Is it a web site? I am finding that I can come up with nearly...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
4
by: Ringo Langly | last post by:
Hi all, I'm a seasoned web programmer, but I've never touched XSLT. It's always been one of those acronyms I've never needed to educate myself on. Now... we're working with a web content...
5
by: Per Johansson | last post by:
Is it possible to use XSLT to automatically create href links while it formats an XML document? That is, if it finds "http://me.us/" in a text, it adds <a href="http://me.us/">http://me.us/</a> --...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
1
by: Paulson | last post by:
Hi guys I got a problem.I am using xml/xsl in asp.net. I know how to add update delete nodes in xml Files. But I dont know how to create a good stylesheet for it.I...
14
by: Lee | last post by:
I have a xml file, here is sample part: <?xml version="1.0" encoding="UTF-8"?> <ProducsList> <Product id="1"> <SpecList> <Spec> <SpecLabel>Height</SpecLabel> <SpecValue>10</SpecValue>...
2
by: astroboiii | last post by:
New to the whole xml thing and finding w3schools to be an excellent resource. Now down to my question: I have several xml files I need to parse through and grab relevant information from and...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.