Re: [xsl] Custom node-set in a variable
Subject: Re: [xsl] Custom node-set in a variable|
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 17 Feb 2005 11:48:29 -0500
At 09:42 AM 2/17/2005, you wrote:
Now I need to exclude certain xml elements from the transformations by
The list of such elements is long.
Can I use a variable inside my XSL as a custom node-set, something like:
You can do this, but it requires a little extra effort to do so in XSLT
1.0. This is because a variable declared this way is not bound to a true
node-set, but rather to a result tree fragment -- which is much like a node
set with the caveat that you can't query into it -- which is,
unfortunately, exactly what you need to do.
The XSLT 1.0 workaround to this problem takes two forms:
* use an extension function such as exslt:node-set() to turn the result
fragment into a node set
* use the document() function to query the document where this node set
appears, instead of simply using the variable as bound. Since document('')
returns the stylesheet as a (parsed) document, this can be done like so:
So now $bypass-names is actually a node set, which you can traverse.
Now as for your real question....
And what would be the good way to handle the exclusion, keeping in mind
Actually, not as you have it, like
but rather something like
This is nice because then the names you want to bypass can be collected as
a set of nodes whose values, not names, can be checked against. That is,
since $bypass-names (using the declaration described above) will be a set
of <name> elements, the names you want to exclude is available as the
*values* of the node-set $bypass-names.
The equality operator "=" in XPath is very handy for the test you then need
to do ... for any element in your input, when
test="name() = $bypass-names"
you know its name is in the set.
(You might want to research the name() and local-name() functions, since it
could matter which one you use, if you have namespaces in play.)
This is a special application of a "lookup table", which you will find
documented in the XSLT FAQ and elsewhere.
Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
Mulberry Technologies: A Consultancy Specializing in SGML and XML