select items based on attribute value in a second, same-levelcollection

C

C Wilson

I may need to resort to some other tool but right now I'm limiting
myself to XPath and XSLT and suchlike. I'm an XPath newbie.
I'm framing my question as a structural one and what I think I'm
looking for is a structural answer, not exact source code.
I have a document that has several collections at the same level. The
two that concern me are entities and numeric snapshots. That is, the
document contains a set of entities, each of which has, say, an ID
and a color. At the same level there is a collection of clumps of
numeric values, a snapshot of the state of the entity; each clump has
the ID of the entity of which it is a snapshot. Let's say weight and
ring size.
<blather>
<entities>
<entity id="x" color="magenta">
</entity>
</entities>
<snapshots>
<snapshot eid="x">
<weight>23</weight>
</snapshot>
</snapshots>
<blather>
I have worked up an XSLT that computes the grand total of all the
weights. I can do this looking only at the collection of snapshots.
Now I need to exclude some snapshots based on the color of their
associated entities. My goal is to calculate the grand total of all
weights for entities that (say) are not blue.
I'm open to such ideas as preprocessing the document to add the color
as an attribute of the snapshot, or whatever goes with the grain of
this XML world.
chw
 
D

David Carlisle

C said:
I may need to resort to some other tool but right now I'm limiting
myself to XPath and XSLT and suchlike. I'm an XPath newbie.
I'm framing my question as a structural one and what I think I'm
looking for is a structural answer, not exact source code.
I have a document that has several collections at the same level. The
two that concern me are entities and numeric snapshots. That is, the
document contains a set of entities, each of which has, say, an ID
and a color. At the same level there is a collection of clumps of
numeric values, a snapshot of the state of the entity; each clump has
the ID of the entity of which it is a snapshot. Let's say weight and
ring size.
<blather>
<entities>
<entity id="x" color="magenta">
</entity>
</entities>
<snapshots>
<snapshot eid="x">
<weight>23</weight>
</snapshot>
</snapshots>
<blather>
I have worked up an XSLT that computes the grand total of all the
weights. I can do this looking only at the collection of snapshots.
Now I need to exclude some snapshots based on the color of their
associated entities. My goal is to calculate the grand total of all
weights for entities that (say) are not blue.
I'm open to such ideas as preprocessing the document to add the color
as an attribute of the snapshot, or whatever goes with the grain of
this XML world.
chw

index your entities

<xsl:key name="e" match="entity" use="@id"/>

then

My goal is to calculate the grand total of all
> weights for entities that (say) are not blue.

is

sum(snapshot[not(key('e',@eid)/@color='blue')]/weight)

If you have a DTD and @id is of type ID then you don't need xsl:key and
can instead do

sum(snapshot[not(id(@eid)/@color='blue')]/weight)

but key() is a bit safer avoids questions about whether the xml parser
being used does or does not read the DTD.

David
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top