fetch multiple attributes in xpath3

828 Views Asked by At

I have to fetch all attributes by name 'product-id' when the currency of that pricebook is 'GBP'. This expression is working fine when xpath is used. But when I use xpath3, it returns only the first matching value instead of all the values. All I need is an equivalent expression of xpath in xpath3

working xpath expression:

#[xpath:/pricebooks/pricebook[./header/currency ="GBP"]/price-tables/price-table/@product-id]

xpath expression I tried:

#[xpath3('/pricebooks/pricebook[./header/currency ="GBP"]/price-tables/price-table/@product-id',payload,'STRING')]

expected : [product1,product2,product4]

actual : product1

INPUT XML:

<pricebooks>
 <pricebook>
    <header pricebook-id="GB">
        <currency>GBP</currency>
        <display-name>name1</display-name>
    </header>
    <price-tables>
        <price-table product-id="product1">
            <amount quantity="1">24.0</amount>
        </price-table>
    </price-tables>
 </pricebook>
 <pricebook>
    <header pricebook-id="NZ">
        <currency>GBP</currency>
        <display-name>name2</display-name>
    </header>
    <price-tables>
        <price-table product-id="product2">
            <amount quantity="1">38.00003</amount>
        </price-table>
    </price-tables>
 </pricebook>
 <pricebook>
    <header pricebook-id="US">
        <currency>USD</currency>
        <display-name>name3</display-name>
    </header>
    <price-tables>
        <price-table product-id="A215ZZ003">
            <amount quantity="1">28.0</amount>
        </price-table>
    </price-tables>
 </pricebook>
 <pricebook>
    <header pricebook-id="AU">
        <currency>GBP</currency>
        <display-name>name4</display-name>
    </header>
    <price-tables>
        <price-table product-id="product4">
            <amount quantity="1">30.0</amount>
        </price-table>
    </price-tables>
  </pricebook>
</pricebooks>
2

There are 2 best solutions below

4
har07 On

Looks like the third parameter in your attempted use of xpath3() is wrong. Try using NODESET instead of STRING since you mean to return multiple nodes.

Quoted from the documentation :

  • BOOLEAN: Returns the effective boolean value of the expression as a java.lang.String. Equivalent to wrapping the expression in a call of the XPath boolean() function.

  • STRING: Returns the result of the expression converted to a string, as a java.lang.String. Equivalent to wrapping the expression in a call to the XPath string() function.

  • NUMBER: Returns the result of the expression converted to a double as a java.lang.Double. Equivalent to wrapping the expression in a call of the XPath number() function.

  • NODE: Returns the result as a node object.

  • NODESET: Returns a DOM NodeList object.

1
Wael M Elmahask On

maybe this helps,

/pricebooks/pricebook/price-tables/price-table[contains(@product-id,'product')]/@product-id