Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Return values based on dynamic expression in XmlStarlet

$
0
0

I want to get the text element based on the values given in "part1".

In below example XML, I want Text2 and Text5 to be returned, because the values in part1 refer to 2 and 5.

How can this be done using XmlStarlet, preferably under Windows.

I started with:

xml sel -t -m //part1/line -v @val -o "," -c ../../part2/line[@val='3']/text -n example.xml

giving:

2,<text>Text 3</text>5,<text>Text 3</text>

Which is, of course not correct because I do want want the fixed 3, but that should be dynamic, like:

xml sel -t -m //part1/line -v @val -o "," -c concat('../../part2/line[@val=',@val,']/text') -n example.xml

which returns, the Xml-path, and not the value which I was expecting:

2,../../part2/line[@val=2]/text5,../../part2/line[@val=5]/text

I am expecting the next text to be returned.

2,<text>Text 2</text>5,<text>Text 5</text>

(The <text> and </text> are not really needed here...)

Can the be done using XmlStarlet (preferably under Windows)?

The example.xml is:

<root><part1><line val="2"></line><line val="5"></line></part1><part2><line val="1"><text>Text 1</text></line><line val="2"><text>Text 2</text></line><line val="3"><text>Text 3</text></line><line val="4"><text>Text 4</text></line><line val="5"><text>Text 5</text></line><line val="6"><text>Text 6</text></line></part2></root>

EDIT: As always, on gets an idea after posting the question

I would appreciate an easier solution than (which produces the correct results!):

for /f "usebackq tokens=*" %f in (`xml sel -t -m //part1/line -v @val -n example.xml`) do @xml sel -t -m //part2/line[@val=%f]/text -v "%f" -o "," -v . -n example.xml

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>