[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: newbie - parameters with/without values??

Subject: RE: newbie - parameters with/without values??
From: cknell@xxxxxxxxxx
Date: Mon, 04 Oct 2004 11:29:42 -0400
xpath select expression
You can't use variables to match element or attributes in an XPath expression. You can use them to match element names, but that gets tricky to explain. Although tedious to build, an xsl:choose element will do what you have asked. This works because a parameter with no value evaluates to "false" in a logical test, thus:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

  <xsl:strip-space elements="*" />
  <xsl:param name="gender" />
  <xsl:param name="city" />

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="my_document">
    <xsl:choose>
      <xsl:when test="$gender and $city">
        <xsl:apply-templates select="person[gender=$gender and city=$city]" />
      </xsl:when>
      <xsl:when test="$gender and not($city)">
        <xsl:apply-templates select="person[gender=$gender]" />
      </xsl:when>
      <xsl:when test="not($gender) and $city">
        <xsl:apply-templates select="person[city=$city]" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="person" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="person">
    <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Hardy Merrill <HMerrill@xxxxxxxxxxxxxxxx>
Sent:     Mon, 04 Oct 2004 11:10:03 -0400
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:   newbie - parameters with/without values??

XSLT newbie alert.

I have an xml document with lots of nodes.  I have some fields
(each with its own value) I'd like to use as "search" 
criteria - normally I would pass the field values into the XSL
style sheet as parameters, and just reference the parameters.
But the kicker here is that the parameters may *** or may not ***
have values.  If a parameter has a value, I want to include it in
an XPath select.  If a parameter does NOT have a value, then I
don't want to include that parameter in the XPath select.

I've been trying to use variables to "build" an XPath expression
from the supplied parameters using "if test"s, but I'm not having
much luck since variables can only be given a value once, and their
scope is limited to the block in which they are declared.

Given this XML document: 

<my_document>
   <person>
      <name>Joe</name>
      <gender>Male</gender>
      <city>Boston</city>
  </person>
  <person>
     <name>Janice</name>
     <gender>Female</gender>
     <city>Denver</city>
  </person>
</my_document>

can I build an XSL style sheet to take in parameters "gender" and
"city", which may or may not have values, and *build* an XPath
select expression that will find the right nodes?

I'm sure this is a trivial question, but being a newbie I'm having
trouble with it.

TIA.

Hardy Merrill

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.