arregold
arregold

Reputation: 3

Parsing XPath Query

I would like to receive the output:

10.62.197.125, 10.62.197.126, 10.62.197.127.

How do I make that happen? (Note that the code has been somehow manipulated due to customer restrictions).

<output arrayType="ns2:aaa[20]" type="ns2:aaa_array">
  <item type="ns2:bbb">
    <ip_addr type="xsd:string">0a2ef556</ip_addr>
    <hostaddr type="xsd:string">10.62.197.125</hostaddr>
    <subnet_name type="xsd:string">Test</subnet_name>
    <pool_id type="xsd:string">0</pool_id>
  </item>
  <item type="ns2:aaa">
    <ip_addr type="xsd:string">0a3c3c33</ip_addr>
    <hostaddr type="xsd:string">10.62.197.126</hostaddr>
    <subnet_name type="xsd:string">Test</subnet_name>
    <pool_id type="xsd:string">0</pool_id>
  </item>
  <item type="ns2:aaa">
    <ip_addr type="xsd:string">0a3a72c4</ip_addr>
    <hostaddr type="xsd:string">10.62.197.127</hostaddr>
    <subnet_name type="xsd:string">Test</subnet_name>
    <pool_id type="xsd:string">0</pool_id>
  </item>
</output>

Upvotes: 0

Views: 34

Answers (1)

Dennis Holmer
Dennis Holmer

Reputation: 134

To get the elements:
/*/item/hostaddr/text()

results in

10.62.197.125
10.62.197.126
10.62.197.127

To get a string (supported by XPath 2.0+):
string-join(/*/item/hostaddr/text(), ', ')

results in

10.62.197.125, 10.62.197.126, 10.62.197.127

This means:
Select the text in the hostaddr element in all item elements.

Test:
You can test this by going to a XPath tester, like this, paste your XML in Option 1, paste the expression in XPath expression field and press the test XPath button.

Upvotes: 1

Related Questions