faqts : Computers : Programming : Languages : JavaScript : XML : E4X (ECMAScript for XML)

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 15 people (87%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I access elements or attributes that are in a namespace?
How can I access elements or attributes that are in a namespace?

Dec 26th, 2008 21:16
Rockys rainwal, Raj Aryan, Martin Honnen, http://www.travelpackageindia.com/


As http://www.faqts.com/knowledge_base/view.phtml/aid/35123/fid/1762
points out E4X is fully namespace aware, elements and attributes have
qualified names.
If you want to access an element or attribute in a namespace you can
easily do so by creating a Namespace object with the proper namespace e.g.
  var pf1 = new Namespace('http://example.com/2005/04/ns1');
and using that identifier in your element or attribute name in the form
of a qualified identifier which consists of the variable name, a double
colon '::' and the local element or attribute name e.g.
  xmlObject.pf1::elementName
  [email protected]::attributeName
As with normal element and attribute access the use of the name '*' as a
wild card is possible to match all attributes or all elements in a
certain namespace.
To give some examples we use the following XML markup which has elements
in the XHTML namespace (namespace URI http://www.w3.org/1999/xhtml) and
elements in the SVG namespace (namespace URI http://www.w3.org/2000/svg)
elements and attributes in the XLINK namespace (namespace URI
http://www.w3.org/1999/xlink):
var xhtmlMarkup = 
<html xmlns="http://www.w3.org/1999/xhtml"
      xml:lang="en">
<head>
<title>Example compound document with XHTML and SVG elements</title>
</head>
<body>
<h1>Example compound document with XHTML and SVG elements</h1>
<p>Kibology for all:
<svg xmlns="http://www.w3.org/2000/svg"
     width="200" height="200">
  <a xmlns:xlink="http://www.w3.org/1999/xlink"
     xlink:href="http://www.kibo.com/">
    <circle cx="100" cy="100" r="50" fill="green" />
    <text x="100" y="105" 
          font-size="20" fill="yellow"
          text-anchor="middle">Kibology</text>
  </a>
</svg>
</p>
</body>
</html>;
To access elements and attributes in those namespaces (without using the
default namespace statement we look at later) you have to define three
variables with Namespace objects with the proper URIs:
var xhtml = new Namespace('http://www.w3.org/1999/xhtml');
var xlink = new Namespace('http://www.w3.org/1999/xlink');
var svg = new Namespace('http://www.w3.org/2000/svg');
so that you can use qualified identifiers as in the following examples:
// XHTML child elements of the root element
var xhtmlChildElements = xhtmlMarkup.xhtml::*;
alert(xhtmlChildElements.length()); // shows 2
// XHTML h1 descendant elements of the root element
var h1Descendants = xhtmlMarkup..xhtml::h1;
alert(h1Descendants.length()); // shows 1
// SVG descendant elements of the root element
var svgDescendants = xhtmlMarkup..svg::*;
alert(svgDescendants.length()); // shows 4
// SVG a element
var svgAElement = xhtmlMarkup..svg::a;
alert(svgAElement.nodeKind()); // shows 'element'
// XLINK href attribute of the SVG a element
alert([email protected]::href); // shows 'http://www.kibo.com/'
// all attributes (both those in a namespace and those in no namespace
var attributeList = [email protected]*::*;
alert(attributeList.length()); // shows 13
var result = '';
for each (var attribute in attributeList) {
  result += attribute.name() + ' : ' + attribute + '\r\n';
}
alert(result);
/* shows
'http://www.w3.org/XML/1998/namespace::lang : en
width : 200
height : 200
http://www.w3.org/1999/xlink::href : http://www.kibo.com/
cx : 100
cy : 100
r : 50
fill : green
x : 100
y : 105
font-size : 20
fill : yellow
text-anchor : middle'
*/
// compare to all attributes in no namespace
var attributeList = [email protected]*;
alert(attributeList.length()); // shows 11
var result = '';
for each (var attribute in attributeList) {
  result += attribute.name() + ' : ' + attribute + '\r\n';
}
alert(result);
/* shows
'width : 200
height : 200
cx : 100
cy : 100
r : 50
fill : green
x : 100
y : 105
font-size : 20
fill : yellow
text-anchor : middle'
*/
As already explained in
http://www.faqts.com/knowledge_base/view.phtml/aid/35123/fid/1762 there
is a new statement to declare a default namespace in the current scope
using e.g.
  default xml namespace = 'http://example.com/2005/04/ns1';
If you make use of that then child and descendant element access applies
to elements in the default namespace e.g. continueing with the above
mixeed namespace XHTML and SVG markup you could do:
var titleElements = xhtmlMarkup..title;
alert(titleElements.length()); // shows 0
// now use XHTML URI as default namespace URI
default xml namespace = 'http://www.w3.org/1999/xhtml';
var titleElements = xhtmlMarkup..title;
alert(titleElements.length()); // shows 1
http://www.jaipurtravelguide.com/
http://www.jaipurtravels.com/
http://www.jaipurjaipur.com/
http://www.jaipurtourismguide.com/
http://www.rajasthantravelsguide.com/
http://www.rajasthantravelguide.co.in/
http://www.travelpackagerajasthan.com/
http://www.namaskarindiatravels.com/
http://www.indianhotelsindia.com/
http://www.travelpackageindia.com/
http://www.navyatravels.com/
http://www.hillstationindiatour.com/
http://www.wildlifeindiatravel.com/ 
http://www.pushkartravels.com/
http://www.udaipurtravels.com/
http://www.agratravels.com/
http://indiantravelguide.co.in/
http://www.traveltoindia.biz/
http://www.info4india.com/
http://www.indiantravelsguide.com/
http://www.indiantravelguideindia.com/
http://rajasthantravelguide.wordpress.com/
http://attractioninjaipur.blogspot.com/
http://jaipurtravelsguide.blogspot.com/
http://hoteles-en-la-india.blogspot.com/
http://jaipurguiadeviajes.blogspot.com/
http://jaipurrajasthanindia.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://wildlifeindiatravel.blogspot.com/
http://indiantravelguideindia.blogspot.com/
http://indianhotelsindia.blogspot.com/
http://rajasthantravelsguide.blogspot.com/
http://pushkartravels.blogspot.com/
http://indiatravelsguide.blogspot.com/
http://goaguidedevoyage.blogspot.com/
http://goatravelsguide.blogspot.com/
http://indiaguadeviajes.blogspot.com/
http://htelseninde.blogspot.com/
http://indienguidedevoyage.blogspot.com/
http://indianhillstationsindiatravel.blogspot.com/
http://jaipurtravelguiderajasthan.blogspot.com/
http://hillstationinindia.blogspot.com/
http://jaipurcarrentals.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://indianadventuretourpackages.blogspot.com/
http://rajasthantraintourpackage.blogspot.com/
http://jaipurinformationguide.blogspot.com/
http://rajasthanculturaltourpackages.blogspot.com/
http://uttaranchaltourpackages.blogspot.com/
http://luxurytourspackages.blogspot.com/
http://indiahimalayatourtravelpackages.blogspot.com/
http://eastindiatravelpackages.blogspot.com/
http://indiahillstationstravelpackages.blogspot.com/
http://indiatravelpackages.blogspot.com/
http://westindiatourpackages.blogspot.com/
http://lehladakhtourpackages.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://worldtravelsguide.blogspot.com/
http://jaipursilverjewellery.com/
http://www.jaipurhandicrafts.com/