3

我不确定如何解决这个问题,因为我对 XSLT 还很陌生。

我正在使用xsl:for-each循环Types,这很好,但是当我在那个循环(类型)中时,我将如何选择Rate使用 中的属性TypeCode?我是否认为 select 仅具有我当时所处的 for-each 的范围?也可以有许多袋子元素。

<Bags>
<Bag>
    <Types>
        <Type TypeCode="DBL">
            <Description Name="BLAH">
                <Text>BLAH</Text>
                <Image/>
            </Description>
        </Type>
        <Type TypeCode="JRS">
            <Description Name="BLAH">
                <Text>BLAH BLAH</Text>
                <Image/>
            </Description>
        </Type>
    </Types>
    <Plans>
        <Plan PlanCode="DISC" PlanName="Best rate">
            <Description Name="Best rate">
                <Text>Best available rate</Text>
            </Description>
        </Plan>
        <Plan PlanCode="NOCC" PlanName="No CC Required">
            <Description Name="No CC Required">
                <Text>Rate- No CC Required</Text>
            </Description>
        </Plan>
    </Plans>
    <Rates>
        <Rate TypeCode="DBL" PlanCode="DISC">
            <Rates>
                <Rate>
                    <Base AmountBeforeTax="0" CurrencyCode="EUR"/>
                </Rate>
            </Rates>
        </Rate>
        <Rate TypeCode="JRS" PlanCode="DISC">
            <Rates>
                <Rate>
                    <Base AmountBeforeTax="0" CurrencyCode="EUR"/>
                </Rate>
            </Rates>
        </Rate>
    </Rates>
</Bag>
    </Bags>
4

1 回答 1

1

我是否认为 select 仅具有我当时所处的 for-each 的范围?

不是真的,XPath 表达式可以是relative,在这种情况下它是从当前节点计算出来的,或者是absolute(以 开头/),在这种情况下它是从当前文档节点计算出来的。

Even with a relative XPath expression it is possible to select nodes outside of the subtree rooted by the current node -- simply by using reverse axes or the following:: or following-sibling:: axis.

Use:

ancestor::Bag/Rates/Rate[@TypeCode = current()/@TypeCode]

This assumes that bags cannot be nested, otherwise a slightly different expression needs to be used:

ancestor::Bag[1]/Rates/Rate[@TypeCode = current()/@TypeCode]
于 2012-05-25T13:33:01.530 回答