【问题标题】:How to navigate an XML document with Xpath using a namespace? [duplicate]如何使用命名空间通过 Xpath 导航 XML 文档? [复制]
【发布时间】:2021-03-21 17:48:44
【问题描述】:

来自w3 schools 站点的这个sample 很容易从xpath 中提取数据:

> 
> open bookstore
Database 'bookstore' was opened in 0.03 ms.
> 
> xquery /bookstore/book[@category="web"]/author
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<author>Erik T. Ray</author>
Query executed in 9.07 ms.
> 
> xquery .
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
Query executed in 1.04 ms.
> 

但这个xmlsomehow 不同:

> 
> open sample
Database 'sample' was opened in 0.03 ms.
> 
> xquery .
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="First Name">a</S>
      <S N="Last Name">b</S>
      <S N="Emails">a@b;b@a.com</S>
      <S N="Phones">123 456-8904</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
  <Obj RefId="1">
    <TNRef RefId="0"/>
    <MS>
      <S N="First Name">c</S>
      <S N="Last Name">c</S>
      <S N="Emails">e@f.com</S>
      <S N="Phones">123456-3532;563 346-3453</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
</Objs>
Query executed in 1.06 ms.
> 
> xquery /Objs

Query executed in 0.61 ms.
> 

这是由于namespace?对于上面的示例数据,我如何使用xpath“向下钻取”?

【问题讨论】:

  • 搜索“XPath 默认命名空间”可为您提供该问题的大约 1800 个答案。我相当随意地选择了其中一个作为副本。如果它不能解决您的所有问题,请查看其他一些问题。
  • 很公平,谢谢。

标签: xml xpath xquery xml-namespaces basex


【解决方案1】:

部分solution:

> 
> xquery /*:Objs
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="First Name">a</S>
      <S N="Last Name">b</S>
      <S N="Emails">a@b;b@a.com</S>
      <S N="Phones">123 456-8904</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
  <Obj RefId="1">
    <TNRef RefId="0"/>
    <MS>
      <S N="First Name">c</S>
      <S N="Last Name">c</S>
      <S N="Emails">e@f.com</S>
      <S N="Phones">123456-3532;563 346-3453</S>
      <S N="Company Name"/>
    </MS>
  </Obj>
</Objs>
Query executed in 1.12 ms.
> 

虽然不太清楚如何在此处使用xpath 进一步“深入”到子节点和属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2010-10-08
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多