'xmlstarlet output with quotes
I have a list of XPath queries modeled in an XML file as follows (querylist.xml) :
<queries>
<query id="1" project="docbook" xpath=".//@id|.//@xml:id"/>
<query id="2" project="docbook" xpath="descendant::h:span[@data-type='footnote']"/>
</queries>
And I am using xmlstarlet in makefile to list and run the xpath queries on files with the same project attribute name, using for example:
Update-Makefile content:
QUERYLIST0 = $(shell xmlstarlet sel -t -m "//query[@project='docbook']/@id" -v "concat(., ' ')" querylist.xml)
QUERYLIST1 = $(shell xmlstarlet sel -t -m "//query[@project='docbook']/@xpath" -v "concat(., ' ')" querylist.xml)
echoq:
@echo $(QUERYLIST0)
@echo $(QUERYLIST1)
But I am unable to list the xpath attribute queries whenever it has single quotes(ex: 'footnote'), How can I keep the attribute value as is, even when containing single quotes?
The output should be a list separated by a space:
.//@id|.//@xml:id descendant::h:span[@data-type='footnote']
Solution 1:[1]
I checked your command on the bash shell as well as in a bash script. It worked in both cases - with the slight modification of omitting the "shell" part after the $(...:
On command line:
xmlstarlet sel -T -t -m "//query[@project='docbook']/@xpath" -v "concat(.,' ')" querylist.xmldoes work.
And on the
bashshell the script#!/bin/bash RES=$(xmlstarlet sel -T -t -m "//query[@project='docbook']/@xpath" -v "concat(.,' ')" querylist.xml) echo $RESdoes work, too.
In both cases the result is
.//@id|.//@xml:id descendant::h:span[@data-type='footnote']
as desired.
Solution 2:[2]
<?xml version="1.0"?>
<queries>
<query id="1" project="docbook" xpath=".//@id|.//@xml:id"/>
<query id="2" project="docbook" xpath="descendant::h:span[@data-type='footnote']"/>
</queries>
$ xml sel -t -m queries/query[@project='docbook'] -v @xpath -i "number(count(preceding::query[@project='docbook']))+1<number(count(./../query[@project='docbook']))" -o " " -b file.xml
.//@id|.//@xml:id descendant::h:span[@data-type='footnote']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zx485 |
| Solution 2 | focog77269 |
