Main Page

QName class

A
Namespace
object can then be used in selection statements:
var oWroxNS = new Namespace(“wrox”, “http://www.wrox.com/”);
var oXml = <wrox:root xmlns:wrox=”http://www.wrox.com/”>
<wrox:message>Hello World!</wrox:message>
</wrox:root>;
var sMessage = oXml.oWroxNS::message;
The highlighted line uses the
oWroxNS Namespace
object to select the
<wrox:message>
text
Hello
World!
. If the XML code specifies a namespace, this method must be used when selecting elements in E4X.
The QName class
The
QName
class represents a qualified name for XML elements and attributes. The qualified name is
made up of a namespace prefix and a local name, such as the following:
<wrox:message xmlns:wrox=”http://www.wrox.com/”>Hello World!</wrox:message>
In this code,
wrox:message
is a qualified name, with
wrox
representing a namespace and
message
rep-
resenting the local name (also called tag name). The prefix
wrox
points to a namespace URI of
http://
www.wrox.com/
. A
QName
object can represent the
wrox:message
qualified name like this:
var oWroxNS = new Namespace(“wrox”, “http://www.wrox.com/”);
var oQName = new QName(oWroxNS, “message”);
Using this version of the constructor, a
Namespace
object must be provided to represent the qualified
name. Alternately, a
QName
object can be created without any namespace if one isn’t specified:
var oQName = new QName(“message”);
After the object is created, it has two properties that can be accessed:
uri
and
localName
. The
uri
prop-
erty returns the URI of the namespace specified when the object is created (or an empty string if no
namespace is specified); the
localName
property returns the local-name part of the qualified name. For
example:
var oWroxNS = new Namespace(“wrox”, “http://www.wrox.com/”);
var oQName = new QName(oWroxNS, “message”);
alert(oQName.uri); //outputs “http://www.wrox.com/”
alert(oQName.localName); //outputs “message”
In this example, the
uri
property returns
“http://www.wrox/com/”
and
localName
returns
“mes-
sage”
. These properties are read-only and cause an error if you try to change their values.
The
QName
object overrides the
toString()
object to return a string in the form
uri::localName
, such
as
“http://www.wrox.com/::message”
in the previous example.
The XML class
As mentioned previously, an
XML
object represents an XML element, meaning that it represents an
ordered list of properties with a name, a parent, a set of attributes, child nodes, and a set of namespaces.
An
XML
object can be created in one of two ways. The first way is to use its constructor and pass in an
XML string like this:
608
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 608


JavaScript EditorFree JavaScript Editor     Ajax Editor


©