Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help with XML/XPATH

Reply
Thread Tools

Help with XML/XPATH

 
 
baggy
Guest
Posts: n/a
 
      07-14-2005
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

 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      07-14-2005
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


"baggy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      07-14-2005
In <comp.text.xml> baggy <(E-Mail Removed)> 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 <(E-Mail Removed)>, 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/
 
Reply With Quote
 
Gadrin@gmail.com
Guest
Posts: n/a
 
      07-16-2005
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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments