Solved

"drill down" in an XML sheet using XSL

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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

630 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