XSLT按属性值选择

时间:2014-02-25 17:23:21

标签: xml xslt

XML结构如下。如果地址类型=“C”,我需要为变量currentAddress_Street1分配文本。

<Addresses>
    <Address Type="P">
      <Street1>220 State Street</Street1>
      <Street2 />
      <City>HATTIESBURG</City>
      <State>MS</State>
      <ZipCode>12354</ZipCode>
      <Country />
     </Address>

    <Address Type="C">
      <Street1>1000 </Street1>
      <Street2 />
      <City>Jackson</City>
      <State>MS</State>
      <ZipCode>12354</ZipCode>
      <Country />
     </Address>

以下是我的尝试。对XLST来说很新。有人帮忙修改它吗?

  <xsl:variable name="currentAddress_Street1">
<xsl:value-of select="//Addresses/Address/Street1[Address/@Type='C']" />
  </xsl:variable>

2 个答案:

答案 0 :(得分:2)

可以做的另一种方法是将谓词放在Address上,如此

//Addresses/Address[@Type='C']/Street1

正如您所看到的那样,它更清洁,更容易识别您要做的事情。

答案 1 :(得分:0)

您正在测试父节点的属性,因此您必须在谓词中指定该属性,因为它在Street1的上下文中运行。

//Addresses/Address/Street1[parent::Address/@Type='C']