如何获取某个命名空间中节点的所有属性

how to get all attributes of node in certain namespace xml javascript

本文关键字:节点 属性 命名空间 何获取 获取      更新时间:2023-09-26

我有一个来自xml文档的节点。它具有来自多个名称空间的多个属性。我想找到fo命名空间的所有属性。如何做到这一点?例如,从下面的示例中,我想获得以fo:

开头的所有属性
<thingy fo:line-height="200%" fo:blah="blah" gh:sdf="sdfdfer">
blah
</thingy>
var tag = document.getElementsByTagName('thingy')[0];
var attr = tag.attributes;
for(var i=0;i<attr.length;i++)
{
    if(attr.item(i).nodeName.search('fo:') == 0)
    {
        alert(attr.item(i).nodeName);
        alert(attr.item(i).nodeValue);
    }
}

Working JS Fiddle