Reputation: 73
Currently I have some xml structure like this:
<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>
I would like to ask is there any way using xpath to extract value of enumeration[@text] tag with value equals with text in property[@name='value']. In this case the expectation text "Australia".
It's just a first time I use xpath, any idea will be appreciated. Thanks all.
Upvotes: 3
Views: 3647
Reputation: 243449
Use:
/*/restriction/*[@value = /*/property[@name='value']]/@text
This selects any text
attribute of any child element of /*/restriction
, whose value
attribute is equal to the string value of a property
child of the top element, that (the property
child) has a name
attribute, whose string value is the string "value"
.
If you don't want to select the attribute, but just its string value, use:
string(/*/restriction/*[@value = /*/property[@name='value']]/@text)
XSLT - based verification:
<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>
When this transformation is applied on the provided XML document:
<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>
the two xpath expressions are evaluated against the above document and the string values of the results of these evaluations (properly delimited) are copied to the output:
Australia
==========
Australia
Upvotes: 3
Reputation: 56162
Use:
/*/*/enumeration[@value = ../../*[@name = 'value']]/@text
Upvotes: 3