Learn to use ECMAScript (aka JavaScript) for XML (E4X), and discover of technical articles and tips, tutorials, standards, and IBM Redbooks. ECMAscript for XML (more colloquially known as E4X) has had comparatively limited success of adoption in the face of the power of JSON. E4X: ECMAScript for XML ECMAScript for XML, better known as E4X, is a standard extension[22] to JavaScript This section presents a quick tutorial on E4X.

Author: Monos Nikorr
Country: Ukraine
Language: English (Spanish)
Genre: Software
Published (Last): 5 October 2004
Pages: 169
PDF File Size: 1.87 Mb
ePub File Size: 7.29 Mb
ISBN: 448-2-98854-624-7
Downloads: 52626
Price: Free* [*Free Regsitration Required]
Uploader: Mezshura

First came Javascript, introduced by Netscape and having no real non-marketing relation tutirial the Java programming language. Then came JScript, Microsoft’s Javascript implementation.

Then came ECMAscript, the standardized version of the language which unified the flavors at least the basics and provided tutofial behavior and licensing terms to implementations. ECMAscript, the heir to a long series of unfortunate and confusing names, serves as the basis to much of the dynamic content on the Web.

These languages are generally exposed to script using a set of objects called the Document Object Model DOMa notoriously difficult way to navigate and shape the XML structure. E4X support is still not universal — it’s not supported in Internet Explorer yet, but it is supported in Firefox and implementations based on the open-source Rhino Javascript implementation, including the WSO2 Mashup Server.

This quick start guide should not only give you the basics of E4X, but also point out some of the tricky cases that an intermediate user will likely encounter.

Knowing where these traps are will lead you quickly to an enjoyable and productive use tutorisl E4X. A working knowledge of XML and Javascript is required.

For those with a working knowledge of XPath, we highlight differences in assumptions to make the transition smoother. If you try you might get an error. Watch out for the following circumstances:. But if so you should watch out for backslashes in the XML — as they are interpreted as Javascript escaping mechanism. This is obviously useful in queries for instance, when asking for all the children an element, an XMLList is created.

Once you have an XML object, you can access its name and namespace, children, attributes, and text content using the familiar Javascript dot operator. The XML literal can contain comments and processing instructions as well as elements, attributes, and text. A child element can be accessed by name as a property of the parent.

If there are more than one child with that name, an XML List is returned, which can be further qualified by an index or other qualifier. Notice that one writes “c. One doesn’t write “customer. The name of the variable is separate from the name of the XML element value. I’ve adopted the convention tuforial naming the variable the same as the XML element to avoid this.


E4X Quick Start Guide

That is, in the above example, I’d typically name the variable “customer” rather than “c”. In XPath terms the context of the “. A child attribute can be accessed by name using the ” ” prefix, or with the attribute method. Escape reserved words when they appear as XML element names. Javascript has a fair number of reserved words that appear fairly commonly as XML element names.

In this case, dot notation cannot be allowed, and an escaped syntax must be employed. Escape names with “. Dot and hyphen already have meaning within Javascript, so when referring to an XML element containing one of those characters, an escaping mechanism must be employed. The following expressions are invalid E4X because of reserved words or invalid name characters.

The text value of an element with a primitive value no element children or of an attribute can be obtained explicitly as a string by the toString method, but in most circumstances the toString method will be called automatically when a string value is needed. For a complex element one with childrentoString returns the XML syntax representation of the element. One useful construct in XPath is the predicate notation e. E4X has a similar construct:. In this notation the XML List of elements matching c.

If no elements match, the result is an empty XML list, best checked for empty using “. Children hide variables of the same name. If there is a choice within the filtering expression between a child element name or a variable, the child element name will take precedence, sometimes with unexpected results, as shown below:. In this example, one might expect that x would be empty, as the type attribute of the first phone element is not equal to the value of the “preferred” variable.

But instead, the value of the “preferred” element is used. The rule to remember is that within a filter expression, names are assumed to be relative to the context XML. Only if no such name is found are variables checked.

Return the processing instruction children of element with the specified name if omitted, all are returned. Return the concatenated text nodes of this element and all its descendants.

Javascript E4X tutorial and examples – nk4um

tutoeial Within text content, curly braces can be used to insert a value into the XML:. Attribute values can be determined by replacing the whole attribute value including quotes! Curly braces within quotes will be treated literally. Deleting an XML element or attribute from a structure is accomplished with the delete operator:. E4X also provides many DOM-like capabilities, often synonyms for the functionality already discussed.

Flash E4X Tutorial

The methods of the XML object include the following methods. Returns the child element with the given propertyName, or if propertyName is an integer, returns the child in that position.


Compares this element with the value, primarily provided to enable an XML element and an XML list to be used interchangeably. Returns a deep copy of the element.

The parent property of the copy will be set to null. Returns the descendant elements children, grandchildren, etc. If a name is provided, only elements with that name are returned. Returns the child elements. Returns true for attributes, text nodes, or elements without child elements, otherwise false. Returns an array of Namespace objects representing the namespaces in scope for this object.

Inserts child2 immediately after child1 in the XML object’s children list. Inserts child2 immediately prior to child1 in the XML object’s children list. Returns the namespace associated with this object, or if a prefix is specified, an in-scope namespace with that prefix. An array of Namespace objects representing the namespace declarations associated with this object.

The parent titorial this object. For an XML List tutoria, this returns undefined unless all the items of the list have the same parent. A list of all processing instructions that are children of this element.

E4X: JavaScript on steroids

If a name is provided, only processing e44x matching this name will be returned. Replace the children of the object with the value typically an XML List. Sets the name of the XML object to the requested value possibly qualified.

For elements without element children, returns the values of the text node children. For elements with element children, returns same as toXMLString. For other kinds of objects, the value of the object.

E4X uses methods where DOM uses properties. In E4X, children elements are exposed as properties, so similar accessors are modeled as methods instead. The for operator is extended to more easily traverse the properties of an object especially children of an XML object, or items in an XMLList. This information is extracted from the ECMA specification [1]. Contribute your ideas for improving this document to mashup-user wso2. Watch out for the following circumstances: Named entities other than those defined in XML 1.

Curly braces are used for templating see below. If there is a choice within the filtering expression between a child element name or a variable, the child element name will take precedence, sometimes with unexpected results, as shown below: Select all descendents children, grandchildren, etc.

Methods of the XML object. Adds child as a new child of the element, after all other children. A string representing the kind of object this is e.

Removes a namespace from the in-scope namespaces of the element.