Re: [xsl] Can I substitute a predefined path/expression within the [..] tags?

Subject: Re: [xsl] Can I substitute a predefined path/expression within the [..] tags?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 23 Jan 2003 15:26:56 +0000
Hi Ganesh,

> I have several references in my stylesheet to a pattern that resembles:
>
> Node[@name='AA' or @name='BB']
>
> With time, I continuously need to update this pattern to include
> newer attributes @name='CC', @name='DD' and so on.
>
> Instead of repeating this addition in every match in the stylesheet,
> I am wondering if there is some way to store this path/expression:
>
> myexpression = "Node[@name='AA' or @name='BB']" or
> myexpression = "@name='AA' or @name='BB' "
>
> and use 'myexpression' in my pattern matches. That way, I will have
> only one location to update. I have tried using variables without
> success.

I think that the best thing is to construct a list of the names that
you're interested in and then check whether @name is in that list.

You could use a string containing the list:

  <xsl:variable name="names" select="' AA BB '" />
  <xsl:apply-templates
    select="Node[contains($names, concat(' ', @name, ' '))]" />

Or you could construct a node set containing the names as a list of
elements. For example, you could have a separate document, names.xml,
that looked like:

<names>
  <name>AA</name>
  <name>BB</name>
</names>

and then do:

  <xsl:variable name="names"
                select="document('names.xml')/names/name" />
  <xsl:apply-templates select="Node[@name = $names]" />

You could use the latter method with a result tree fragment that you
convert to a node set using an extension node-set() function if you
didn't want to create a separate document.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread