Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"drill down" in an XML sheet using XSL

Posted on 2004-03-31
3
Medium Priority
?
658 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

927 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