Solved

"drill down" in an XML sheet using XSL

Posted on 2004-03-31
3
609 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select distinct 25 60
Problem to file 3 50
WCAG (Web Content Accessibility Guidelines) levels 3 44
Problem to page 4 29
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

746 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

9 Experts available now in Live!

Get 1:1 Help Now