473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with XML/XPATH

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

Nov 12 '05 #1
3 1257
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
"baggy" <gb******@hotmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro 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

Nov 12 '05 #2
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/
Nov 12 '05 #3
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...

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MegaZone | last post by:
I'm having some issues with PHP DOMXML - in particular the get_elements_by_tagname method. Now, the PGP docs on this are, well, sparse, so maybe I'm just doing something stupid. I thought this...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
3
by: Thierry Lam | last post by:
Let's say I have the following xml tag: <para role="success">1</para> I can't figure out what kind of python xml.dom codes I should invoke to read the data 1? Any help please? Thanks...
0
by: Rajesh Jain | last post by:
I Have 2 separate schemas. --------------Schema 1 is defined as below----------- <xs:schema targetNamespace="http://Schemas/1" xmlns="http://Schemas/1" xmlns:xs="http://www.w3.org/2001/XMLSchema"...
4
by: Iain A. Mcleod | last post by:
Hi I'm stuck with the following schema validation problem in VS.NET 2003: I have two types of xml document and related schema: project and projectCollection. A projectcollection is just a set...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
by: John Lee | last post by:
Hi, I have a document <root> <name>John</name> </root> and the xpath "//name" will return correct node by using xmldoc.SelectSingleNode(xpath) and if the document contains a default...
7
by: Peter Morris [Droopy eyes software] | last post by:
Hi all I need a little help with XSD, here is my XML <data> <brandData> <brandGroups> <brandGroup> <name>Group1</name> </brandGroup>
1
by: Showjumper | last post by:
Hi, I need some help with xpath. I am very unfamilair with XPATH. I have an xml file with the folloing structure: <Photos> <Photo> <Description>This is a photo of horse that colicked. The bowel...
2
by: Monty | last post by:
Despite reading posts in Google, I don't understand XPATH. Can someone help me write an XPATH. From Google I think my problem is that the default namespace does not have a prefix. I can't change...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.