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?

3 of 5 people (60%) answered Yes
Recently 3 of 5 people (60%) answered Yes

Entry

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

Jan 23rd, 2008 10:40
jack sparrow, Martin Honnen,


An XMLList literal respectively an XMLList intializer is an expression
starting with <> and ending with </> to initialize an XMLList object as
an ordered list of XML objects. 
As with XML literals/initializers in the simplest form there are just
snippets of XML markup to describe the objects in the list e.g.
  var godList = <>
    <god>Kibo</god>
    <god>Xibo</god>
  </>;
  alert(godList.length()); // shows 2
  alert(godList);
  /* shows
  '<god>Kibo</god>
   <god>Xibo</god>'
  */
  XML.ignoreComments = false;
  XML.ignoreProcessingInstructions = false;
  var xmlList = <>
    <!-- Kibology for all -->
    <god>Kibo</god>
    <?process Kibology="on"?>
  </>;
  alert(xmlList.length()); // shows 3
  alert(xmlList);
  /* shows
  '<!-- Kibology for all -->
   <god>Kibo</god>
   <?process Kibology="on"?>'
  */
  XML.ignoreComments = true;
  XML.ignoreProcessingInstructions = true;
  var xmlList = <>
    <!-- Kibology for all -->
    <god>Kibo</god>
    <?process Kibology="on"?>
  </>;
  alert(xmlList.length()); // shows 1
  alert(xmlList.toXMLString()); // shows '<god>Kibo</god>'
As with XML literals/initializers you can also use JavaScript
expressions embedded in curly braces to dynamically create the objects
in the XMLList, you can create the content of an element with an
expression e.g.:
  var i = 1;
  var godList = <>
    <god>{"God " + i++}</god>
    <god>{"God " + i++}</god>
    <god>{"God " + i++}</god>
  </>;
  alert(godList.length()); // shows 3
  alert(godList);
  /* shows
  '<god>God 1</god>
   <god>God 2</god>
   <god>God 3</god>'
  */
You can create the value of attributes with an expression e.g.:
  var i = 1;
  var godList = <>
    <god id={"God" + i++}>Kibo</god>
    <god id={"God" + i++}>Xibo</god>
  </>;
  alert(godList.length()); // shows 2
  alert(godList);
  /* shows
  '<god id="God1">Kibo</god>
   <god id="God2">Xibo</god>'
  */
You can create element or attribute names with an expression e.g.:
  var elementName = 'GOD';
  var attributeName = 'ID';
  var godList = <>
    <{elementName.toLowerCase()}
{attributeName.toLowerCase()}="God1">Kibo</god>
    <{elementName.toLowerCase()}
{attributeName.toLowerCase()}="God2">Xibo</god>
  </>;
  alert(godList.length()); // shows 2
  alert(godList);
  /* shows
  '<god id="God1">Kibo</god>
   <god id="God2">Xibo</god>'
  */
Of course these different methods can be combined in one literal to
create element names, attribute names, element contents and attribute
values with arbitrary JavaScript expressions e.g.:
  var elementName = 'GOD';
  var attributeName = 'NUMBER';
  var i = 1;
  var godList = <>
    <{elementName.toLowerCase()} {attributeName.toLowerCase()}={i}>
{"God " + i++}</god>
    <{elementName.toLowerCase()} {attributeName.toLowerCase()}={i}>
{"God " + i++}</god>
  </>;
  alert(godList.length()); // shows 2
  alert(godList);
  /* shows
  '<god number="1">God 1</god>
   <god number="2">God 2</god>'
  */
http://islandsofworld.blogspot.com/
http://worldsbestbeaches.blogspot.com/
http://top-honeymoondestinations.blogspot.com/
http://topweddingguide.blogspot.com/