Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Selecting a set of nodes

Reply
Thread Tools

Selecting a set of nodes

 
 
jdhcards@gmail.com
Guest
Posts: n/a
 
      10-13-2006
Hello,

I've been banging my head against a problem all day without a solution,
and I'm hoping you all can help. I've got a piece of XML that defines a
set of elements in a flat list. Each of these elements has an attribute
"Id" which has a unique value. Some of these elements have a
parent-child relationship, specified with a "ResultId" attribute in one
of the child nodes.

<element id=1>
<child resultid=2/>
<child resultid=3/>
</element>
<element id=2>
</element>
<element id=3>
</element>
<element id=4>

I'm trying to filter the list of elements down to only the parent
elements. My approach has been to create a list of "child ids" and
throw out an element that have an id within that set of "child ids". In
the example above, I'd keep element 1&4, but through away 2 & 3.

I'm having trouble comparing the two node sets. Can anyone offer an
approach or the XSL syntax I'll need to get my approach done?

XSL to select child id's:

<xsl:variable name="subJobResultId"
select="ResultInfo/TaskDetails/*/TaskList/Task/TaskInformation/Task[@RunJobIdResultId
!= '']"/>

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      10-13-2006
> <element id=1>
> <child resultid=2/>
> <child resultid=3/>
> </element>

....etcetera.

That isn't an XML document. Attribute values must be quoted, and there
must be a single root element. But we'll assume your actual data is
better formed, for the sake of argument.

> XSL to select child id's:
> <xsl:variable name="subJobResultId"
> select="ResultInfo/TaskDetails/*/TaskList/Task
> /TaskInformation/Task[@RunJobIdResultId!= '']"/>


That really doesn't help us; it doesn't correspond to your sketch
and it introduces a whole bunch of complexities which would tend to
obscure the answer. So I'm not going to make any attempt to write
something that would work against your real data. I'll discuss it in
terms of the sketch, and leave it for you to apply appropriately.

> I'm trying to filter the list of elements down to only the parent
> elements.


And you aren't willing to take the obvious test that only parents will
contain a <child>? "pathToElements/element[child]" would do that quickly
and easily, where pathToElements is whatever path is needed to find the
<element>s.

If you insist on searching for matching values: What you want is those
<element>s which have a child/@resultid whose value matches the @id of
some other <element>. That's simple enough:

pathToElements/element[child/@resultid = ../element/@id]

I'm assuming all <element>s are children of the same parent node; if
not, you may need to replace .. with something more complicated.

This works because testing equality between nodesets returns true if any
node in the first set has the same value as any node in the second set.
(Good example of how to make XPath/XSLT do all the work for you,
actually; no explicit loops, just a flat statement of what you're
looking for.)


By the way:

1) Using examples with names like <element> and <child> makes discussing
this much more difficult than it should be, since we have to distinguish
between the element named element and the logical concept of an element.
PLEASE avoid doing so in the future.

2) If your task permits it, you might want to consider getting rid of
this whole ID/IDREF linkage design and just use XML's own parent/child
hierarchy, nesting elements within each other rather than doing lookups
by name. Of course that isn't always appropriate; sometimes data is
linked multiple ways or there are other constraints that force this
approach... but I've lost count of how many cases I've seen where a
simple change in data structure tremendously simplified the whole
application.

3) If you're using DTDs or schemas, I presume you're declaring id (or
its equivalent) as being of type ID, and resultid (or its equivalent) as
being of type IDREF. That will tell the validator to check that no two
ids have the same value, and that all resultids have a value that
corresponds to one of the ids -- ie, that a reliable many-to-one
relationship exists.

(If you're using schemas, there are fancier alternatives to ID/IDREF, of
course. "But the idea's the important thing." -- Tom Lehrer)

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
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
Selecting Nodes Using Subtotal of Child Nodes renfrochris@hotmail.com XML 1 08-24-2006 12:30 AM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
Looking A Nodes From Within Nodes Johnny Ooi XML 10 11-14-2004 06:55 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57