Entry
How do I use the method attribute?
How do I use the method attribute?
Apr 9th, 2005 03:38
Martin Honnen,
The method attribute takes a single argument with the attribute name and
returns an XMLList with attributes matching the argument.
[Note: Attributes of an XML object representing an XML element can be
accessed like normal object properties with a special attribute property
identifier
xmlObject.@attributeName
xmlObject['@attributeName']
so while there is a method attribute to access an attribute by its name
you might not need/use the method but prefer the shorter property access
instead. See
http://www.faqts.com/knowledge_base/view.phtml/aid/35118/fid/1762
for further details.]
Here are some examples on using the attribute method:
var paragraph = <p id="p1" xml:lang="en" class="style1">Kibology for
all.</p>;
alert(paragraph.attribute('id')); // shows 'p1'
alert(paragraph.attribute('class')); // shows 'style1'
alert(paragraph.attribute(new
QName('http://www.w3.org/XML/1998/namespace', 'lang'))); // shows 'en'
While you can pass '*' as a wild card attribute name it interestingly
enough does not return all attributes with any name in any namespace but
only all attributes with any name in no namespace:
alert(paragraph.attribute('*').length()); // shows 2
If you really want all attributes with any name in any namespace or in
no namespace you need to use:
alert(paragraph.attribute(new QName('*')).length()); // shows 3