简体   繁体   English

使用xml:id属性获取特定的xml节点

[英]Get specific xml node with xml:id attribute

I am using jquery to get node's values and texts. 我正在使用jquery获取节点的值和文本。

This is my .xml: 这是我的.xml:

<div xml:id = "pe007">
<persName>
<surname>Smith</surname
</persName>
</div>

This is my js code: 这是我的js代码:

$.ajax({
type: "GET",
url: "myfile.xml",
dataType: "xml",
success: function (xml) {

var surname = $(xml).find('div[xml:id="pe007"] > surname').text();

}});

I already know how to get text and value of nodes. 我已经知道如何获取节点的文本和值。 I just want to select a specific node. 我只想选择一个特定的节点。 When the attribute is something like 'ref', I have no issues of getting what I want (with .attr("ref") ). 当属性是类似于“ ref”的东西时,我就不会得到想要的东西(使用.attr("ref") )。 But jquery seems not to like this expression: 但是jQuery似乎不喜欢这个表达式:

'div[xml:id="pe007"]'

In fact I get 实际上我得到

Error: Syntax error, unrecognized expression: div[xml:id="pe007"] > surname

Why is div[ref="pe007"] OK and not xml:id? 为什么div [ref =“ pe007”]可以,而不是xml:id? Is it something to do with the colon? 与结肠有关吗? I really need to access the children of a div with a specific xml:id attribute. 我确实需要使用特定的xml:id属性访问div的子级。

The answer was relatively simple: I had to escape the colon with \\\\ , so: 答案相对简单:我必须使用\\\\来使冒号转义,所以:

var surname = $(xml).find('div[xml\\:id="pe007"] > surname').text();

works. 作品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM