[jdom-interest] ID and IDREF

Galluzzo, Eric EGalluzzo at synchrony.net
Wed Sep 6 06:48:22 PDT 2000


> -----Original Message-----
> From: Brett McLaughlin [mailto:brett.mclaughlin at lutris.com]
> 
> veerpal singh wrote:
> > 
> > Hi All
> > 
> > I was just looking around on net for ID and IDREF 
> processing. Is it possible thru jdom that when idref is 
> encountered, element having iD can be retrieved. Or has to be 
> done by higher level appl.
> 
> What do you need to be able to do, and what would you expect 
> the API to
> look like? Finally, what's your use-case for these changes.
> 
> Let us know, and I'll look at what it would take to do.

I could actually use this too. :)  We're modeling contacts with XML.  Many
contacts can share a person and/or an organization, so we have something
inside the contact that references (via an IDREF) a person and an
organization (each identified via an ID), thus:

    ...

    <contacts>
      <contact>
        <person-ref person="p1"/>
        <org-ref org="o1"/>
        ...
      </contact>
      ...
    </contacts>

    <people>
      <person id="p1">
        ...
      </person>
      ...
    </people>

    <orgs>
      <org id="o1">
        ...
      </org>
      ...
    </orgs>

    ...

It would be very handy to go something like:

    Document doc         = ...;
    Element  contactElem = ...;
    String   personID    =
        contactElem.getChild ("person-ref").getAttribute ("ref").getValue();
    Element  personElem  = doc.getElementFromID (personID);

Of course, if we had the XPath stuff, I could just go something like:

    Element personElem =
        doc.resolveQuery ("id(//contact[1]/person-ref/@person)");

but that's somewhat of a pipe dream. :)

It would make sense to me to put the getElementFromID() method on Document,
since the Document is the only thing that could know about all the elements
within a document.  Also, it contains the Doctype by which one can tell
whether an element is an ID or not.

One problem I can foresee is that maintaining some sort of ID -> element map
while the user is constructing a document will be really quite painful, and
perhaps even impossible.  For example, given:

    <!ELEMENT root (foo|bar)*>

    <!ELEMENT foo EMPTY>
    <!ATTLIST foo
        id  ID    #REQUIRED
    >

    <!ELEMENT bar EMPTY>
    <!ATTLIST bar
        foo IDREF #REQUIRED
    >

then:

    Document doc  = new Document();
    Element  root = new Element ("root");
    root.addContent (new Element ("foo").addAttribute ("id", "id0"));
    root.addContent (new Element ("bar").addAttribute ("foo", "id0"));
    doc.setRootElement (root);

would have to update the ID -> element map on the last statement, which
could be slow if root actually had lots more elements in it.  Likewise,
moving children about between documents and so on would become much trickier
for JDOM to implement.

So I would say the only feasible way to implement such a method would be to
do it "on the fly" -- in other words, to search through all attributes of
all elements until one is found with type "ID" and with the correct value.
But that's bound to be really, really slow.  So perhaps it's not even
feasible at all....

Anyway, if you've actually read this far, sorry for rambling on! :)

    - Eric



More information about the jdom-interest mailing list