首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果元素具有属性,则无法使用xpath从XML中获取数据

如果元素具有属性,则无法使用xpath从XML中获取数据
EN

Stack Overflow用户
提问于 2018-04-15 11:54:39
回答 1查看 66关注 0票数 0

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

代码语言:javascript
复制
// 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;
代码语言:javascript
复制
<p>With attributes: <span id="result1"></span></p>
<p>Without attributes: <span id="result2"></span></p>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-15 12:16:26

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

代码语言:javascript
复制
// 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;
代码语言:javascript
复制
<p>With attributes: <span id="result1"></span></p>
<p>Without attributes: <span id="result2"></span></p>

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49841607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档