@
namespaceXML
object from a string:var order = new XML("<order/>");
XML
wrapper for a DOM object:var doc = new XML(document);
<order id="1234">
<customer><name>Joe Green</name>
<address><street>410 Main</street>
<city>York</city><state>VT</state>
</address>
</customer>
<item>. . .</item>
<item>
<make>Acme</make>
<model>Framistat</model>
<price>2.50</price>
<qty>30</qty>
</item>
</order>
var address = order.customer.address;
var second = order.item[1];
second.total = second.qty * second.price;
var order = <order><customer>. . .</customer>
<item><price>5</price><qty>10</qty></item>
<item level="rush">
<price>2.5</price>
<qty>30</qty>
</item>
<item level="rush">
<price>1.5</price>
<qty>50</qty>
</item>
</order>;
var items = order.item; // XMLList of item el'ts
var prices = order..price;
var urgentItems = order.item.(@level == "rush");
var itemAttrs = order.item[0].@*;
order.customer.address = <address><street>...</street></address>;
order.item += <item level={++urgency}/>
order.customer.name = <prefix>{prefix[i]}</prefix>
fully_dynamic = <{tagname}>{content}</{tagname}>;
tagname
must be a valid XML namenew QName(uri, localName);
var n1 = new Namespace([prefix, ]uri1);
x = <t xmlns:p="uri1">
<p:u>42</u>
<p:not-an-identifier id="99"/>
<t>;
y = x.n1::u; // y == 42
i = x.n1::["not-an-identifier"].@id;
Oddments, bugs, and fun stuff
- ECMA-357 does not throw ReferenceError for undefined XML names
*
=> undefined
@oops
=> undefined
@*::*
=> undefined
- What were they thinking?
- May have been motivated by filtering predicate operator
- applied to inhomogeneous data
- but you can use
..
to homogenize
- Fixed in ISO version of spec
Oddments, bugs, and fun stuff (2)
- Expandos make markup composition a snap!
- Just start appending extra property tiers:
var html = <html/>;
html.head.title = "My Page Title";
html.body.@bgcolor = "#e4e4e4";
html.body.form.@name = "myform";
html.body.form.@action = "someurl.jss";
html.body.form.@method = "post";
html.body.form.@onclick = "return somejs();";
html.body.form.input[0] = "";
html.body.form.input[0].@name = "test";
Oddments, bugs, and fun stuff (3)
- Results in this XML:
<html>
<head>
<title>My Page Title</title>
</head>
<body bgcolor="#e4e4e4">
<form name="myform" action="someurl.jss"
method="post" onclick="return somejs();">
<input name="test"></input>
</form>
</body>
</html>
Future work
- Disambiguate identifiers in filtering predicates and
with
- Generalize filtering predicate and descendant operators to other types
- Namespaces and QNames also generalized in Edition 4 (JavaScript 2)
- XML methods are hidden in a special namespace
- You can't extract
m = x.toXMLString;
and call m later
- SpiderMonkey adds
m = x.function::toXMLString
for this
- Should JS have a list type and generators?