xml - 如何使用命名空间通过 Xpath 导航 XML 文档?
问题描述
该站点的示例w3 schools
很容易从中提取数据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.
>
但这xml
有点不同:
>
> 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
对于上面的示例数据,我如何“向下钻取” ?
解决方案
部分解决方案:
>
> 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
这里进一步“深入”到子节点和属性。
推荐阅读
- javascript - 使用 AJAX 和按钮提交的 Flask 上传文件不起作用
- css - 用线悬停挥动
- flutter - 如何在 Flutter Tabs 中添加圆点作为指示器?
- java - 将资源文件捆绑到 ecs 集群上使用的 java app jar 文件中
- laravel - 无法得到正确的结果 algolia
- servicenow - 是否可以在 servicenow 付费帐户中创建多个实例?
- azure-devops - Azure Devops api 获取保留的构建
- python-3.x - Python中的持续时间计算器
- reactjs - 比较两个对象列表,一旦匹配,则返回 React JS 中的对象列表
- javascript - 如何在 vuetify 的 v-data-table 中显示带有嵌套 v-data-table 的数组中的 JSON 数组?