In <comp.text.xml> baggy <gb******@hotmail.com> wrote:
Hi,
I have the following XML:-
<X1>
<Props>
<Prop xsi:type=\"text\" Name=\"N1\"><Value>TestUser</Value></Prop>
<Prop xsi:type=\"bool\" Name=\"flag\"><Value>0</Value></Prop>
<Prop xsi:type=\"int\" Name=\"numemp\"><Value>100</Value></Prop>
</Props>
</X1>
I want to extract just the value for a specific <Prop>, for instance I
want to be able to just retrieve the Value of the "flag" <Prop>. I am
thinking of looping through each node and testing for the Name
attribute but that seems a little complicated. Is there a way to do it
using XPATH?
Any help?
First, fix the quoting of double-quotes. Once you have
<X1>
<Props>
<Prop xsi:type="text" Name="N1"><Value>TestUser</Value></Prop>
<Prop xsi:type="bool" Name="flag"><Value>0</Value></Prop>
<Prop xsi:type="int" Name="numemp"><Value>100</Value></Prop>
</Props>
</X1>
then feed it through Bash shell with Expat interface. Something like
start() # Usage: start tag type=bool Name=flag ...
{
case $1 in
Value) unset value ;;
esac
}
data() # Usage: data text
{
case ${XML_ELEMENT_STACK[1]} in
Value) strcat value "$1" ;;
esac
}
end() # Usage: end tag
{
case $1 in
Value) [ "${XML_ELEMENT_STACK|=Name=flag}" ] && echo "Value=$value" ;;
easc
}
expat -s start -d data -e end < file.xml
--
William Park <op**********@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/