如何从嵌套循环内访问外循环中的元素? [英] How do I access elements from the outer loop from within nested loops?

查看:35
本文介绍了如何从嵌套循环内访问外循环中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌套了 xsl:for 循环:

I have nested xsl:for loops:

<xsl:for-each select="/Root/A">
    <xsl:for-each select="/Root/B">
        <!-- Code -->
    </xsl:for>
</xsl:for>

从内循环中,如何从外循环中的当前节点访问属性?

我一直发现自己在写这样的代码:

I keep finding myself writing code like this:

<xsl:for-each select="/Root/A">
    <xsl:variable name="someattribute" select="@SomeAttribute"/>
    <xsl:for-each select="/Root/B">
        <!-- Now can use $someattribute to access data from 'A' -->
    </xsl:for>
</xsl:for>

这不能很好地扩展,因为有时我需要访问多条信息并最终为每条信息创建一个变量.有没有更简单的方法?

This doesn't scale very well, as sometimes I need to access several pieces of information and end up creating one variable for each piece. Is there an easier way?

推荐答案

您可以将整个/Root/A 结构存储在一个变量中,并引用该变量,而不是为您需要的每个属性和子元素创建一个新变量访问.

You can store the entire /Root/A structure in a variable, and make reference to that variable rather than creating a new variable for every attribute and subelement you need to access.

<xsl:for-each select="/Root/A/">
    <xsl:variable name="ROOT_A" select="."/>
    <xsl:for-each select="/Root/B/">
         <!-- Variable is accessed like this: $ROOT_A/@someAttribute
              Just like a normal XML node -->
    </xsl:for-each>
</xsl:for-each>

这篇关于如何从嵌套循环内访问外循环中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆