Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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


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


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
up vote 4 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


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.