質問

I am trying to sum some XML values in one node based on a value in another node.

The node in Entry that relates to Item is always one RecordNo less than the RecordNo in Item. So the Entry with RecordNo 1 is related to the Item with RecordNo 2.

I want to sum all Item/Cost nodes where IsValid = 1 for the corresponding Entry Node. I can only use XSLT version 1.0.

I have tried

Sum(../Items/Item[../Entries/Entry[IsValid=1 and RecordNo -1 = ../Entries/Entry/RecordNo]])

My desired output from the example below would be 22.

<Root>
<Items>
    <Item>
        <Cost>10</Cost>
        <RecordNo>2</RecordNo>
        <Type>1</Type>
    </Item>
    <Item>
        <Cost>12</Cost>
        <RecordNo>5</RecordNo>
        <Type>1</Type>
    </Item>
    <Item>
        <Cost>10</Cost>
        <RecordNo>9</RecordNo>
        <Type>2</Type>
    </Item>
</Items>
<Entries>
    <Entry>
        <IsValid>1</IsValid>
        <RecordNo>1</RecordNo>
    </Entry>
    <Entry>
        <IsValid>1</IsValid>
        <RecordNo>4</RecordNo>
    </Entry>
    <Entry>
        <IsValid>0</IsValid>
        <RecordNo>8</RecordNo>
    </Entry>
</Entries>
</Root>
役に立ちましたか?

解決

You should solve with this expression:

sum(/Root/Items/Item[RecordNo -1=/Root/Entries/Entry[IsValid=1]/RecordNo]/Cost)

The problem with yours is that you are messing up with the context node, remember that any relative path expression in the predicate (the expression you put between the brackets) refers to the current element, the one that will be picked in case the expression evaluates to true.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top