Solved

Adding child rows to a table using XSL

Posted on 2012-04-11
4
308 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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