Gs.
Gs.

Reputation: 592

How do I use a variable in select in xsl

now

<xsl:for-each select="tm-reg-gaz/trademark/img">

want to change like this.

<xsl:variable name="tag_name">
<xsl:if test="tm-reg-gaz">tm-reg-gaz</xsl:if>
<xsl:if test="mp-tm-reg-gaz">mp-tm-reg-gaz</xsl:if>
</xsl:variable>

<xsl:for-each select="$tag_name/trademark/img">
</xsl:for-each>

I tried these

<xsl:for-each select="$tag_name/trademark/img">
<xsl:for-each select="path[$tag_name]/trademark/img">
<xsl:for-each select="{$tag_name}/trademark/img">

Let me know if it can. Thank you.

Upvotes: 0

Views: 81

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

You can use select="*[name()=$tagname]/trademark/img".

A better approach is probably

<xsl:variable name="selection" select="tm-reg-gaz|mp-tm-reg-gaz">

<xsl:for-each select="$selection/trademark/img">
</xsl:for-each>

(But the details depend on whether both elements can appear or whether they are mutually exclusive).

Upvotes: 1

barbwire
barbwire

Reputation: 218

Here is my proposal, feel free to shoot it down:

<xsl:variable name="variable" as="node()*">
        <xsl:choose>
            <xsl:when test="$testme = 'something'">
                <xsl:copy-of select="//tm-reg-gaz/trademark/img"/>               
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="//mp-tm-reg-gaz/trademark/img"/>  
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>        
    <xsl:for-each select="$variable">
        <!-- do something -->
    </xsl:for-each>

Upvotes: 1

Related Questions