
CHAPTER 9: Integrating XML into JavaScript The XML Object 242
X
The result of XML.children() contains 3 elements, the direct child tags <one> and <two>, and the
directly contained text of the
<top> tag:
> x.children()
<one>one text</one>
<two>
two text
<inside>inside text</inside>
</two>
top text
> x.children().length()
3
X
The result of XML.elements() contains 2 elements, the direct child tags <one> and <two>:
> x.elements()
<one>one text</one>
<two>
two text
<inside>inside text</inside>
</two>
> x.elements().length()
2
X
The result of XML.descendants() contains 7 elements, the direct child tags <one> and <two>, the
<inside> tag one level down, and the text contents of all the tags:
> x.descendants()
<one>one text</one>
one text
<two>
two text
<inside>inside text</inside>
</two>
two text
<inside>inside text</inside>
inside text
top text
> x.descendants().length()
7
Creating and accessing namespaces
Simple access statements access elements in the default namespace. If you need to define elements in
more than one namespace, you must use a Namespace object
to access any elements that are NOT in the
default namespace.
Defining a namespace within the tree
You can define a namespace within an XML element using the xmlns attribute, and define elements within
the schema as belonging to that namespace. For example, these additions to the example XML add a
namespace that maps the prefix "
kids" to the namespace "http://kids.mybookstore.com", and then
uses the prefix to place a particular book element in that namespace:
<bookstore xmlns:kids="http://kids.mybookstore.com">
Kommentare zu diesen Handbüchern