如何使用命名空间使用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.
> 

尽管不太清楚如何进一步";向下钻取";其中CCD_ 6在此用于子节点和属性。

最新更新