如何根据其他属性的值选择属性

时间:2014-11-18 15:08:12

标签: xml xslt xpath

我需要在NativeDescription中通过在XSLT中循环来选择Credit_Term_Code="4"值:

<Credit_code_parents>
  <Credit_Term_parent Credit_Term_Code="1" NativeDescription="Letter of Credit" EnglishDescription="Letter of Credit" />
  <Credit_Term_parent Credit_Term_Code="2" NativeDescription="Cash on Delivery" EnglishDescription="Cash on Delivery" />
  <Credit_Term_parent Credit_Term_Code="3" NativeDescription="Contract" EnglishDescription="Contract" />
  <Credit_Term_parent Credit_Term_Code="4" NativeDescription="Net" EnglishDescription="Net" />
  <Credit_Term_parent Credit_Term_Code="5" NativeDescription="Contract" EnglishDescription="Contract" />
  <Credit_Term_parent Credit_Term_Code="6" NativeDescription="Net" EnglishDescription="Net" />
  <Credit_Term_parent Credit_Term_Code="7" NativeDescription="Contract" EnglishDescription="Contract" />
  <Credit_Term_parent Credit_Term_Code="8" NativeDescription="Net" EnglishDescription="Net" />
</Credit_code_parents>

2 个答案:

答案 0 :(得分:3)

要选择NativeDescription元素的Credit_Term_parent属性且Credit_Term_Code等于4,使用以下XPath之一

  1. 如果Credit_Term_parent上方的祖先结构如图所示是固定的:

    /Credit_code_parents/Credit_Term_parent[@Credit_Term_Code='4']/@NativeDescription
    
  2. 如果上面有可能变化的祖先结构 Credit_Term_parent(并假设提供的Credit_Term_Code在整个文档中是唯一的):

    //Credit_Term_parent[@Credit_Term_Code='4']/@NativeDescription
    
  3. 您要求使用XSLT循环代码:

    <xsl:for-each select="/Credit_code_parents/Credit_Term_parent">
       <xsl:if test="@Credit_Term_Code=4">
         <xsl:value-of select="@Credit_Term_parent"/>
       </xsl:if>
    </xsl:for-each>
    

    或者,没有循环:

     <xsl:value-of
              select="//Credit_Term_parent[@Credit_Term_Code='4']/@NativeDescription"/>
    

    ...或者,使用上面#1中的XPath而不是#2中的XPath。

答案 1 :(得分:0)

尝试以下

<xsl:for-each select="Credit_code_parents/Credit_Term_parent">
  <xsl:variable name="temp" select="@Credit_Term_Code"/>
  <xsl:if test="$temp='1'">
    <check>
      <xsl:value-of select="@NativeDescription"/>
    </check>
  </xsl:if>
</xsl:for-each>

希望有所帮助。