2

my xml looks like

<list1>
   <list2 id="abc" value="yes"></list2>
   <list2 id="xyz" value="no"></list2>
   <list2 id="def" value="yes"></list2>
<list1>

using xmllint but I am not able to get the right output. When I query the above xml giving "abc", I need "yes" returned.

  • What XPath query are you using? – larsks Jul 31 '15 at 0:09
  • now I am trying to use xmllint //list1/list2[@id="abc"]/@value' | xmllint --shell myxml | awk -F'[="]' '!/>/{print $(NF-1)}' when I run on console it works but unable to capture it to a var. – kumar2008 Jul 31 '15 at 0:30
1

You can use the XPath string() function to get just the value of the value attribute:

$ xmllint --xpath 'string(//list1/list2[@id="abc"]/@value)' data.xml
yes

You would capture this to a variable using typical bash syntax:

value=$(
  xmllint --xpath \
  'string(//list1/list2[@id="abc"]/@value)' data.xml
)

And then:

echo $value

Results in:

yes
  • If your xmllint doesn't have the xpath option, use whatever syntax you have been using for doing XPath queries. This works on all of the systems to which I have access, but you may have an older version of xmllint. – larsks Jul 31 '15 at 1:53
  • echo 'cat //list1/list2[@id="abc"]/@value' | xmllint --shell myxml | awk -F'[="]' '!/>/{print $(NF-1)}' works fine from console but fails when used in the script. when executed the script it gives: cat //list1/list2[@id="abc"]/@value No such file or directory – kumar2008 Jul 31 '15 at 1:56
  • @kumar2008 While I agree that you should continue whatever methods you have been using, I urge you to try to upgrade xmllint, usually part of your distribution's libxml2 package. Using sed and awk are your next choice, but the Xpaths get tougher and harder to debug. – eyoung100 Jul 31 '15 at 2:57
  • eyoung, as we don't have xmllint. any option of using sed? any help on that command would be appreciated. – kumar2008 Aug 5 '15 at 20:56
1
  1. Your xml is in incorrect format. You have a premature end of data in tag list1. Should have </list1> at the end.
  2. As @eyoung100 suggested, please upgrade your xmllint.
  3. echo 'cat //list1/list2[@id="abc"]/@value' | xmllint --shell myxml | awk -F'[="]' '!/>/{print $(NF-1)}' will work if you correct the xml as mentioned in point 1 & make sure you have the myxml file in your current working directory(or use absolute paths), though it's not the right way of doing things.

Code:

myvar='abc'
myxml='myxml'
myvalue="cat //list1/list2[@id=\"'$myvar'\"]/@value | xmllint --shell $myxml"
sh -c "echo $myvalue | gawk -F'[=\"]' '!/>/{print \$(NF-1)}'"

Is this what you are trying to do?

  • Whoot!!!! Someone finally agrees with me :) – eyoung100 Jul 31 '15 at 4:14
  • sorry my bad. xml in file is correct format but while pasting missed it. I have challenge to get the value into variable. – kumar2008 Jul 31 '15 at 4:33
  • Neuron, it is working from command prompt as expected. When I include this in the script. in the script Instead of "abc" hardcoded the value comes as variable ($myvar), instead of echo I have to save the output of @value to my value variable and myxml comes as variable $myxml aswell. I am using below command in script: myvalue= cat //list1/list2[@id="'$myvar'"]/@value | xmllint --shell $myxml | awk -F'[="]' '!/>/{print $(NF-1)}' failing with error: cat: //list1/list2[@id="$myvar"]/@value: No such file or directory – kumar2008 Jul 31 '15 at 4:44
  • @kumar2008: please see the edited post. I have pasted the script there – neuron Jul 31 '15 at 5:47
  • Hi Neuron and others, The above xmllint doesn't execute from AIX. It works fine with Linux. Any help? any command using sed? – kumar2008 Aug 5 '15 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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