[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Variable in XPath


Subject: Re: [xsl] Variable in XPath
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 27 Jun 2007 19:19:25 +0200

Hi Garvin,

see my comments.



Garvin Riensche wrote:

For each attribute there has to be a variable, $id, $owner and $name in this case, which is set from commandline.

though this will work for the quick and dirty solution, you may want to use a more flexible solution if you have many attributes to match. I.e., provide an XML file with the settings that you search for, or make the attribute names variables as well. Note (see all the earlier responses) that you should not use an xsl:variable, but an xsl:param instead.


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.

that would never be a good idea of course ;)


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.

which is neither a good idea. ;)



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]">

well, apart from my previous post, which will still work well with your requirements (but you must adjust the predicates), consider this predicate or test:


class[@id = $id or empty($id)][@owner = $owner or empty($owner)]

or consider this (where the default is the current attribute):

class[($id, @id)[1] = @id][($owner, @owner)[1] = @owner]

Note however the subtle differences between the two: the first will be true if there is no @id on class element, if there is no $id on the commandline. The second will be true if there is no $id on the commandline, but will be false if there is no @id at all on the current class element. Same holds for $owner etc.


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?

Not really. You cannot easily set a value that always evaluates to true in an equality expression. And why would you want that? My second solution above uses the current attribute as a default value.


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).

if you have lots of xsl:whens, there's something wrong ('wrong' is in the eye of the beholder of course) in your design of your stylesheet. Perhaps you do not want xsl:when at all, perhaps you want to use apply-templates, because, if you have more than one match, you select all of them within the apply-templates call.


Hth,

Cheers,
-- Abel Braaksma


Current Thread
Keywords
xml