xsl-list
[Top] [All Lists]

RE: newbie - parameters with/without values??

2004-10-05 02:37:30
Hi Hardy,

I will try explaining.

The [something] is a filter.
A <xsl:for-each select="person"> would select all person elements (that are children of the current element). When you filter it with "person[gender = $gender]" you say that the processor must only select those person elements that have a child element with the value in the variable $gender. Here you have a syntax error as you have written [gender = '$gender'] which says that the element
gender (which is a child of person) must have a string value of "$gender".
That would be the same in many other programming languages as the difference between
(shown in JavaScript syntax)
var a = "This";
var b = a;     ~ gender = $gender
var b = "a";  ~ gender = "$gender"

If the filter is true for all elements, then it is the same as having no filter: <xsl:for-each select="person[1=1]"> is the same as <xsl:for-each select="person"> and if the filter is an absurdity (false for all elements), ie. <xsl:for-each select="person[1=0]"> then no elements are chosen.

So in your expression (syntax error corrected):
<xsl:for-each select="person[$gender='' or gender=$gender]>
If $gender is the default, then the first expression will always be true, and every person (element) is then chosen. If, as you said, $gender is 'Male' then the first expression is false, and the second expression will only be true in the example that you mentioned (the Joe person).

I hope that this helps.

Regards,
Ragulf Pickaxe :-)



From: "Hardy Merrill" <HMerrill(_at_)dhcr(_dot_)state(_dot_)ny(_dot_)us>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: 
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>,<mike(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] newbie - parameters with/without values??
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

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963



<Prev in Thread] Current Thread [Next in Thread>