rea*_*art 2 .net c# xml xpath xpath-2.0
Microsoft 的 System.Xml.XPath nuget-package 可用于 .NET 4.6,声称支持 XPath 1.0 和 2.0。De文档说描述了命名空间:
System.Xml.XPath 命名空间包含定义游标模型的类,用于作为 XQuery 1.0 和 XPath 2.0 数据模型的实例导航和编辑 XML 信息项。
在升级 Visual Studio、升级和我所有的项目到框架版本 4.6 之后,我仍然无法让最简单的 XPath-2.0 for-expression 工作。根据规范,它们应该可以工作。
我无法想象微软声称支持它实际上不支持的东西,所以显然我做错了。如何正确使用 XPath2 查询?
[TestMethod]
public void TestXPath2()
{
// The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the
// XQuery 1.0 and XPath 2.0 Data Model.
var expression = "for $x in /Root/Foo/Bar return $x";
var compiledExpression = System.Xml.XPath.XPathExpression.Compile(expression);
// throws XPathException: "for ... has an invalid token"
}
Run Code Online (Sandbox Code Playgroud)
PS我真正想要的是让这样的东西工作:
[TestMethod]
public void TestLibraryForCustomer1()
{
string xmlFromMessage = @"<Library>
<Writer ID=""writer1""><Name>Shakespeare</Name></Writer>
<Writer ID=""writer2""><Name>Tolkien</Name></Writer>
<Book><WriterRef REFID=""writer1"" /><Title>King Lear</Title></Book>
<Book><WriterRef REFID=""writer2"" /><Title>The Hobbit</Title></Book>
<Book><WriterRef REFID=""writer2"" /><Title>Lord of the Rings</Title></Book>
</Library>";
var titleXPathFromConfigurationFile = "./Title";
var writerXPathFromConfigurationFile = "for $curr in . return /Library/Writer[@ID=$curr/WriterRef/@REFID]/Name";
var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b => b.Key, b => b.Value);
Assert.AreEqual("Shakespeare", library["King Lear"]);
Assert.AreEqual("Tolkien", library["The Hobbit"]);
Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
}
[TestMethod]
public void TestLibraryForCustomer2()
{
string xmlFromMessage = @"<Library>
<Writer ID=""writer1"">
<Name>Shakespeare</Name>
<Book><Title>Sonnet 18</Title></Book>
</Writer>
<Writer ID=""writer2"">
<Name>Tolkien</Name>
<Book><Title>The Hobbit</Title></Book>
<Book><Title>Lord of the Rings</Title></Book>
</Writer>
</Library>";
var titleXPathFromConfigurationFile = "./Title";
var writerXPathFromConfigurationFile = "../Name";
var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b => b.Key, b => b.Value);
Assert.AreEqual("Shakespeare", library["Sonnet 18"]);
Assert.AreEqual("Tolkien", library["The Hobbit"]);
Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
}
public IEnumerable<KeyValuePair<string,string>> ExtractBooks(string xml, string titleXPath, string writerXPath)
{
var library = XDocument.Parse(xml);
foreach(var book in library.Descendants().Where(d => d.Name == "Book"))
{
var title = book.XPathSelectElement(titleXPath).Value;
var writer = book.XPathSelectElement(writerXPath).Value;
yield return new KeyValuePair<string, string>(title, writer);
}
}
Run Code Online (Sandbox Code Playgroud)
托马雷克正确指出:
所以我通过使用第三方 XPath 2 库 The XPath2 nuget package解决了这个问题。这允许像这样的表达式
for $c in . return ../Writer[@ID=$c/WriterRef/@REFID]/Name
Run Code Online (Sandbox Code Playgroud)
请注意,我需要使用从书到作者的相对路径。这并不能正常工作:
# does not work due to the absolute path
for $c in . return /Library/Writer[@ID=$c/WriterRef/@REFID]/Name
Run Code Online (Sandbox Code Playgroud)
供将来参考:此代码在安装 nuget 包后有效:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Wmhelp.XPath2;
namespace My.Library
{
[TestClass]
public class WmhelpTests
{
[TestMethod]
public void LibraryTest()
{
string xmlFromMessage = @"<Library>
<Writer ID=""writer1""><Name>Shakespeare</Name></Writer>
<Writer ID=""writer2""><Name>Tolkien</Name></Writer>
<Book><WriterRef REFID=""writer1"" /><Title>King Lear</Title></Book>
<Book><WriterRef REFID=""writer2"" /><Title>The Hobbit</Title></Book>
<Book><WriterRef REFID=""writer2"" /><Title>Lord of the Rings</Title></Book>
</Library>";
var titleXPathFromConfigurationFile = "./Title";
var writerXPathFromConfigurationFile = "for $curr in . return ../Writer[@ID=$curr/WriterRef/@REFID]/Name";
var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b => b.Key, b => b.Value);
Assert.AreEqual("Shakespeare", library["King Lear"]);
Assert.AreEqual("Tolkien", library["The Hobbit"]);
Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
}
[TestMethod]
public void TestLibraryForCustomer2()
{
string xmlFromMessage = @"<Library>
<Writer ID=""writer1"">
<Name>Shakespeare</Name>
<Book><Title>Sonnet 18</Title></Book>
</Writer>
<Writer ID=""writer2"">
<Name>Tolkien</Name>
<Book><Title>The Hobbit</Title></Book>
<Book><Title>Lord of the Rings</Title></Book>
</Writer>
</Library>";
var titleXPathFromConfigurationFile = "./Title";
var writerXPathFromConfigurationFile = "../Name";
var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b => b.Key, b => b.Value);
Assert.AreEqual("Shakespeare", library["Sonnet 18"]);
Assert.AreEqual("Tolkien", library["The Hobbit"]);
Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
}
public IEnumerable<KeyValuePair<string, string>> ExtractBooks(string xml, string titleXPath, string writerXPath)
{
var library = XDocument.Parse(xml);
foreach (var book in library.Descendants().Where(d => d.Name == "Book"))
{
var title = book.XPath2SelectElement(titleXPath).Value;
var writer = book.XPath2SelectElement(writerXPath).Value;
yield return new KeyValuePair<string, string>(title, writer);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1001 次 |
| 最近记录: |