如何根据另一个属性的值选择一个属性 [英] How to select an attribute based on another attribute's value

查看:36
本文介绍了如何根据另一个属性的值选择一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Credit_Term_Code="4" 在 XSLT 中循环时,我需要选择 NativeDescription 值:

I need to select the NativeDescription value when Credit_Term_Code="4" by looping in XSLT:

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

推荐答案

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

To select the NativeDescription attribute of the Credit_Term_parent element with a Credit_Term_Code equal to 4, use one of the following XPaths:

  1. 如果Credit_Term_parent上面的祖先结构是固定的,如下所示:

  1. If the ancestral structure above Credit_Term_parent is fixed as shown:

/Credit_code_parents/Credit_Term_parent[@Credit_Term_Code='4']/@NativeDescription

  • 如果上面有潜在的可变祖先结构Credit_Term_parent(并假设提供的 Credit_Term_Code 在整个文档中是唯一的):

  • If there's potentially variable ancestral structure above Credit_Term_parent (and assuming that the provided Credit_Term_Code is unique across the document):

    //Credit_Term_parent[@Credit_Term_Code='4']/@NativeDescription
    

  • 您要求 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.

    ...or, alternatively, use the XPath from #1 above instead of the one from #2.

    这篇关于如何根据另一个属性的值选择一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆