如果元素具有属性

时间:2018-04-15 11:54:39

标签: javascript xpath

我想用纯JavaScript解析GetCapabilities XML。问题是,当父元素具有属性时,它返回一个空字符串。但是,如果我从XML中删除所有属性,解析器将给出正确的结果。请参见下面的示例。知道为什么会这样吗?



// Example 1: with attributes

let xmlString = `<WMS_Capabilities xmlns="http://www.opengis.net/wms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.3.0" updateSequence="1523788994171" xsi:schemaLocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd">
                     <Service>
                         <Name>WMS</Name>
                     </Service>
                </WMS_Capabilities>`;
let doc = new DOMParser().parseFromString(xmlString,'text/xml');
let result = doc.evaluate('/WMS_Capabilities/Service/Name', doc, null, XPathResult.STRING_TYPE, null);


document.getElementById('result1').innerHTML = result.stringValue;

// Example 2: without attributes

let xmlStringWithoutAttr = `<WMS_Capabilities>
                                <Service>
                                    <Name>WMS</Name>
                                </Service>
                            </WMS_Capabilities>`;
let doc2 = new DOMParser().parseFromString(xmlStringWithoutAttr,'text/xml');
let result2 = doc.evaluate('/WMS_Capabilities/Service/Name', doc2, null, XPathResult.STRING_TYPE, null);

document.getElementById('result2').innerHTML = result2.stringValue;
&#13;
<p>With attributes: <span id="result1"></span></p>
<p>Without attributes: <span id="result2"></span></p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

属性xmlns="http://www.opengis.net/wms"是一个默认的名称空间声明,并将这些元素放入该名称空间中,因此使用XPath 1.0来选择它们时,您需要使用绑定到名称空间的前缀来选择它们。使用该API,您需要

// Example 1: with attributes

let xmlString = `<WMS_Capabilities xmlns="http://www.opengis.net/wms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.3.0" updateSequence="1523788994171" xsi:schemaLocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd">
                     <Service>
                         <Name>WMS</Name>
                     </Service>
                </WMS_Capabilities>`;
let doc = new DOMParser().parseFromString(xmlString,'text/xml');
let result = doc.evaluate('/wms:WMS_Capabilities/wms:Service/wms:Name', doc, function(prefix) { if (prefix === 'wms') return 'http://www.opengis.net/wms'; else return null; }, XPathResult.STRING_TYPE, null);


document.getElementById('result1').innerHTML = result.stringValue;

// Example 2: without attributes

let xmlStringWithoutAttr = `<WMS_Capabilities>
                                <Service>
                                    <Name>WMS</Name>
                                </Service>
                            </WMS_Capabilities>`;
let doc2 = new DOMParser().parseFromString(xmlStringWithoutAttr,'text/xml');
let result2 = doc2.evaluate('/WMS_Capabilities/Service/Name', doc2, null, XPathResult.STRING_TYPE, null);

document.getElementById('result2').innerHTML = result2.stringValue;
<p>With attributes: <span id="result1"></span></p>
<p>Without attributes: <span id="result2"></span></p>

您可以使用您喜欢的任何前缀,您只需要确保您用作evaluate的第三个参数的函数返回要绑定到该前缀的名称空间URI,即名称空间URI您需要选择的元素。