[e-lang] XML library design: what is XPath self? Or, is this "fragment" idea a good one?
Kevin Reid
kpreid at mac.com
Tue Feb 16 18:31:44 PST 2010
Running into a bit of a problem with the XML library design.
My starting premise has been that XML values are not single elements/
spans of text/etc (like DOM nodes) but rather arbitrary sequences of
XML data; that is, they can contain arbitrary concatenations:
? xml`<a>xxx</a> yyy <b>zzz</b>`[xpath`a/text()`]
# value: [xml`xxx`]
My notion was that this is more the right thing when one is working
with constructing an XML tree in code; being able to carry arbitrary
fragments around as single objects means that you can do natural
things such as xml`this <html:em>simple</html:em> example` without
contortions such as the DOM DocumentFragment objects.
So, for XPath semantics, the above sample implies that the “a” element
is a child of the context node.
But what about xml`<a b="c">xxx</a>`?
Given that one *has* an object which is a single element, one would
imagine it to be natural to want to refer to its attributes directly:
? xml`<a b="c">xxx</a>`[xpath`@@b`]
# value: [xmlattr`b="c"`]
But this is inconsistent with the previous example, because the XPath
context node must be the “a” element for this to work! Given this
model, this must be instead written as:
? xml`<a b="c">xxx</a>`[xpath`a/@@b`]
# value: [xmlattr`b="c"`]
or
? xml`<a b="c">xxx</a>`[xpath`*/@@b`]
# value: [xmlattr`b="c"`]
I think this is reasonably the Right Thing given my original premise,
but I'm wondering whether I should reconsider my original premise.
What do you think?
--
Kevin Reid <http://switchb.org/kpreid/>
More information about the e-lang
mailing list