[ascoders] ECMA Approves ECMAScript for XML (E4X)
- From: Ralf Siegel <ralf@xxxxxxxxxxx>
- To: ascoders@xxxxxxxxxxxxx
- Date: Sun, 22 Aug 2004 23:36:04 +0200
Bin erst heute über den Blog von Peter Hall darüber gestoplert:
http://www.markme.com/mchotin/archives/005614.cfm
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-357.pdf
Wer das PDF noch nicht kennt, hier ein paar Appetitanreger und
Sommerloch-Füller:
_____________________________________________________________
...
10.1.1 ToString Applied to the XML Type
_____________________________________________________________
<order>
<customer>
<firstname>John</firstname>
<lastname>Doe</lastname>
</customer>
<item>
<description>Big Screen Television</description>
<price>1299.99</price>
<quantity>1</quantity>
</item>
</order>
_____________________________________________________________
"the E4X programmer can access individual values of the XML value like this:"
_____________________________________________________________
// Construct the full customer name
var name = order.customer.firstname + " " + order.customer.lastname;
// Calculate the total price
var total = order.item.price * order.item.quantity;
_____________________________________________________________
...
11.1.4 XML Initialiser
_____________________________________________________________
// an XML object representing a person with a name and age
var person = <person><name>John</name><age>25</age></person>;
// a variable containing an XML object representing two employees
var e = <employees>
<employee id="1"><name>Joe</name><age>20</age></employee>
<employee id="2"><name>Sue</name><age>30</age></employee>
</employees>;
for (var i = 0; i < 10; i++)
e[i] = <employee id={i}> // compute id value
<name>{names[i].toUpperCase()}</name> // compute name value
<age>{ages[i]}</age> // compute age value
</employee>;
______________________________________________________________
...
11.1.5 XMLList Initialiser
_______________________________________________________________
var docfrag = <><name>Phil</name><age>35</age><hobby>skiing</hobby></>;
var emplist = <>
<employee id="0" ><name>Jim</name><age>25</age></employee>
<employee id="1" ><name>Joe</name><age>20</age></employee>
<employee id="2" ><name>Sue</name><age>30</age></employee>
</>;
______________________________________________________________
...
11.2.1 Property Accessors
______________________________________________________________
var order = <order id = "123456" timestamp="Mon Mar 10 2003 16:03:25
GMT-0800 (PST)">
<customer>
<firstname>John</firstname>
<lastname>Doe</lastname>
</customer>
<item>
<description>Big Screen Television</description>
<price>1299.99</price>
<quantity>1</quantity>
</item>
</order>;
var customer = order.customer; // get the customer element from the order
var id = order.@id; // get the id attribute from the order
var orderChildren = order.*; // get all the child elements from the order
element
var orderAttributes = order.@*; // get all the attributes from the order
element
// get the first (and only) order [treating single element as a list]
var firstOrder = order[0];
_____________________________________________________________
...
11.2.3 XML Descendant Accessor
______________________________________________________________
var e = <employees>
<employee id="1"><name>Joe</name><age>20</age></employee>
<employee id="2"><name>Sue</name><age>30</age></employee>
</employees>;
var names = e..name; // get all the names in e
______________________________________________________________
...
11.2.4 XML Filtering Predicate Operator
______________________________________________________________
var john = e.employee.(name == "John"); // employees with name John
var twoemployees = e.employee.(@id == 0 || @id == 1); // employees with
id's 0 & 1
var emp = e.employee.(@id == 1).name; // name of employee with id 1
_______________________________________________________________
...
12.1 The default xml namespace Statement
_______________________________________________________________
// declare some namespaces and a default namespace for the current scope
var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
var stock = new Namespace("http://mycompany.com/stocks");
default xml namespace = soap; // alternately, may specify full URI
// Create an XML initializer in the default (i.e., soap) namespace
var message = <Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<Body>
<m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</Body>
</Envelope>
// extract the soap encoding style using a QualifiedIdentifier (unqualified
attributes are in
// no namespace)
var encodingStyle = message.@soap::encodingStyle;
//extract the body from the soap message using the default namespace
var body = message.Body;
// change the stock symbol using the default namespace and qualified names
message.Body.stock::GetLastTradePrice.stock::symbol = "MYCO";
______________________________________________________________________________
ralf ...
-------------------------------------------------------------
Ralf Siegel - Freelance Developer
Recommended Listening: Blonde Redhead - Misery Is A Butterfly
http://www.blonde-redhead.com
-------------------------------------------------------------
------------------------------------------------------
Archiv : http://www.freelists.org/archives/ascoders/
Optionen : http://www.freelists.org/list/ascoders
------------------------------------------------------
Other related posts:
- » [ascoders] ECMA Approves ECMAScript for XML (E4X)
------------------------------------------------------------- Ralf Siegel - Freelance Developer Recommended Listening: Blonde Redhead - Misery Is A Butterfly http://www.blonde-redhead.com -------------------------------------------------------------