XPath从表兄节点中选择一个值

时间:2015-10-27 23:17:06

标签: xml xslt xpath

我有以下xml

<FreightLabelData>
  <Consignments>
    <Consignment>
      <References>
        <Reference>
          <ReferenceNumber>RefLine1</ReferenceNumber>
          <SequenceNumber>1</SequenceNumber>
        </Reference>
        <Reference>
          <ReferenceNumber>RefLine2</ReferenceNumber>
          <SequenceNumber>2</SequenceNumber>
        </Reference>
      </References>
      <FreightLabels>
        <FreightLabel>
          <ReferenceSequenceNumber>1</ReferenceSequenceNumber>
        </FreightLabel>
        <FreightLabel>
          <ReferenceSequenceNumber>2</ReferenceSequenceNumber>
        </FreightLabel>
      </FreightLabels>
    </Consignment>
  </Consignments>
</FreightLabelData>

我的xslt有一个以

启动的for循环
<xsl:for-each select="/FreightLabelData/Consignments/Consignment/FreightLabels/*">

对于每个FreightLabel,我需要找到相关的堂兄参考/参考编号,其中FreightLabel / ReferenceSequenceNumber = Reference / SequenceNumber

我花了很多时间寻找答案而没有任何运气。有谁知道怎么做?

由于

1 个答案:

答案 0 :(得分:1)

在XSLT中,您可以使用:

/FreightLabelData/Consignments/Consignment/References/Reference[SequenceNumber=current()/ReferenceSequenceNumber]

但是,最好使用XSLT的专用机制来处理交叉引用。首先在样式表的顶层定义一个键:

<xsl:key name="ref" match="Reference" use="SequenceNumber" />

然后,从FreightLabel的上下文中,您可以使用以下表达式选择相应的Reference

key('ref', ReferenceSequenceNumber)

请参阅:http://www.w3.org/TR/xslt/#key