Solved

"drill down" in an XML sheet using XSL

Posted on 2004-03-31
3
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
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. 
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

739 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