E4X Adds Direct XML Support to JavaScript : Page 2

E4X provides a more intuitive, familiar, and natural way for JavaScript developers to deal with XML. E4X code is easier to read, write, and maintain than standard XML in programming models such as XSLT, XQuery, and the DOM.

WEBINAR:

On-Demand

Understanding the Usage of E4X
Before digging deeper into E4X, see how E4X can make a difference in your XML handling. Table 1 provides examples of two XML parsing approaches: one using DOM, the other E4X.

Figure 1. The Output from XML Parsing Approaches (With and Without E4X) Is the Same

E4X offers a cleaner and easier way to retrieve values from XML within JavaScript, wouldn't you say? All because E4X is meant specifically for providing XML support. Without E4X, you have to use an XML library/component, and those work differently in different browser.

E4X also provides all types of language constructs, such as statement loops, operators, namespaces, objects, keywords, etc. In order to use it in your pages, you just add the parameter e4x=1 in the script tag as follows:

<script type="application/x-javascript; e4x=1">

Or

<script type="text/javascript; e4x=1">

Context Keywords
E4X adds three context keywords (each, XML, namespace). Context keywords take on a specific meaning when used in specified contexts where identifiers are not permitted by the syntactic grammar.

Statements
The default XML namespace of the global scope has the initial value of no namespace. You can define its scope also. For example, if you define it within a function call, the value set will not be accessible outside the function. To deal with XML with namespaces easily, you can declare a default namespace for the current scope (e.g., function scope or global scope) using the newly introduced statement, as follows:

default xml namespace = expression;

The above statement should evaluate to a string with a URI or to a namespace object. The result is that all elements created in the current scope will be in the default namespace, provided they have no prefix in their name. The following code snippet shows the uri value when the default namespace is both defined and not defined:

The for-in statement evaluates an expression and iterates through each property of the resulting XML object. When the expression evaluates to an XML object, the for-in statement converts the XML object to an XMLList and iterates over the resulting XMLList. The following snippet shows the usage of a for-in loop statement:

The for-each-in statement behaves differently from the for-in statement. In particular, it assigns the loop variable over the range of the object rather than the domain of the object. For example, the for-each-in statement binds the loop variable to the property values of the given object rather than the property names.

An attribute identifier is used to identify the name of a XML attribute. It evaluates to a value of type AttributeName. The preceding "@" character distinguishes an XML attribute from an XML element with the same name.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date