xml 使用 XPath 获取属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4531995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting attribute using XPath
提问by GurdeepS
Given an XML structure like so:
给定一个像这样的 XML 结构:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
How could I get the value of lang(where langis engin book title), for the first element?
我怎么能得到的值lang(这里lang是eng在书名),第一个元素?
回答by Dimitre Novatchev
How could I get the value of lang (where lang=eng in book title), for the first element?
对于第一个元素,我如何获得 lang(书名中的 lang=eng)的值?
Use:
使用:
/*/book[1]/title/@lang
This means:
这意味着:
Select the langattribute of the title element that is a child of the first bookchild of the top element of the XML document.
选择作为XML 文档顶部元素lang的第一book个子元素的子元素的 title 元素的属性。
To get just the string value of this attribute use the standard XPath function string():
要仅获取此属性的字符串值,请使用标准 XPath 函数string():
string(/*/book[1]/title/@lang)
回答by smulldino
Thanks! This solved a similar problem I had with a data attribute inside a Div.
谢谢!这解决了我在 Div 中使用 data 属性时遇到的类似问题。
<div id="prop_sample" data-want="data I want">data I do not want</div>
Use this xpath: //*[@id="prop_sample"]/@data-want
使用这个 xpath: //*[@id="prop_sample"]/@data-want
Hope this helps someone else!
希望这对其他人有帮助!
回答by Sharath
You can try below xPath pattern,
您可以尝试以下 xPath 模式,
XPathExpression expr = xPath.compile("/bookstore/book/title[@lang='eng']")
回答by Vinod Srivastav
You can also get it by
你也可以通过
string(//bookstore/book[1]/title/@lang)
string(//bookstore/book[2]/title/@lang)
although if you are using XMLDOM with JavaScript you can code something like
尽管如果您将 XMLDOM 与 JavaScript 一起使用,您可以编写类似
var n1 = uXmlDoc.selectSingleNode("//bookstore/book[1]/title/@lang");
and n1.textwill give you the value "eng"
并且n1.text会给你价值"eng"
回答by starcwl
you can use:
您可以使用:
(//@lang)[1]
these means you get all attributes nodes with name equal to "lang" and get the first one.
这意味着您将获得名称等于“lang”的所有属性节点并获得第一个。
回答by Royce
If you are using PostgreSQL, this is the right way to get it. This is just an assumption where as you have a booktable TITLEand PRICEcolumn with populated data. Here's the query
如果您使用的是 PostgreSQL,这是获取它的正确方法。这只是一个假设,因为您有一个带有填充数据的书籍表TITLE和PRICE列。这是查询
SELECT xpath('/bookstore/book/title/@lang', xmlforest(book.title AS title, book.price AS price), ARRAY[ARRAY[]::TEXT[]]) FROM book LIMIT 1;
回答by vtd-xml-author
Here is the snippet of getting the attribute value of "lang" with XPath and VTD-XML.
这是使用 XPath 和 VTD-XML 获取“lang”属性值的片段。
import com.ximpleware.*;
public class getAttrVal {
public static void main(String s[]) throws VTDException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("input.xml", false)){
return ;
}
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/bookstore/book/title/@lang");
System.out.println(" lang's value is ===>"+ap.evalXPathToString());
}
}

