XPath多个重复的匿名命名空间



我需要在类似于以下的XML文件中处理数据:

<?xml version="1.0" encoding="utf-8"?>
<bpr:release xmlns:bpr="http://www.bp.co.uk/product/release">
<bpr:contents>
<process id="1" name="Process 1" xmlns="http://www.bp.co.uk/product/process">
<process name="Process 1"></process>
</process>
<process id="2" name="Process 2" xmlns="http://www.bp.co.uk/product/process">
<process name="Process 1"></process>
</process>
<object id="1" name="Object 1" xmlns="http://www.bp.co.uk/product/process">
<process name="Object 1" ></process>
</object>
<work-queue id="1" name="Queue1" xmlns="http://www.bp.co.uk/product/work-queue"/>
<process-group id="1" xmlns="http://www.bp.co.uk/product/process-group">
<members>
<process id="1"/>
<process id="2"/>
</members>
</process-group>
<object-group id="1" xmlns="http://www.bp.co.uk/product/object-group">
<members>
<object id="1"/>
<object id="2"/>
</members>
</object-group>
</bpr:contents>
</bpr:release>

例如,我需要提取/bpr:release/bpr:contents/process/process节点进行分析,但我得到了null或此错误:

System.Xml.XPath.XPathException
HResult=0x80131943
Message=Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
Source=System.Xml

我看到该文件有多个匿名名称空间,并且同一名称空间使用了两次。我尝试过各种组合,例如:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlPath);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("bpr", "http://www.bp.co.uk/product/release");
nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpo", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpwq", "http://www.bp.co.uk/product/work-queue");
nsmgr.AddNamespace("bppg", "http://www.bp.co.uk/product/process-group");
nsmgr.AddNamespace("bpog", "http://www.bp.co.uk/product/object-group");
XmlNodeList p;
XmlNodeList r;
try { r = xmlDoc.SelectNodes("/bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("//bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("/release"); } catch { }
try { r = xmlDoc.SelectNodes("//release"); } catch { }
try { r = xmlDoc.SelectNodes("release"); } catch { }
try { p = xmlDoc.SelectNodes("process"); } catch { }
try { p = xmlDoc.SelectNodes("bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("//process"); } catch { }
try { p = xmlDoc.SelectNodes("//bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpr:release/bpr:contents/process/process"); } catch { }

获取bpr:releasebpr:contentsprocess/processobject/process等的每个子节点的正确XPath是什么。?如何使用XMLNamespaceManager

您正在向XmlNamespaceManager添加命名空间前缀声明,但在对SelectNodes()的调用中没有使用nsmgr

try { r = xmlDoc.SelectNodes("/bpr:release", nsmgr); } catch { }
^^^^^^^

相应地安排其他电话。

还要注意,您的第一个示例XPath,

/bpr:release/bpr:contents/process/process

应该是

/bpr:release/bpr:contents/bpp:process/bpp:process

根据您的声明:

nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");

最后,您应该在一些AddNamespace()调用中删除重复的前缀声明(例如bpp/bpo(。

另请参阅

  • XPath如何处理XML名称空间
  • "//"以及"/"在XPath中

相关内容

  • 没有找到相关文章

最新更新