XPath expression help wanted

Gary McGill

Usenet Archive
Joined
Dec 3, 2004
Messages
7
Reaction score
0
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
 

Richard Tobin

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
Gary McGill said:
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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.
 

Tjerk Wolterink

Usenet Archive
Joined
Sep 18, 2004
Messages
218
Reaction score
0
Gary said:
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?
 

Gary McGill

Usenet Archive
Joined
Dec 3, 2004
Messages
7
Reaction score
0
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 said:
Gary said:
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?
 

Gary McGill

Usenet Archive
Joined
Dec 3, 2004
Messages
7
Reaction score
0
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

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
Gary McGill said:
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
 

William Park

Usenet Archive
Joined
Jul 5, 2002
Messages
213
Reaction score
0
Gary McGill said:
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
 

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
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top