【问题标题】:Xpath get value from parent node of siblingsXpath 从兄弟节点的父节点获取值
【发布时间】:2012-11-12 04:00:49
【问题描述】:

目前我有一些这样的 xml 结构:

<element type="Input" name="nationality">
   <property i18n="true" text="Nationality" prefix="person.nationality."
    name="caption">caption</property>
   <property i18n="true" text="Nationality" prefix="person.nationality."
    name="desc">desc</property>
   <property name="visible">1</property>
   <property name="mandatory">0</property>
   <property name="value">AUS</property>
   <restriction prefix="country." base="String">
    <enumeration text="Albania" value="ALB" />
    <enumeration text="Algeria" value="DZA" />
    <enumeration text="Argentina" value="ARG" />
    <enumeration text="Australia" value="AUS" />
    <enumeration text="Austria" value="AUT" />
    <enumeration text="Bahrain" value="BHR" />
   </restriction>
</element>

我想问有没有什么方法可以使用 xpath 来提取枚举[@text] 标记的值,其值等于属性中的文本 [@name='value']。在这种情况下,期望文本“澳大利亚”。

这只是我第一次使用 xpath,任何想法将不胜感激。谢谢大家。

【问题讨论】:

    标签: xml xslt xpath parent-child siblings


    【解决方案1】:

    用途:

    /*/*/enumeration[@value = ../../*[@name = 'value']]/@text
    

    【讨论】:

      【解决方案2】:

      使用

      /*/restriction/*[@value = /*/property[@name='value']]/@text
      

      这将选择/*/restriction 的任何子元素的任何text 属性,其value 属性等于顶部元素的property 子元素的字符串值,即(property 子元素)具有一个name属性,其字符串值为"value"

      如果您不想选择属性,而只想选择其字符串值,请使用:

      string(/*/restriction/*[@value = /*/property[@name='value']]/@text)
      

      基于 XSLT 的验证

       <xsl:template match="/">
        <xsl:value-of select=
        "/*/restriction/*[@value = /*/property[@name='value']]/@text"/>
      ==========
        <xsl:value-of select=
        "string(/*/restriction/*[@value = /*/property[@name='value']]/@text)"/>
       </xsl:template>
      </xsl:stylesheet>
      

      当此转换应用于提供的 XML 文档时:

      <element type="Input" name="nationality">
         <property i18n="true" text="Nationality" prefix="person.nationality."
          name="caption">caption</property>
         <property i18n="true" text="Nationality" prefix="person.nationality."
          name="desc">desc</property>
         <property name="visible">1</property>
         <property name="mandatory">0</property>
         <property name="value">AUS</property>
         <restriction prefix="country." base="String">
          <enumeration text="Albania" value="ALB" />
          <enumeration text="Algeria" value="DZA" />
          <enumeration text="Argentina" value="ARG" />
          <enumeration text="Australia" value="AUS" />
          <enumeration text="Austria" value="AUT" />
          <enumeration text="Bahrain" value="BHR" />
         </restriction>
      </element>
      

      根据上述文档评估两个 xpath 表达式,并将这些评估结果的字符串值(正确分隔)复制到输出

      Australia
      ==========
        Australia
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-18
        • 1970-01-01
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 2015-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多