0

I know I can do it with the length() method:

>x = <a attr1='33' />
>x.@attr1
33
>x.@attr1.length()
1
>x.@attr2.length()
0

so I could use

if (x.@someattr.length() > 0)
{
    .... do something ....
}

but is there a more appropriate way?

Jason S
  • 184,598
  • 164
  • 608
  • 970

2 Answers2

1

Never mind, I found the answer by poring through the Ecma-357 standard, particularly the XML.prototype.* and XMLList.prototype.* sections 13.4 and 13.5.

It's the hasOwnProperty() method:

js>x = <a attr1='33' ><item>gumball!</item></a>
<a attr1="33">
  <item>gumball!</item>
</a>
js>x.@attr1
33
js>x.hasOwnProperty('@attr1');
true
js>x.hasOwnProperty('@attr2');
false
js>x.hasOwnProperty('item');
true
js>x.hasOwnProperty('mongoose');
false
Jason S
  • 184,598
  • 164
  • 608
  • 970
0

easiest way:

(@attr1 in theXML)

this will return true if id attrtibute exists and false otherwise.

Mahmoud Badri
  • 1,256
  • 14
  • 24