Re: [xsl] Variable in XPath

Subject: Re: [xsl] Variable in XPath
From: Garvin Riensche <g.riensche@xxxxxxx>
Date: Wed, 27 Jun 2007 18:02:52 +0200
Hi Andrew,

Thanks for your answer but it's not exactly that what I was looking for. Maybe I have to explain my problem a bit more. Consider again the following XML:

<facts>
  <class id="2" owner="1" name="A"/>
  <class id="3" owner="1" name="B"/>
<facts>

This is a simple example, there might be input with nodes containing more attributes.

For each attribute there has to be a variable, $id, $owner and $name in this case, which is set from commandline. When writing the stylesheet I dont't know which classes are goint to be selected. This could be all classes, classes selected by id, classes selected by id and name, ect. So there are lots of possibilities - and even more if there are more attributes. The stylesheet would be very large if I would use a <xsl:when> for every possible combination. So my idea was to use a default value for each variable, so that all (class) nodes are selected if the variable is not set.

For example:

I call saxon with $owner="1" $name="A" then $id is empty and I want the following test to return true:

<xsl:when test="doc('factbase.xml')/facts/class[@id eq $id and @owner eq $owner and @name eq $name]">

This will return false because $id is empty. My quesion is, is there a default value that I can put in the select of <xsl:variable name="id" select"?????"/> so that the test above will return true?
It would be great if that would work somehow because with lots of <xsl:when>s I think it would be much more complicated (considering more attributes).


regards,
Garvin


On 6/27/07, Andrew Welch <andrew.j.welch@xxxxxxxxx> wrote:
Seems like a long-winded way of just saying you want to check if <class> exists:

<xsl:when test="doc('factbase.xml')/facts/class">

If you then want to do something specific if the @id eq $id then add a
condition inside the xsl:when.

Or its probably cleaner to just add another when:


<xsl:when test="doc('factbase.xml')/facts/class[@id eq $id]">
 there is a class with a matching @id
</xsl:when>
<xsl:when test="doc('factbase.xml')/facts/class">
 there are one or more class elements, but none of them a matching @id
</xsl:when>
<xsl:otherwise>
 this aren't any class elements...
</xsl:otherwise>

You'd want to refactor the call to doc() into a variable too.

Current Thread