Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

xml - XPath based on named field value

What XPath query should I do to retrieve all property prop_id and prop_name fields where the property contains a field <Field name="prop_label">resource_workskills</Field> In this sample, it should just load:

<Field name="prop_id">100</Field>
<Field name="prop_name">Work Skills</Field>

Sample data:

<?xml version="1.0" encoding="UTF-8"?>
<properties>
  <property>
    <Field name="prop_id">100</Field>
    <Field name="prop_label">resource_workskills</Field>
    <Field name="prop_entity_type">3</Field>
    <Field name="prop_name">Work Skills</Field>
    <Field name="Property ID">100</Field>
    <Field name="Property Language">1</Field>
    <Field name="Text Entry Identifier">0</Field>
  </property>
  <property>
    <Field name="prop_id">101</Field>
    <Field name="prop_label">resource_capacity_categories</Field>
    <Field name="prop_entity_type">3</Field>
    <Field name="prop_name">Capacity Categories</Field>
    <Field name="Property ID">101</Field>
    <Field name="Property Language">1</Field>
    <Field name="Text Entry Identifier">0</Field>
  </property>
</properties>

I do not know how to work with the value of a named field.

Thanks!

question from:https://stackoverflow.com/questions/65833247/xpath-based-on-named-field-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This XPath,

/properties/property[Field[@name="prop_label" and .="resource_workskills"]]
           /Field[@name='prop_id' or @name='prop_name']

will select all Field elements with prop_id or prop_name @name attribute values that are children of a property element that has a child Field element with a prop_label @name attribute value and a string value of "resource_workskills":

<Field name="prop_id">100</Field>
<Field name="prop_name">Work Skills</Field>

as requested.


This is great @kjhughes Thanks! What if I want to use a wildcard in the conditional, such as .="resource_*" so I match all starting by resource_

See: starts-with()

See also: contains() and ends-with()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...