Solved

"drill down" in an XML sheet using XSL

Posted on 2004-03-31
3
629 Views
Last Modified: 2013-11-19
Hello experts,

This might be a typical question, but I need an answer urgently and 2 hours of Googling didn't help

I have an XML document as follows: (simplification may have led to typos)

<?xml version="1.0" ?>
<cars>
  <manufacturer name="Aston Martin">
   <model name="DB7 GT">
    <weight>1770kg</weight>
   </model>
   <model name="DB7 Vantage">
    <weight>1875kg</weight>
   </model>
  </manufacturer>
  <manufacturer name="Jaguar">
   <model name="S-Type">
    <weight>1270kg</weight>
   </model>
   <model name="XKR">
    <weight>1895kg</weight>
   </model>
  </manufacturer>
</cars>

I call the following XSL from an as page "viewman.asp"; this lists the manufacturers:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
      <xsl:template match="/">
            <h1>Manufacturers:</h1>
            <xsl:for-each select="cars/manufacturer">      
                  <xsl:element name="a">
                        <xsl:attribute name="href">
                        viewcars.asp?man=<xsl:value-of select="@name"/>
                        </xsl:attribute>
                        <xsl:value-of select="@name"/>
                  </xsl:element>
                  <br />
            </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>

This displays each manufacturer as a link to "viewcars.asp" with "man" as a GET variable. Now, I want the second page to display all cars from only ONE manufacturer. Ideally this should be possible using the same xsl sheet, by passing the "man" variable to it. However, I get the feeling this isn't possible, and that I should use "SelectSingleNode" in ASP instead.

After displaying the cars on "viewcars.asp", I would like to proceed to "viewcar.asp" that displays the car's weight (and other specs).

Any help will be greatly appreciated.

Many thanks,
Victor
0
Comment
Question by:vpikula
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
conorj earned 500 total points
ID: 10723642
First change your namespace to use the XSLT 1.0 namespace:

http://www.w3.org/1999/XSL/Transform

then declare a top level parameter with the same name as your GET variable and give it a default value. The first time you call the stylesheet you don't pass any parameter and so the default value is used. i.e. all the manufacturers are listed. If any value other than the default is passed to the stylesheet then all models for thst manufacturer will be listed.

<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:param name="man" select="'all'" />
     <xsl:template match="/">
           <xsl:choose>
                 <xsl:when test="$man = 'all'">
                <h1>Cars:</h1>
                    <ul>
                      <xsl:for-each select="cars/manufacturer[@name = $man]/model">
                                 <li><xsl:value-of select="@name"/></li>
                      </xsl:for-each>
                    </ul>
                 </xsl:when>
                 <xsl:otherwise>
                <h1>Manufacturers:</h1>
                <xsl:for-each select="cars/manufacturer">    
                     <a href="viewcars.asp?man={@name}">
                          <xsl:value-of select="@name"/>
                     </a>
                     <br />
                </xsl:for-each>
                 </xsl:otherwise>
           </xsl:choose>
     </xsl:template>
</xsl:stylesheet>

Hope this helps.

rgds,
Conor.
0
 

Author Comment

by:vpikula
ID: 10723802
Brilliant work! To be honest, for the last hour I've been fiddling around with the exact same principle and came to almost the exact same conclusion, but didn't know how to work with xsl:if properly since it didn't have an 'else'.  Your xsl:choose works perfectly.

Even our parameter name 'man' is the same! ;)

Great work, thanks for the 'choose' pointer. Are they nestable?
Victor
0
 
LVL 5

Expert Comment

by:conorj
ID: 10724275
Yes, you can nest one xsl:choose inside another. Note that the nested xsl:choose must be inside the xsl:when or the xsl:otherwise statement.
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

Title # Comments Views Activity
Convert XML to excel12book 5 29
XML XSL Message 3 22
Shredding xml into an oracle 11g Database 2 46
Fulfillment API php code sample 1 41
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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. 
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

803 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