Home > other >  Is it possible to combine two attribute value with one xpath query?
Is it possible to combine two attribute value with one xpath query?

Time:09-16

Sample xml

<Resources>
    <Resource>
        <UID>14</UID>
        <Name>Entertainment</Name>
        <ID>2</ID>
    </Resource>
    <Resource>
        <UID>12</UID>
        <Name>Movie</Name>
        <ID>1</ID>
    </Resource>
</Resources>

Expected Output:

14,Entertainment
12,Movie

CodePudding user response:

In XQuery:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'text';
declare option output:item-separator '&#10;';

/Resources/Resource/string-join((UID, Name), ',')

CodePudding user response:

It can be done on a single Xpath 1.0 expression

concat(concat(//Resource[1]/UID/text(), ",", //Resource[1]/Name/text()), " ", concat(//Resource[2]/UID/text(), ",", //Resource[2]/Name/text()))

Which is of the form

 concat(concat(elem1, "," elem2), " ",concat(elem3, "," elem4))

Testing on command line

 xmllint --xpath 'concat(concat(//Resource[1]/UID/text(), ",", //Resource[1]/Name/text()), " ", concat(//Resource[2]/UID/text(), ",", //Resource[2]/Name/text()))' tmp.xml ; echo

Result:

14,Entertainment 12,Movie 
  • Related