xsl-list
[Top] [All Lists]

Re: Variable creation and scoping

2003-04-15 12:52:33


<xsl:variable name="c1">
    <xsl;value-of select="document('external.xml')/a/b/c/@foo *
document('external.xml')/a/b/c/@bar"/>
</xsl:variable>

Note using xsl:variable with content rather than a select attribute
is relatively expensive. the above creates a result tree fragment
consisting of a root node containing a text node containing the value as a
string.

If you went

<xsl:variable name="c1"
 select="document('external.xml')/a/b/c/@foo *
document('external.xml')/a/b/c/@bar"/>

then the variable would contain a number, which is less to store and
quicker to use as a number later.

Is there an easier/cleaner/better way to do this that will allow me to not
edit the xsl when the xml file used to create the variables is altered?

Probably but the problem seems a little underspecified?
Do you need a variables at all? If there are an unknown number of
usable foo bar attribute pairs then it seems like you don't want
variables for each product, as that would limit you to a fixed number.

I'd go

<xsl:variable name="foobar"
select="document('external.xml')/a/b/c[string(@foo) and string(@bar)]"/>

now $foobar contains all the usable c nodes and to get the 4th 
product
<xsl:value-of select="$foobar[4]/@foo * $foobar[4]/@bar"/>

David




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



<Prev in Thread] Current Thread [Next in Thread>