xml:id

xml:id, a W3C Recommendation, offers a means for XML processors to be able to find unique id attributes across XML documents without needing to perform validations, whether in the internal document subset or load external documents (Firefox does not offer either for XML). This was possible since the 'xml:' prefix was reserved and belongs to its own assumed namespace (see below).

As Firefox does not currently implement getElementById() on XML documents (bugĀ 275196) --besides XUL documents which grab @id or @ref--this simple helper function can allow getElementById() to work on any XML document (such as returned by an Ajax call), though it does not attempt to parse a DTD to find attributes of type ID and looks solely for xml:id.

var getElementByIdWrapper = function (xmldoc, myID) {
        return xmldoc.evaluate('//*[@xml:id="'+myID+'"]', xmldoc, 
                function () {
                  return 'http://www.w3.org/XML/1998/namespace';},
                XPathResult.FIRST_ORDERED_NODE_TYPE, 
                null).singleNodeValue;
};

// Confirm it works:
var el = getElementByIdWrapper(xmldoc, 'pg2'); // Got it
alert(el.getAttributeNS('http://www.w3.org/XML/1998/namespace', 'id')); // Verify the function works

Document Tags and Contributors

Tags:
Contributors to this page: George3, Ms2ger, ericjung, Brettz9, sabrish
Last updated by: sabrish,