Solved

Adding child rows to a table using XSL

Posted on 2012-04-11
4
279 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
Comment Utility
<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
Comment Utility
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
Comment Utility
Thank you Gertone, that is very clear and helpful.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSD Data to SQL Tables 2 21
xml read and write 3 58
Adding Namespace to the second node of the xml 16 29
Create XML 5 30
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…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

12 Experts available now in Live!

Get 1:1 Help Now