Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] Can templates be invoked based on the value of a variable?

From: "Xia Li" <xli@------------->
To:
Date: 4/1/2009 8:27:00 PM
I think you might achieve this by first constructing a variable with a
string value,

<xsl:variable name="item" select="concat('item', $i)"></xsl:variable>

then,

apply the templates on those elements whose name is the value of the
variable "item" like this,

<xsl:apply-templates select="element()[local-name() = $item]" />


Lisa



-----Original Message-----
From: pilgrim cnonline.net [mailto:pilgrim@xxxxxxxxxxxx]
Sent: Wednesday, April 01, 2009 12:52 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Can templates be invoked based on the value of a
variable?

I would like to be able to invoke templates based on the value of a
variable.

I've done some Internet searching, but haven't seen what I'm looking
for, so
I don't know whether it's possible or not.  My various attempts have
failed.

My data has the structure:

<data>
    <item1>
        ...
    </item1>
    <item2>
        ....
    </item2>
</data>


I want to process the nodes <item1/> or <item2/>, but not both,
depending on
a variable derived from some other processing.  The outline of the XSL
processing
that I'd like to do is:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

    <xsl:template match="data">

        <xsl:variable name="i">2</xsl:variable>

        <!-- $item will have a value of either "item1" or "item2" -->
        <xsl:variable name="item">item<xsl:value-of
select="$i"/></xsl:variable>

        <!--  Want to invoke template for either item1 or item2, but
not both.  -->
        <!--  Would like to avoid using a 'choose' statement.  -->
        <!--  This does not work, but is conceptually what I'm thinking.
-->
        <xsl:apply-templates select="$item" />

    </xsl:template>

    <xsl:template match="item1">
        ...
    </xsl:template>

    <xsl:template match="item2">
        ...
    </xsl:template>

</xsl:stylesheet>


I could use a <choose/> statement to do the selection based on the
$item variable,
but I'm wondering whether there is a more "elegant" way, or whether
the <choose/>
is the way to go even though the code is longer and "bulkier".

Thanks for suggestions.

-- Mike


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent