RE: [xsl] newbie - parameters with/without values??

Subject: RE: [xsl] newbie - parameters with/without values??
From: "Hardy Merrill" <HMerrill@xxxxxxxxxxxxxxxx>
Date: Mon, 04 Oct 2004 13:01:00 -0400
I'm trying to understand...

Given the XML document below with 2 "person" nodes in it, if my XSL
document looks like this:
---------------------------------------------
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="html" />

<xsl:param name="gender"/>
<xsl:param name="city"/>

<xsl:template match="/my_document">
    <xsl:for-each select="person[$gender = '' or gender = '$gender']">
        <p>Name: <xsl:value-of select="name"/></p>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
---------------------------------------------
1. if the value of parameter "gender" is the "default default" (""),
then what if anything will be displayed?

2. if parameter "gender" has a value of "Male", will "Name: Joe" be the
output?

I read in your (Michael's) book about the "or" only evaluating the 2nd
operand if the 1st operand is false.  But I still don't quite understand
how the xpath select will be evaluated if the parameter has a "" value -
the first operand ($gender='') will then be true, which will essentially
make the xpath select="person[$gender='']" right?  And I don't know how
xpath will interpret that.

Please straighten me out ;-)

TIA.

Hardy Merrill

>>> mike@xxxxxxxxxxxx 10/04/04 11:56AM >>>
Firstly, a stylsheet parameter always has a value. Let's suppose the
default
value is "$$$" (the "default default" is ""). Then you can write your
query
as

//item[$param = '$$$' or @field = $param]

Michael Kay
http://www.saxonica.com/ 

 

> -----Original Message-----
> From: Hardy Merrill [mailto:HMerrill@xxxxxxxxxxxxxxxx] 
> Sent: 04 October 2004 16:10
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> Subject: [xsl] 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