3

現在、私は次のような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を使用して、プロパティ[@ name='value']のテキストと等しい値を持つ列挙[@text]タグの値を抽出する方法はありますか。この場合、期待値テキスト「オーストラリア」。

xpathを使用するのは初めてですが、どんなアイデアでもありがたいです。皆さんありがとう。

4

2 に答える 2

3

使用

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

これにより、textの任意の子要素の任意の属性が選択されます。この属性は、最上位要素の子の文字列値と等しく、 (子)には属性があり、その文字列値は文字列です。/*/restrictionvaluepropertypropertyname"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>

2つのxpath式が上記のドキュメントに対して評価され、これらの評価の結果の文字列値(適切に区切られている)が出力にコピーされます

Australia
==========
  Australia
于 2012-11-12T04:44:37.823 に答える
3

使用する:

/*/*/enumeration[@value = ../../*[@name = 'value']]/@text
于 2012-11-12T04:12:03.677 に答える