For example, given the following XML document:
<?xml version="1.0" encoding="utf-8"?> <root> <para id="test">Test</para> </root>
If you try to access the element with ID test, you get null:
alert(document.getElementById('test'));
alert(document.querySelector('#test'));
// null
This happens because in the XML DOM there's no predefined attribute associated with that element. Instead, you have to access the aforementioned element using a more general approach:
alert(document.getElementsByTagName('para')[0]);
alert(document.querySelector('root para:first-child'));
// [object Element]
As said earlier, a quick-and-dirty way to fix this problem is to set the XHTML namespace on a nearest ancestor, for example the root element:
<?xml version="1.0" encoding="utf-8"?> <root xmlns="http://www.w3.org/1999/xhtml"> <para id="test">Test</para> </root>
Now DOM methods such as getElementById() will work as expected.