简体   繁体   中英

How to change the root of <xsl:for-each > dynamically in xslt file

I want to change the root of <xsl:for-each> dynamically. Actually I used nested loop. Based on the outer loop I want to change the root of inner loop. For that I write the following code but I am not able to do it.

outer loop start then variable that hold root

<xsl:variable name="cdtitle">
    <xsl:value-of select="/root/Data/AppNameEncrpt"/>
    </xsl:variable>  
    inner loop
    <xsl:for-each select="msxsl:node-set($cdtitle)">
    ..
    </xsl:for-each>

If I pass the static value of msxsl:node-set($cdtitle) then it work but when I pass it as a varible in root it not work

How can achieve the same. Can anyone help me.

In XSLT (as in any functional language), variables are immutable. However, inside a "loop" (which technically is not a loop) you can set the variable to anything that is in the context of that loop. Since you didn't show any input XML, I am guessing to what you want to achieve, but there's always a (simple) way.

Suppose you have this variable:

<xsl:variable name="cdtitles" select="/data/cds/title" />

It will hold all cd titles. Now I can do:

<xsl:for-each select="$cdtitles">
    <xsl:variable name="cdtitle" select="." />
    <!-- do something with the title, var changes with each title -->
</xsl:for-each>

Have a look at XSLT: Getting started , it will help you understand these and other principles of the language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM