Selecting a set of nodes

Discussion in 'XML' started by jdhcards@gmail.com, Oct 13, 2006.

  1. Guest

    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
    != '']"/>
     
    , Oct 13, 2006
    #1
    1. Advertising

  2. > <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
     
    Joe Kesselman, Oct 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Wulveryck
    Replies:
    1
    Views:
    689
    Andrew Thompson
    Sep 14, 2004
  2. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    419
    Patrick TJ McPhee
    Jun 17, 2004
  3. Tjerk Wolterink
    Replies:
    9
    Views:
    1,270
    Joris Gillis
    Feb 7, 2005
  4. Ryan  Adler
    Replies:
    2
    Views:
    397
  5. Replies:
    1
    Views:
    590
    Soren Kuula
    Aug 24, 2006
Loading...

Share This Page