Powershell:当元素具有“xmlns"时,XPath 无法选择标签? [英] Powershell: XPath cannot select when element has "xmlns" tag?

查看:24
本文介绍了Powershell:当元素具有“xmlns"时,XPath 无法选择标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 xml,如下所示:

I've got a very simple xml, as below:

<?xml version="1.0" encoding="utf-8"?>
<First>
  <Second>
    <Folder>today</Folder>
    <FileCount>10</FileCount>
  </Second>
  <Second>
    <Folder>tomorrow</Folder>
    <FileCount>90</FileCount>
  </Second>
  <Second>
    <Folder>yesterday</Folder>
    <FileCount>22</FileCount>
  </Second>
</First>

然后我有一个powershell脚本来选择文件夹"元素:

Then I have a powershell script to select "Folder" element:

[xml]$xml=Get-Content "D:\m.xml"
$xml.SelectNodes("//Folder")

它输出:

#text                                                                                                                                                                            
-----                                                                                                                                                                            
today                                                                                                                                                                            
tomorrow                                                                                                                                                                         
yesterday 

没问题.但是,如果我更改 xml 文件以将xmlns="http://schemas.microsoft.com/developer/msbuild/2003"添加到First",如下所示:

No problem. But if I change the xml file to add "xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to "First" like below:

<?xml version="1.0" encoding="utf-8"?>
<First xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Second>
    <Folder>today</Folder>
    <FileCount>10</FileCount>
  </Second>
  <Second>
    <Folder>tomorrow</Folder>
    <FileCount>90</FileCount>
  </Second>
  <Second>
    <Folder>yesterday</Folder>
    <FileCount>22</FileCount>
  </Second>
</First>

然后,我的 powershell 脚本不输出任何内容.为什么?如何更改我的 powershell 脚本以支持此 xmlns?

Then, my powershell script outputs nothing. Why? How to change my powershell script to support this xmlns?

非常感谢.

推荐答案

您添加的是默认命名空间.与带前缀的命名空间不同,后代元素隐式继承祖先默认命名空间,除非另有说明(使用显式前缀或指向不同 URI 的本地默认命名空间).

What you added is default namespace. Unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly, unless otherwise specified (using explicit prefix or local default namespace that points to different URI).

要选择命名空间中的元素,您需要定义指向命名空间 URI 的前缀,并在 XPath 中正确使用该前缀,例如:

To select element in namespace, you'll need to define prefix that point to the namespace URI and use that prefix properly in your XPath, for example :

$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectNodes("//d:Folder", $ns)

这篇关于Powershell:当元素具有“xmlns"时,XPath 无法选择标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆