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

Re: Variable in XPath

Subject: Re: Variable in XPath
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 27 Jun 2007 19:19:25 +0200
Re:  Variable in XPath
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.


-- Abel Braaksma

Current Thread


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.
First Name
Last Name
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.