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?

6 of 6 people (100%) answered Yes
Recently 6 of 6 people (100%) answered Yes

Entry

How do I test if an attribute of an XML object is set?

Jan 22nd, 2008 06:24
daim, Gregor Heine,


One way to test if an attribute of an XML object is set is to try to
retrieve the attribute as an XMLList and test it's length. If it turns 
up with length zero, then the attribute is not there:

 if (foo.@bar.length() != 0) {
   alert("bar attribute set");
 }

Alternatively, the following construct has the same effect:

 if ("@bar" in foo) {
   alert("bar attribute set");
 }

Note : the first method will throw an exception in Gecko if the
element  foo does not have the attribute 'bar' explicitely set. 
For selecting all elements that have the attribute 'bar' (explictely set
or not), one can use: 

elements.*.(function::attribute('bar').length() != 0)