如何使用xPath从命名空间的xml中检索特定元素

时间:2013-03-14 15:15:19

标签: xml vb.net namespaces

我有一个关于从命名空间的xml中检索节点的问题。只要我在查询中指定xml的单个元素,我就可以检索所有匹配的元素,并且可以迭代它们的子元素。当我尝试通过指定更详细的查询来检索单个唯一元素时,我的问题出现了。

我用于测试的XML文件

<?xml version="1.0" encoding="utf-8"?>
<<MTConnectStreams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectStreams:1.2 http://mtconnect.org/schemas/MTConnectStreams_1.2.xsd" xmlns="urn:mtconnect.org:MTConnectStreams:1.2">
  <Streams>
    <Samples>
      <Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="x_pos_cmd" subType="COMMANDED" units="MILLIMETER">1</Position>
      <Position dataItemId="xs5" timestamp="2012-11-29T12:30:19Z" sequence="1718270" name="x_pos_act" subType="ACTUAL" units="MILLIMETER">2</Position>
      <Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="y_pos_cmd" subType="COMMANDED" units="MILLIMETER">3</Position>
      <Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="y_pos_act" subType="COMMANDED" units="MILLIMETER">4</Position>
      <Position dataItemId="xs4" timestamp="2012-11-29T12:30:19Z" sequence="1718269" name="z_pos_cmd" subType="COMMANDED" units="MILLIMETER">5</Position>
    </Samples>
    <Condition>
      <Unavailable dataItemId="xc1" type="POSITION" timestamp="2012-11-27T23:19:40Z" sequence="7" name="x_cond_pos">UNAVAILABLE</Unavailable>
      <Unavailable dataItemId="xc2" type="LOAD" timestamp="2012-11-27T23:19:40Z" sequence="8" name="x_cond_load">UNAVAILABLE</Unavailable>
    </Condition>
  </Streams>
</MTConnectStreams>

给出以下代码:

Dim xIt As XPathNodeIterator
Dim xDoc As XmlDocument = New XmlDocument
xDoc.Load("c:\Download\Test.xml")
Dim xNav As XPathNavigator = xDoc.CreateNavigator
Dim nsMgr = New XmlNamespaceManager(xNav.NameTable)
nsMgr.AddNamespace("ns", "urn:mtconnect.org:MTConnectStreams:1.2")

sQuery = "//ns:Samples"
xIt = xNav.Select(sQuery, nsMgr)
MsgBox(xIt.Count.ToString)

When sQuery = "//ns:Samples" (1 result)
When sQuery = "//ns:Position" (5 results)
When sQuery = "//ns:Samples/Position (0 results  why?)
When sQuery = "//ns:Samples/Position[@name='x_pos_act'] (0 results)
When sQuery = "//ns:Position[@name='x_pos_act']" (0 results)
When sQuery = "//ns:Position/[@name='x_pos_act']" (0 results)

等等。每当查询不止一个节点名称时,我就得不到任何结果。我无法弄清楚如何指定更详细的查询。

1 个答案:

答案 0 :(得分:1)

它应该使用//ns:Samples/ns:Position

在XPath中,您必须在每个子部分中指定名称空间前缀。这也适用于属性!

所以试试:

When sQuery = "//ns:Samples"
When sQuery = "//ns:Position"
When sQuery = "//ns:Samples/ns:Position
When sQuery = "//ns:Samples/ns:Position[@ns:name='x_pos_act']
When sQuery = "//ns:Position[@ns:name='x_pos_act']"
When sQuery = "//ns:Position/[@ns:name='x_pos_act']"

如果您不想考虑名称空间,也可以使用@*[local-name()='Samples']语法。这将忽略所有名称空间,并仅检查节点名称本身。