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

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

Entry

How do I use the method attribute?
How do I use the method attribute?

Apr 14th, 2005 03:20
Martin Honnen,


The method attribute takes one argument, the attribute name of
attributes to look for. It calls the method attribute
(http://www.faqts.com/knowledge_base/view.phtml/aid/35362/fid/1784) of
each object in this XMLList and returns the results in order in an XMLList.
[Note: while the method attribute exists you can access attributes using
the property accessors
  xmlList.@attributeName
  xmlList['@attributeName']
thus you might not need/use the method attribute but prefer the shorter
property accessor instead.]
Here are some examples for using the method attribute:
  var xmlList = <>
    <god name="Kibo" home="http://www.kibo.com/" />
    <god name="Xibo" />   
  </>;
  alert(xmlList.attribute('name').length()); // shows 2
  alert(xmlList.attribute('home').length()); // shows 1
  alert(xmlList.attribute('*').length()); // shows 3
  alert(xmlList.attribute('attributeName').length()); // shows 0
  var xml = <gods>
    <god>
      <name>Kibo</name>
      <home xmlns:xlink="http://www.w3.org/1999/xlink"
            xlink:type="simple"
            xlink:href="http://www.kibo.com/">www.kibo.com</home>
    </god>
  </gods>;
  var xmlListOfDescendants = xml..*;
  // attributes in no namespace:
  alert(xmlListOfDescendants.attribute('*').length()); // shows 0
  // all attributes
  alert(xmlListOfDescendants.attribute(new QName('*')).length()); 
  // shows 2
  for each (var xmlAttribute in xmlListOfDescendants.attribute(new
QName('*')))  
  {
    alert(xmlAttribute.name() + ': ' + xmlAttribute);
  }
  /* shows
  'http://www.w3.org/1999/xlink::type: simple'
  'http://www.w3.org/1999/xlink::href: http://www.kibo.com/'
  */