xml - XPath to concatenate parent and child attribute values in one query -


given:

<?xml version="1.0" encoding="utf-8"?> <root> <report id="0" > <property name="comments">comment</property> <property name="test">sdcs</property> <property name="test">csd</property> <property name="eventhandlerclass">sdcs</property> </report> <report  id="1"> <property name="comments">comment</property> <property name="test">dcs</property> <property name="test">gds</property> <property name="test">jds</property> <property name="eventhandlerclass">sdcs</property> </report> </root> 

expected output:

id=0, test=sdcs id=0, test=csd id=1, test=dcs id=1, test=gds id=1, test=jds 

only looking plain xpath 1.0 achieve this. tried varioud functions , predicates fail output above. essentially, after reading should able tell "test" belongs report id.

tried:

/root/report/@id | /root/report/property[@name="test"] -- gives result in separate lines

/root/report/ ( @id | property[@name="test"]) -- gives result in separate lines

concat( /root/report/@id, /root/report/property[@name="test"]) -- gives error (think concat works on single line result)

string-join(/root/report/@id | /root/report/property[@name="test"] , ',') -- comes single line, 2 report ids.

you can use for keep id in variable:

for $report in /root/report  return $property in $report/property[@name="test"] return concat("id=", $report/@id, ", ", $property/@name, "=", $property) 

or shorter:

for $report in /root/report return $report/property[@name="test"]/ concat("id=", $report/@id, ", ", @name, "=", .) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -