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?

Answers


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

Need Your Help

Tools for automating mouse and keyboard events sent to a windows application

testing automation

What tools are useful for automating clicking through a windows form application? Is this even useful? I see the testers at my company doing this a great deal and it seems like a waste of time.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.