[英]XPath expression evaluation error
我嘗試解析一個大的XML文件,我正在使用很多相對路徑的XPath表達式。
現在我遇到了.net XPath評估的麻煩。
這是一個解釋我問題的小例子:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
</book>
</bookstore>
以下是代碼:
static void Main(string[] args)
{
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"D:\simpleXml.xml");
System.Diagnostics.Trace.WriteLine(d.SelectSingleNode("//price/..[year=\'2005\']").Name);
}
我收到以下錯誤消息:附加信息:'//price /.. [year ='2005']'有一個無效的令牌。
對我來說,它似乎是一個有效的XPath表達式,因為像XMLSpy這樣的其他工具會成功地評估該表達式。
為什么不使用linq2xml
XDocument doc=XDocument.Load("yourXML");
var bookPrice=doc.Descendants("book")
.Where(x=>x.Element("year").Value=="2005")
.Select(y=>y.Element("price").Value);
//gets the price of the books published in 2005
如果你想要xpath版本,就在這里
"bookstore/book[year='2005']/*/../price"
//gets the price of the books published in 2005
如果您查看http://www.w3.org/TR/xpath/#NT-Step上的XPath規范,我們可以看到Step生產定義為:
Step ::= AxisSpecifier NodeTest Predicate*
| AbbreviatedStep
換句話說,謂詞不能遵循縮寫步驟,例如。 或者......我認為實施是(嚴格地)正確的。 也許XMLSpy在解釋上有點自由,只是總是擴展到parent:node()?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.