Consider this simple xml element example:
<parent foo="1" bar="2" foobar="3">
<child/>
</parent>
In the xsl file, I am in the context of "parent" (i.e. within the <template match="parent">). I want to select a node set (in the example, only one attribute) based upon a string variable. For example i want to select a node-set which matches $attribute-name. I'll show my failed xsl example and you will probably understand what i'm trying to do.
<xsl:template match="parent">
<xsl:call-template name="print-value-of">
<xsl:with-param name="attribute-type" select="'foo'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="print-value-of">
<xsl:param name="attribute-type"/>
<xsl:value-of select="$attribute-type"/>
</xsl:template>
This prints the output:
foo
What I first INTENDED it to do (but I realize that this is not what it should do) is:
- evaluate the variable attribute-type (or param, if you want to be picky) as the string 'foo'
- call the value-of as if I had called <xsl:value-of select="foo"/>
I.e. what I wanted it to print was:
1
THE QUESTION: How can I achieve this behaviour?
Notice: I am aware of the fact that I, in this simple case, could pass the actual attribute node as a parameter (i.e. <xsl:with-param name="attribute" select="foo"/>). But that is not the solution I am searching for. I need to pass only information about the attribute type (or attribute name if you'd prefer to call it that)
What I am actually trying to do is creating a general function template which can:
- Call a function (call-template) with the attribute-type as a parameter
- In the function do a bunch of operations which give me a node set, stored in a variable
- sum all of the attributes of the elements in the node set, which are of the previously selected attribute-type
<EDIT>
I can only use XSLT 1.0, so 1.0 solutions are much preferred!
</EDIT>
<EDIT2>
A follow-up question on a similar theme: Is it also possible to create attributes of a with the name/type specified by a string variable? I.e.
<xsl:attribute name="$attribute-type"/>
Doing it like the line above results in $attribute-type being the literal name of the attribute in the xml output. Instead I would like it, again it to evaluate the variable and give the evaluated value as the name.
</EDIT2>