473,396 Members | 1,702 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,396 software developers and data experts.

XPath expression help wanted

Suppose I have an xml file that looks like this:

<example>
<foo id="1">
<bar id="a1"/>
<bar id="b1"/>
...
</foo>
<foo id="2">
<bar id="a2"/>
<bar id="b2"/>
...
</foo>
...
</example>

In other words, there are multiple foo elements, each of which contains
multiple bar elements.

Now suppose I want to produce a list of all the bar ids. I can do this with
the following XPath expression:

/example/foo/bar/@id

This will give me:

a1
b1
...
a2
b2
...
etc.

Now for my question: how do I get a list of the foo ids that matches the
list of bar ids? That is, rather than having each foo id listed once, I want
it listed as many times as there are corresponding bars.

If I use an XPath expression similar to the one above:

/example/foo/@id

....then I just get each foo id listed once:

1
2
etc.

....whereas what I want is:

1
1
...
2
2
...
etc.

I want to do this so that when I display the lists side-by-side, they
correspond, as in:

1 a1
1 b1
...
2 a2
2 b2
...
etc.

I tried using the following expression:

/example/foo/bar/../@id

....in the hope that referencing the bar element would mean that I got as
many output nodes as there are bars, but that didn't work - it still only
gave me one of each foo id.

Is there a way to do what I'm trying to do?

Thanks,
Gary
Oct 26 '05 #1
7 1202
In article <11*************@corp.supernews.com>,
Gary McGill <ga*********@electrum.co.uk> wrote:
I tried using the following expression:

/example/foo/bar/../@id

...in the hope that referencing the bar element would mean that I got as
many output nodes as there are bars, but that didn't work - it still only
gave me one of each foo id.


XPath node sets are just that - sets - so you can't get a set that
contains a node more than once.

Things are different in XPath 2, if that's relevant.

-- Richard
Oct 26 '05 #2


Gary McGill wrote:

Now for my question: how do I get a list of the foo ids that matches the
list of bar ids? That is, rather than having each foo id listed once, I want
it listed as many times as there are corresponding bars.


But XPath simply allows you to select existing nodes in the input XML,
if you want to reorder or group them then you need XSLT for instance.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 26 '05 #3
Gary McGill wrote:
Suppose I have an xml file that looks like this:

<example>
<foo id="1">
<bar id="a1"/>
<bar id="b1"/>
...
</foo>
<foo id="2">
<bar id="a2"/>
<bar id="b2"/>
...
</foo>
...
</example>

[cut]


Just a side note, but your xml contains redundant information,
i would do it this way:

<foo id="1">
<bar id="a"/>
<bar id="b"/>
...
</foo>
<foo id="2">
<bar id="a"/>
<bar id="b"/>
...
</foo>

We now that the first bar with id=a has a parent element foo with
id=1, so it is redundant to say that it has id=a1

understand?
Oct 26 '05 #4
Thanks, but my xml file was an example for the purposes of asking my
question. I don't really have elements called foo and bar :-)
"Tjerk Wolterink" <tj***@wolterinkwebdesign.com> wrote in message
news:dj**********@netlx020.civ.utwente.nl...
Gary McGill wrote:
Suppose I have an xml file that looks like this:

<example>
<foo id="1">
<bar id="a1"/>
<bar id="b1"/>
...
</foo>
<foo id="2">
<bar id="a2"/>
<bar id="b2"/>
...
</foo>
...
</example>
[cut]


Just a side note, but your xml contains redundant information,
i would do it this way:

<foo id="1">
<bar id="a"/>
<bar id="b"/>
...
</foo>
<foo id="2">
<bar id="a"/>
<bar id="b"/>
...
</foo>

We now that the first bar with id=a has a parent element foo with id=1,
so it is redundant to say that it has id=a1

understand?

Oct 26 '05 #5
Oh sh!t. That's bad news, and all the worse because it makes perfect sense
:-)

Can I then cheat the system by returning a set of calculated values instead
of nodes in the document, where the values are calculated from the bar nodes
but include the id from the foo nodes?

Sort of like this (although I know this syntax is nonsense):

/example/foo/bar/(@id + ../@id)
"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:dj***********@pc-news.cogsci.ed.ac.uk...
In article <11*************@corp.supernews.com>,
Gary McGill <ga*********@electrum.co.uk> wrote:
I tried using the following expression:

/example/foo/bar/../@id

...in the hope that referencing the bar element would mean that I got as
many output nodes as there are bars, but that didn't work - it still only
gave me one of each foo id.


XPath node sets are just that - sets - so you can't get a set that
contains a node more than once.

Things are different in XPath 2, if that's relevant.

-- Richard

Oct 26 '05 #6
In article <11*************@corp.supernews.com>,
Gary McGill <ga*********@electrum.co.uk> wrote:
Can I then cheat the system by returning a set of calculated values instead
of nodes in the document, where the values are calculated from the bar nodes
but include the id from the foo nodes?

Sort of like this (although I know this syntax is nonsense):

/example/foo/bar/(@id + ../@id)


No, because XPath can only return nodes that exist in the source document,
not construct new ones out of their bits.

You're going to have to use something layered on top of XPath, that
provides some additional structuring or control mechanism, like XSLT
for example.

-- Richard
Oct 26 '05 #7
Gary McGill <ga*********@electrum.co.uk> wrote:
Suppose I have an xml file that looks like this:

<example>
<foo id="1">
<bar id="a1"/>
<bar id="b1"/>
...
</foo>
<foo id="2">
<bar id="a2"/>
<bar id="b2"/>
...
</foo>
...
</example> .... I want to do this so that when I display the lists side-by-side, they
correspond, as in:

1 a1
1 b1
...
2 a2
2 b2


You are printing out 'foo' id and 'bar' id, for each 'bar' elements, right?
Then,

start() # Usage: start tag att=value...
{
case $1 in
foo) declare "${@:2}"; foo=$id ;;
bar) declare "${@:2}"; bar=$id ;;
esac
}
end() # Usage: end tag
{
case $1.${XML_TAG_STACK[1]}.${XML_TAG_STACK[2]} in
bar.foo.example) echo "$foo $bar" ;;
esac
}
expat -s start -e end < file.xml

Ref:
http://home.eol.ca/~parkw/index.html#expat

--
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/
Oct 26 '05 #8

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

Similar topics

3
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
1
by: Michael H | last post by:
If I have xml as this: <sElements> <ses ct="0"> <se ... /> <se ... /> <se ... /> <se ... /> </ses> <ses ct="1">
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: bruce | last post by:
for guys with python/xpath expertise.. i'm playing with xpath.. and i'm trying to solve an issue... i have the following kind of situation where i'm trying to get certain data. i have a...
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
2
by: arunairs | last post by:
Hi, Is there a way of validating and XPath? In this case , I am accepting an XPath from the user and I need to validate the XPath syntax. Is there a regular expression available that anyone can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.