Solved

Adding child rows to a table using XSL

Posted on 2012-04-11
4
290 Views
Last Modified: 2012-06-27
I am currently transforming XML into an HTML table using XSL.

The XML file is a collection of 'Roads' - which I am rendering out to a table.

XML: http://sandbox5.project4.net/child/roads_locs.xml
XSL: http://sandbox5.project4.net/child/roads.xsl
HTML Output: http://sandbox5.project4.net/child/default.asp

You may note in the XSL that some roads may have one or more child 'locations'.

Question: How can I modify my XSL to show 'location' information below each road row?
0
Comment
Question by:sonic1234
  • 3
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 37836398
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" method="html"/> 
<xsl:template match="roads">
   
            <table class="table-j" summary="Road Closures">
               <thead>
                  <tr>
                     <th scope="col">Road Name</th>
                     <th scope="col">Council</th>
                     <th scope="col">Status</th>
                     <th scope="col">Last Updated</th>
                     <th scope="col">Last Observed</th>
                  </tr>
               </thead>
                  <tfoot>
                  <tr>
                     <td colspan="5"><em>As road conditions are constantly changing, the information shown above may not represent the actual road condition. Please drive with caution at all times.</em></td>
                  </tr>
               </tfoot>
               <tbody>
                  <xsl:apply-templates select="road"/>
               </tbody>
            </table>

    </xsl:template>
   
   <xsl:template match="road">
      <tr>
         <td>
            <b><xsl:value-of select="name"/></b>
         </td>
         <td>
            <xsl:value-of select="org"/>
         </td>
         <td>
            <xsl:value-of select="status"/>
         </td>
         <td>
            <xsl:value-of select="observed"/>
         </td>
         <td>
            <xsl:value-of select="updated"/>
         </td>
      </tr>
      <xsl:apply-templates select="locations/location"></xsl:apply-templates>
   </xsl:template>
   
   <xsl:template match="location">
      <tr>
         <td>
            <i><xsl:value-of select="name"/></i>
         </td>
         <td>
            <xsl:value-of select="org"/>
         </td>
         <td>
            <xsl:value-of select="status"/>
         </td>
         <td>
            <xsl:value-of select="observed"/>
         </td>
         <td>
            <xsl:value-of select="updated"/>
         </td>
      </tr>
   </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37836401
Note that I slightly restructured your original stylesheet to use apply-templates in favour of for-each.
It greatly improves  the readibility and maintainability of your stylesheet
You pass the child nodes to a different template

Then you simply do the same with the location nodes

I made the road names bold so you see the difference
0
 

Author Closing Comment

by:sonic1234
ID: 37840906
Thank you Gertone, that is very clear and helpful.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37841309
welcome
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now