【发布时间】:2015-07-06 11:03:52
【问题描述】:
我正在尝试用使用相同 XPath 从另一个文档中选择的节点替换一个文档中的节点。我正在使用 Saxon XPath 选择器从两个文档中选择节点,但我没有找到任何关于如何替换节点的线索。
假设我的 XPath 是:/bookstore/book,以下是我迄今为止编写的一段代码,用于从源文档和目标文档访问节点。
Processor SaxonProcessor = new Processor();
XPathCompiler Compiler = SaxonProcessor.NewXPathCompiler();
XmlDocument xmlDocumentTarget = new XmlDocument();
xmlDocumentTarget.LoadXml(targetXml);
DocumentBuilder sBuilder = SaxonProcessor.NewDocumentBuilder();
var sourceNode = sBuilder.Wrap(xmlDocumentSource);
XmlDocument xmlDocumentTarget = new XmlDocument();
xmlDocumentTarget.LoadXml(targetXml);
DocumentBuilder sBuilder = SaxonProcessor.NewDocumentBuilder();
var sourceDoc = sBuilder.Wrap(xmlDocumentSource);
DocumentBuilder tBuilder = SaxonProcessor.NewDocumentBuilder();
var targetDoc = tBuilder.Wrap(xmlDocumentTarget);
string xPath = @"/bookstore/book";
var sourceExp = Compiler.Compile(xPath).Load();
sourceExp.ContextItem = sourceDoc;
var targetExp = Compiler.Compile(xPath).Load();
targetExp.ContextItem = targetDoc;
var sourceXdmValue = sourceExp.Evaluate(); // Gives me source nodes
var targetXdmValue = targetExp.Evaluate(); // Gives me target nodes to replace
现在我想用源文档中的节点替换目标文档中的节点。如何使用 Saxon API 做到这一点?
请注意,在目标文档中,不包括具有“my”命名空间的书,因为不会根据提供的 XPath 从源中选择该书。
源 XML
<bookstore specialty="novel">
<book style="autobiography">
<author>
<first-name>Joe</first-name>
<last-name>Bob</last-name>
<award>Trenton Literary Review Honorable Mention</award>
</author>
<price>12</price>
</book>
<book style="textbook">
<author>
<first-name>Mary</first-name>
<last-name>Bob</last-name>
<publication>Selected Short Stories of
<first-name>Mary</first-name>
<last-name>Bob</last-name>
</publication>
</author>
<editor>
<first-name>Britney</first-name>
<last-name>Bob</last-name>
</editor>
<price>55</price>
</book>
<book style="novel" id="myfave">
<author>
<first-name>Toni</first-name>
<last-name>Bob</last-name>
<degree from="Trenton U">B.A.</degree>
<degree from="Harvard">Ph.D.</degree>
<award>Pulitzer</award>
<publication>Still in Trenton</publication>
<publication>Trenton Forever</publication>
</author>
<price intl="Canada" exchange="0.7">6.50</price>
<excerpt>
<p>It was a dark and stormy night.</p>
<p>But then all nights in Trenton seem dark and
stormy to someone who has gone through what
<emph>I</emph> have.</p>
<definition-list>
<term>Trenton</term>
<definition>misery</definition>
</definition-list>
</excerpt>
</book>
<my:book xmlns:my="uri:mynamespace" style="leather" price="29.50">
<my:title>Who's Who in Trenton</my:title>
<my:author>Robert Bob</my:author>
</my:book>
</bookstore>
目标 XML
<bookstore specialty="novel">
<book style="History">
<author>
<first-name>Joe</first-name>
<last-name>Bob</last-name>
<award>Trenton Literary Review Honorable Mention</award>
</author>
<price>12</price>
</book>
</bookstore>
结果 XML
<bookstore specialty="novel">
<book style="autobiography">
<author>
<first-name>Joe</first-name>
<last-name>Bob</last-name>
<award>Trenton Literary Review Honorable Mention</award>
</author>
<price>12</price>
</book>
<book style="textbook">
<author>
<first-name>Mary</first-name>
<last-name>Bob</last-name>
<publication>Selected Short Stories of
<first-name>Mary</first-name>
<last-name>Bob</last-name>
</publication>
</author>
<editor>
<first-name>Britney</first-name>
<last-name>Bob</last-name>
</editor>
<price>55</price>
</book>
<book style="novel" id="myfave">
<author>
<first-name>Toni</first-name>
<last-name>Bob</last-name>
<degree from="Trenton U">B.A.</degree>
<degree from="Harvard">Ph.D.</degree>
<award>Pulitzer</award>
<publication>Still in Trenton</publication>
<publication>Trenton Forever</publication>
</author>
<price intl="Canada" exchange="0.7">6.50</price>
<excerpt>
<p>It was a dark and stormy night.</p>
<p>But then all nights in Trenton seem dark and
stormy to someone who has gone through what
<emph>I</emph> have.</p>
<definition-list>
<term>Trenton</term>
<definition>misery</definition>
</definition-list>
</excerpt>
</book>
</bookstore>
【问题讨论】:
-
我很惊讶在您的 XPath 表达式中看到反斜杠。 XPath 总是使用正斜杠。大概这意味着您在此处显示的代码实际上是不可执行的。
-
是的,这个示例代码是为了解释这个问题而写的。我已经替换了问题中的那些斜线