Online Shopping : Computers : Programming : Languages : JavaScript : Language Core : E4X (ECMAScript for XML)

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

16 of 17 people (94%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How does an XML literal respectively an XML initializer look?
How does an XML literal respectively an XML initializer look?

Mar 24th, 2009 18:25
chat alarab, Martin Honnen,


In its simplest form an XML literal is just a snippet of XML markup
written literally in a JavaScript/ECMAScript expression. On the right
side of the following assignments there are XML literals for the
different kind of nodes an XML object can represent (element node, CDATA
text node, comment node, processing instruction node):
  var god = <god>Kibo</god>;
  alert(god.nodeKind()); // shows 'element'
  var text = <![CDATA[Escaped markup.<br>CDATAs are text.]]>;
  alert(text.nodeKind()); // shows 'text'
  // need this to have comments parsed into objects
  XML.ignoreComments = false;
  var comment = <!-- Kibology for all -->;
  alert(comment.nodeKind()); // shows 'comment'
  XML.ignoreComments = true; // now comments are ignored
  var whatIsThis = <!-- All for Kibology -->;
  alert(whatIsThis.nodeKind()); // shows 'text'!
  // need this to have processing instructions parsed into objects
  XML.ignoreProcessingInstructions = false;
  var processingInstruction = <?process Kibology="on"?>;
  alert(processingInstruction.nodeKind()); 
  // shows 'processing-instruction';
  XML.ignoreProcessingInstructions = true;
  var whatIsThis = <?process Kibology="on"?>;
  alert(whatIsThis.nodeKind()); // shows 'text';
As you can see there is a setting needed to have comments parsed into
objects and there is a further setting needed to have processing
instructions parsed into object, with the default settings both are
ignored during parsing. As you can also see if there is a literal solely
containing a comment or processing instruction but the setting to ignore
them is true then an XML object for node kind 'text' is the result of
the expression.
An XML object can also represent an attribute node but you cannot write
a literal solely for an attribute obviously as an attribute in XML
markup is always part of an element. Here is a literal for an element
with two attributes:
  var element = <data god="Kibo" devil="Xibo" />;
  for each (var attribute in element.@*) {
    alert(attribute.nodeKind()); // shows 'attribute'
  }
An XML intializer however does not need to be a snippet of pure XML
markup, instead it can contain other JavaScript expressions in curly
braces to dynamically create XML. Here are some simple examples showing
the different places where expressions in curly braces can be used, of
course you can combine that to have complex expressions:
  // element content
  var now = <now>{new Date().toUTCString()}</now>;
  alert(now.toXMLString());
  // shows e.g. '<now>Wed, 30 Mar 2021 17:24:09 GMT</now>'
  // attribute content
  var published = <published at={new Date().toUTCString()} />;
  published.toXMLString();
  // shows e.g. '<published at="Wed, 30 Mar 2021 17:27:17 GMT"/>'
  // element name
  var tagName = 'TEXT';
  var element = 
<{tagName.toLowerCase()}>Kibology for all.</{tagName.toLowerCase()}>;
  alert(element.toXMLString());
  // shows '<text>Kibology for all.</text>'
  // attribute name
  var name = 'god';
  var element = <religion {name.toUpperCase()}="Kibo">yes</religion>;
  alert(element.toXMLString());
  // shows '<religion GOD="Kibo">yes</religion>';
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com