RVK
RVK

Reputation: 1035

Accessing XML nodes using XPath

<attributes>
  <attribute name="Mail Zone" property="alerts.p45" type="">
    <value>mailzone</value>
  </attribute>
  <attribute name="Employee Name" property="acm_alert_custom_attributes.cs11" type="">
    <value>employeename</value>
  </attribute>
  <attribute name="Manager Name" property="alerts.p23" type="">
    <value><managername></value>
  </attribute>

How can I select the node <value> based on the attribute "name" of the above XML using XPath?

Upvotes: 0

Views: 369

Answers (1)

Welbog
Welbog

Reputation: 60378

Let's say you wanted to select the value element that is a child of the attribute whose name is "Employee Name".

The XPath expression for that is the following:

/attributes/attribute[@name="Employee Name"]/value

In XSL you can use it like this:

<xsl:value-of select="/attributes/attribute[@name='Employee Name']/value"/>

It's built up the following way. First you want to select a value attribute, whose parent is an attribute element, whose parent is the root (I'm assuming) attributes. The / operator indicates the parent-child relationship between elements. So an expression to select all value elements would be the following:

/attributes/attribute/value

With that as a base, you want to filter the result by some other attribute. In this case, you want to filter by the name attribute of the attribute element (your choice of names might make things hard to follow). Filtering is done with [] clauses on the elements you want to filter by. In your case that's the attribute element:

/attributes/attribute[]/value 

Now you need to put something in the filter. The @ symbol indicates attributes of the element you're filtering by, followed by the name of the attribute you want. Then you compare the attribute to some known value, to arrive at the above expression:

/attributes/attribute[@name='filter']/value

Upvotes: 6

Related Questions