Help with XML/XPATH

B

baggy

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?

Thanks
 
D

Dan

First, I'm assuming your XML looks like this, since what you posted is nt
well-formed:
<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>

To get at the value, the xpath is something like:
/X1/Props/Prop[@Name='flag']/Value

The flage value can be input as a variable into the XPATH:
<xsl:variable name="input">flag</xsl:variable>
<xsl:value-of select="/X1/Props/Prop[@Name=$input]/Value"/>

dan
 
W

William Park

In said:
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 <[email protected]>, 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/
 
G

Gadrin

sure, but like they said clean up your 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>

using XSL, you can use something like:

<xsl:value-of select="//Prop[@Name='flag'" />

I don't know what the full XML looks like so...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top