[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding child rows to a table using XSL

Posted on 2012-04-11
4
Medium Priority
?
325 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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…
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 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