[
Lists Home |
Date Index |
Thread Index
]
Elliotte Rusty Harold wrote,
> At 10:40 AM -0600 9/21/02, Aaron Skonnard wrote:
> > Yes, both MSXML and the .NET implementation have supported this
> > through the Inner/OuterXml properties.
>
> Now that you mention it, I have seen that. I had filed it away at the
> back of my brain as "horrid, ugly, non-standard extensions to
> JavaScript and DOM" and promptly forgotten it once I finished working
> at the company that was using it.
That's always been my reaction too.
But on reflection (and playing devils advocate a bit) maybe it's not as
unreasonable as it seems. It has quite a lot in common with printf-
style IO APIs, which despite being error-prone (and occasionally the
source of security vulnerabilities) have proved to be surprisingly
resistant to replacement by more structured, typesafe or object-
oriented APIs.
To illustrate, compare,
const char* s = "printf";
int i = 2;
puts(s);
puts(" is ");
putint(i); // pretend this is standard C
puts(" times more usable\n");
vs.
const char* s = "printf";
int i = 2;
printf("%s is %i times more usable\n", s, i);
with,
String s = "InnerXml";
int i = 2;
Element usability = new Element("usability");
usability.add(new Attribute("factor", Integer.toString(i)));
usability.appendChild(s);
vs.
String s = "InnerXml";
int i = 2;
parent.appendChild("<usability factor=\"+2+"\">"+s+"</usability>");
Why do these string-template approaches support such dramatically more
compact and readable code? I think it's simply because the syntax of
the template-strings provides a domain-specific "little language" which
is better tuned to the task at hand than the generic mechanisms
provided by the host language. I guess things like embedded SQL, for
all their ugliness, do much the same job.
So maybe the answer would be to come up with a template string syntax
that's more idiomatic for XML than printf style. How about something
XPath-like? For example,
String s = "XPath expansion";
int i = 2;
parent.appendChild("usability[@factor="+i+"]/text("+s+")");
or if we waited for J2SE 1.5 which'll have auto-boxing and concise array
literals, we could even have,
parent.appendChild("usability[@factor=%i]/text(%s)", {i, s});
Thoughts?
Cheers,
Miles
|