How to point to an element using context-dependent information?

A

Arndt Jonasson

Say I have an XML document, where I want the contents of certain
elements
to be a textual pointer to another element, based on the contents
of yet another element. Somewhat silly example:

<top>
<friends>
<friend>
<name1>Joe</name1>
<name2>Anderson</name2>
</friend>
<friend>
<name1>Paul</name1>
<name2>Bright</name2>
</friend>
</friends>
<addresses>
<address>
<name>Joe</name>
<moredata>/top/friends/friend[name1=$./../name]</moredata>
<street>Wilson St.</street>
<number>15</number>
</address>
</addresses>
</top>

The string "/top/friends/friend[name1=$./../name]" could be what
I want, except that the "$." thing is an invention. The contents
of /top/friends/addresses/address[name="Joe"]/moredata is
meant to be an XPath expression pointing out the node
/top/friends/friend[name1="Joe"], based on the fact that when looking
at /top/friends/addresses/address[name="Joe"]/moredata, "../name" has
the contents "Joe".

I could invent a syntax (like $. above), but I'd like to use existing
syntax if there is any. I'm not familiar enough with XPointer or
XQuery
to know whether they can express this. Can they? I don't see that
XPath alone is enough. Thanks in advance for any suggestions.
 
P

Pavel Lepin

Arndt Jonasson said:
Say I have an XML document, where I want the contents of
certain elements
to be a textual pointer to another element, based on the
contents of yet another element. Somewhat silly example:

[snip]
The string "/top/friends/friend[name1=$./../name]" could
be what I want, except that the "$." thing is an
invention.

XSLT defines several extensions to XPath, including
current() function. Taking a look at XPointer/XLink/XQuery
is probably a much better idea, though.

However. Are you certain you want to do something like that
at all? That is, do you want to allow pointers to anything
in the document (and perhaps *beyond* the document, if you
allow document() or somesuch as well)? Perhaps it would be
a better idea to define a more structured grammar
for 'links', and let your application logic handle the
actual processing.

For example:

<moredata><friends/></moredata>

Upon seeing that, your applications would know they should
look up the corresponding friends. If you needed to link
something else as well, you would define more elements and
the logic for processing them as well:

<moredata><friends/><borks foobarcity="42"/></moredata>

(Supposedly linking all the Joe's borks with foobarcity of
42 or more... whatever that means.)
 
P

Peter Flynn

Arndt said:
Say I have an XML document, where I want the contents of certain
elements
to be a textual pointer to another element, based on the contents
of yet another element. Somewhat silly example:

<top>
<friends>
<friend>
<name1>Joe</name1>
<name2>Anderson</name2>
</friend>
<friend>
<name1>Paul</name1>
<name2>Bright</name2>
</friend>
</friends>
<addresses>
<address>
<name>Joe</name>
<moredata>/top/friends/friend[name1=$./../name]</moredata>
<street>Wilson St.</street>
<number>15</number>
</address>
</addresses>
</top>

The string "/top/friends/friend[name1=$./../name]" could be what
I want, except that the "$." thing is an invention.

If you only want links within the document, just us ID and IDREF
attribute types.

<top>
<friends>
<friend ID="JA1">
<name1>Joe</name1>
<name2>Anderson</name2>
</friend>
<friend>
<name1>Paul</name1>
<name2>Bright</name2>
</friend>
</friends>
<addresses>
<address>
<name>Joe</name>
<moredata see="JA1"/>
<street>Wilson St.</street>
<number>15</number>
</address>
</addresses>
</top>

But there are other, probably much more efficient ways to do this.

///Peter
 

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
474,008
Messages
2,570,269
Members
46,871
Latest member
Stephendes

Latest Threads

Top