首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能使用XPath访问这些节点内容?

为什么我不能使用XPath访问这些节点内容?
EN

Stack Overflow用户
提问于 2014-03-06 10:40:39
回答 1查看 491关注 0票数 1

我是XPathC#的新手,我有以下问题:

我必须解析这个文件:v2.3.xml

如您所见,在浏览器中打开它,该文件具有以下结构:

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8'?>
<cpe-list xmlns:meta="http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2" xmlns:config="http://scap.nist.gov/schema/configuration/0.1" xmlns:ns6="http://scap.nist.gov/schema/scap-core/0.1" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.3" xmlns="http://cpe.mitre.org/dictionary/2.0" xmlns:cpe-23="http://scap.nist.gov/schema/cpe-extension/2.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://scap.nist.gov/schema/configuration/0.1 http://nvd.nist.gov/schema/configuration_0.1.xsd http://cpe.mitre.org/dictionary/2.0 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary_2.3.xsd http://scap.nist.gov/schema/scap-core/0.3 http://nvd.nist.gov/schema/scap-core_0.3.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 http://nvd.nist.gov/schema/cpe-dictionary-metadata_0.2.xsd http://scap.nist.gov/schema/cpe-extension/2.3 http://scap.nist.gov/schema/cpe/2.3/cpe-dictionary-extension_2.3.xsd">
  <generator>
    <product_name>National Vulnerability Database (NVD)</product_name>
    <product_version>2.22.0-SNAPSHOT (PRODUCTION)</product_version>
    <schema_version>2.3</schema_version>
    <timestamp>2014-03-05T05:13:33.550Z</timestamp>
  </generator>
  <cpe-item name="cpe:/a:1024cms:1024_cms:0.7">
    <title xml:lang="en-US">1024cms.org 1024 CMS 0.7</title>
    <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:0.7:*:*:*:*:*:*:*"/>
  </cpe-item>
  <cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">
    <title xml:lang="en-US">1024cms.org 1024 CMS 1.2.5</title>
    <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.2.5:*:*:*:*:*:*:*"/>
  </cpe-item>
  <cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
    <title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
    <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
  </cpe-item>

  .............................................................
  .............................................................
  .............................................................

  <cpe-item name="cpe:/h:zyxel:p-660hw_t3:v2">
    <title xml:lang="en-US">ZyXEL P-660HW T3 Model v2</title>
    <cpe-23:cpe23-item name="cpe:2.3:h:zyxel:p-660hw_t3:v2:*:*:*:*:*:*:*"/>
  </cpe-item>
</cpe-list>

因此,现在,使用XPath,我必须获得所有标记的列表(不包括作为第一个标记的第一个标签到我的标签中)。

在我的代码中,我有类似的内容:

代码语言:javascript
复制
       XmlDocument document = new XmlDocument();    // Represent an XML document
       document.Load(sourceXML.FullName);       // Loads the XML document from the specified stream

       // Add the namespaces:
       XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
       nsmgr.AddNamespace("ns6", "http://scap.nist.gov/schema/scap-core/0.1");
       nsmgr.AddNamespace("cpe-23", "http://scap.nist.gov/schema/cpe-extension/2.3");
       nsmgr.AddNamespace("ns", "http://cpe.mitre.org/dictionary/2.0");
       nsmgr.AddNamespace("meta", "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2");
       nsmgr.AddNamespace("scap-core", "http://scap.nist.gov/schema/scap-core/0.3");
       nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
       nsmgr.AddNamespace("config", "http://scap.nist.gov/schema/configuration/0.1");



       /* nodeList is the collection that contains all the <cpe-item> tag that are
         * inside the root <cpe-list> tag in the XML document: 
         */
       XmlNodeList nodeList;
       nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
       long conta = 0;

因此,我使用这一行来选择标签中的所有标记:

代码语言:javascript
复制
nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);

它似乎有效,但我不确定它是否正确,因为当我查看使用Visual调试器时,它告诉我,我的XmlNodeList nodeList包含: 80588元素(文件非常大,但在我看来它似乎是许多元素!)

另一个疑问与ns命名空间的使用有关,该名称空间已进入我以前的代码中(这不是我的代码,我必须处理它)。

为什么在前面的代码中,前面有ns、名称空间、cpe列表、cpe项,如果在要解析的XML代码中,我几乎有以下内容:

代码语言:javascript
复制
  <cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
    <title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
    <cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.3.1:*:*:*:*:*:*:*"/>
  </cpe-item>

而不是以ns命名空间开头的?为什么要用它?

最后一个问题是如何访问标题内部文本内容?

我试着做这样的事,但这样做是行不通的:

代码语言:javascript
复制
XmlNodeList nodeList;
nodeList = document.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
long conta = 0;

DataModel.Vulnerability.CPE currentCPE;

foreach (XmlNode node in nodeList)
{
    // Access to the name ATTRIBUTE of the <cpe-item> tag:
    Debug.WriteLine(String.Format("[{0:N0}] CPE: {1}  Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value));

    // Access to the <title> tag content:
    //Debug.WriteLine(String.Format("[{0:N0}] Title: {1}  Title: {2}", conta, node.SelectSingleNode("./title", nsmgr)));
    XmlNode titleNode = node.SelectSingleNode("./title", nsmgr);

    conta++;
 }

在执行这段代码时,在我的列表中访问当前cpe元素的名称属性没有问题,但是我无法访问标记的内容,因为在执行这一行时:

代码语言:javascript
复制
 XmlNode titleNode = node.SelectSingleNode("./title", nsmgr);

它返回值为

有什么问题吗?我遗漏了什么?我该怎么解决呢?

Tnx

安德里亚

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-06 11:08:09

  1. 考虑到在这个问题中发布的XML片段,您的XPath看起来很好。据我所知,它应该返回正确的元素数。没有什么比这更能说明问题了,你应该自己检查一下。
  2. 您的XML具有默认名称空间(xmlns=".....")。在默认命名空间中不考虑前缀的XML中的所有元素。但是在XPath中,没有考虑前缀的所有元素都没有命名空间。最后,这两个平台的不同范例要求您定义指向默认名称空间url的ns前缀,以便在XPath语句中使用。
  3. 请记住,所有没有前缀的元素都是默认命名空间。<title>元素也是如此。因此,您需要在ns语句中添加XPath前缀:./ns:title
  4. 理想情况下,one post只需包含一个特定问题。在一个帖子里回答一堆问题对未来的访问者来说很少有用,反而会让他们感到困惑。请记住,我们不仅在这里解决您的问题,而且试图建立知识库,希望对其他有类似问题的人有用。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22221980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档