Xml 在XSLT中创建嵌套循环
我有一个创建货币对所需的货币代码列表。下面是一个(简化的)示例Xml 在XSLT中创建嵌套循环,xml,xslt,nested-loops,cartesian-product,Xml,Xslt,Nested Loops,Cartesian Product,我有一个创建货币对所需的货币代码列表。下面是一个(简化的)示例 美元 BRL 欧元 英镑 我试图将每种货币与其他货币进行匹配,以创建这样的货币对(笛卡尔乘积) USDBRL,USDEUR,USDGBP,BRLUSD,BRLEUR,BRLGBP,EURMUSD,EURBRL,EURBP(你明白了) 我可以循环使用xslt并获取每个值,但我不确定如何再次获取这些值 这是我的示例xslt代码 <xsl:template match="/"> <CurrencyPairs
美元
BRL
欧元
英镑
我试图将每种货币与其他货币进行匹配,以创建这样的货币对(笛卡尔乘积)
USDBRL,USDEUR,USDGBP,BRLUSD,BRLEUR,BRLGBP,EURMUSD,EURBRL,EURBP(你明白了)
我可以循环使用xslt并获取每个值,但我不确定如何再次获取这些值
这是我的示例xslt代码
<xsl:template match="/">
<CurrencyPairs>
<Total>
<xsl:value-of select="count(CurrencyLists/Currency)"></xsl:value-of>
</Total>
<xsl:for-each select="CurrencyLists/Currency">
<!--<CurrencyPair><xsl:value-of select="."/></CurrencyPair>-->
<xsl:variable name="first" select="."/>
<first><xsl:value-of select="$first"/></first>
<!-- nested loop / cartesian here -->
</xsl:for-each>
</CurrencyPairs>
</xsl:template>
如果我在中间加上一个第二个,我就不会得到任何输出。我在这里搜索了一下,但没有发现任何相关的东西。我正在尝试做一些事情,允许添加更多的货币代码(如日元、瑞士法郎、泰铢),而不必手工创建成对/笛卡尔乘积(这就是我现在正在做的)
谢谢 对于每个的
内部,您当然可以有另一个具有绝对路径的
或与每个外部路径相对的路径,例如
您可以非常简单地执行此操作,如下所示:
<xsl:template match="/">
<CurrencyPairs>
<xsl:variable name="allCurrencies" select="CurrencyLists/Currency" />
<Total>
<xsl:value-of select="count($allCurrencies)"></xsl:value-of>
</Total>
<xsl:for-each select="$allCurrencies">
<xsl:variable name="first" select="."/>
<xsl:for-each select="$allCurrencies[. != $first]">
<CurrencyPair>
<First>
<xsl:value-of select="$first" />
</First>
<Second>
<xsl:value-of select="." />
</Second>
</CurrencyPair>
</xsl:for-each>
</xsl:for-each>
</CurrencyPairs>
</xsl:template>
完美!这很有效。非常感谢你。我不确定我在做什么,因为它不可见。不过我有嵌套的循环
<xsl:template match="/">
<CurrencyPairs>
<xsl:variable name="allCurrencies" select="CurrencyLists/Currency" />
<Total>
<xsl:value-of select="count($allCurrencies)"></xsl:value-of>
</Total>
<xsl:for-each select="$allCurrencies">
<xsl:variable name="first" select="."/>
<xsl:for-each select="$allCurrencies[. != $first]">
<CurrencyPair>
<First>
<xsl:value-of select="$first" />
</First>
<Second>
<xsl:value-of select="." />
</Second>
</CurrencyPair>
</xsl:for-each>
</xsl:for-each>
</CurrencyPairs>
</xsl:template>