Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using xmllint to parse xml file which has several tags and each tag has several attributes. Sample structure as shown below:

<root>
   <child attr1="abc" attr2="def" attr3="ghi" />
   ...
   ...
</root>

I need to fetch the values from the attributes attr1,attr2 and attr3.

So far, I have tried the following which is giving the data of one attribute perfectly

echo 'cat //root/child/@attr1' | xmllint --shell data.xml 

This output

attr1="abc"

So, my question is, how can we specify more than one attributes in the string to get the required output as

attr1="abc"
attr2="def"
attr3="ghi"

I tried the following for this, but no good result:

echo 'cat //root/child/@*[attr1|attr2|attr3]' | xmllint --shell data.xml 
echo 'cat //root/child/@*[attr1 or attr2 or attr3]' | xmllint --shell data.xml 

output for above was that the echo statement was re-echoed again that means, the xmllint didn't accept it as input.

Any ideas on how to go about this?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

As far as I know the | separator can be used only on entire paths:

echo 'cat /root/child/@attr1|/root/child/@attr2|/root/child/@attr3' | xmllint --shell data.xml

(As // means at any depth, “//root” puts the parser to pointless extra work. Assuming your sample XML looks has similar structure as the real one (so root is indeed the XML's root node), better use “/root/child”.)

Or you can use an expression with XPath functions:

echo 'cat /root/child/@*[name()="attr1" or name()="attr2" or name()="attr3"]' | xmllint --shell data.xml

If you need all attributes with “attr*” name, you can use a generic expression:

echo 'cat /root/child/@*[starts-with(name(),"attr")]' | xmllint --shell data.xml
share|improve this answer
    
Pefect answer :) –  mtk Oct 24 '12 at 13:34

grep -o attr[0-9]...... file.ext

quick and dirty...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.