Allen BMC
Allen BMC

Reputation: 27

XPath to select multiple elements

I have an XML as shown below:

<managedObject class="SUBRACK" version="1.0" distName="xxxx-xxxx/BSC-2222/xxx-102/xxx-1/xx-1" id="2222">
  <p name="locationName">000000-000</p>
  <p name="subrackSpecificType">xxxx</p>
  <p name="vendorName">xxxx</p>
  <p name="version">01</p>
</managedObject>
<managedObject class="UNIT" version="1.0" distName="x1-X2/XXX-111111/YYY-102/ZZ-1/AAAA-1/BBBB-CCC_2_3" id="55555">
  <list name="availabilityStatus">
    <p>Power On</p> 
  </list>
  <p name="identificationCode">9999A</p>
  <p name="operationalState">1</p>
  <p name="position">1</p>
  <p name="serialNumber">8888B</p>
  <p name="unitId">1</p>
  <p name="unitType">HHHH</p>
  <p name="vendorName">AAAA</p>
  <p name="version">333</p>
</managedObject>

I want an XPath to select all the p name along with their values.

I am able to get the other values using:

Similar to the last two fields, I want to get the rest of the elements using XPath:

How do I do this?

Upvotes: 0

Views: 13183

Answers (3)

marabu
marabu

Reputation: 1196

There are 2 locations where you specify an XPath expression in step Get-Data-From-XML:

  1. The Loop XPath (Content tab) is meant to derive rows from a document by returning a nodelist.
  2. The Field XPath (Fields tab) is meant to populate the fields of a row.

If your document contains multiple nodelists on different levels, it's best to aim for the deepest list. It's easy to access ancestor information using axes or the .. operator.

When a field XPath returns a nodelist, Kettle will always pick the first item instead of aborting.

Obviously you must use //p as your Loop XPath.

Upvotes: 1

Andersson
Andersson

Reputation: 52675

If you want to get required data from all p elements without specifying each name attribute value you can use

//list/following-sibling::p[@name]

or

//*[name()="list"]/following-sibling::*[name()="p" and @name]

If you want both name attribute value and text content of p:

//list/following-sibling::p[@name]/@name | //list/following-sibling::p[@name]

Upvotes: 2

Pankaj Kapare
Pankaj Kapare

Reputation: 7812

You can try following xpath to retrieve any "p" element in xml with specific "name" attribute no matter where its located in xml.

//p[@name='unitType']

Here // is the descendant-or-self. Just replace unitType string in above xpath with desired one. Output of above xpath selection will be

<p name="unitType">HHHH</p>

Upvotes: 0

Related Questions