DazzlaJ
DazzlaJ

Reputation: 57

XSLT transversing

I am not sure how to go about this problem as I am pretty new to XSLT.

I am using an xsl:for-each to loop through the Types, which is fine but when I am in that loop(Type) how would I select the Rate using the attribute in the TypeCode? Am I right in thinking the select only has the scope of the for-each that I am in at that time? There could be many bag elements as well.

<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>

Upvotes: 3

Views: 97

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

Am I right in thinking the select only has the scope of the for-each that I am in at that time?

Not really, an XPath expression can be relative, in which case it is evaluated off the current node, or absolute (starting with /) in which case it is evaluated off the current document node.

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]

Upvotes: 1

Related Questions