加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何在PowerShell中使用带有命名空间的xpath访问元素?

发布时间:2020-12-16 23:50:02 所属栏目:百科 来源:网络整理
导读:电源外壳: $doc = new-object System.Xml.XmlDocument$doc.Load($filename)$items = Select-Xml -Xml $doc -XPath '//item'$items | foreach { $item = $_ write-host $item.name} 我没有输出 XML: ?xml version="1.0" encoding="UTF-8"?submission versio
电源外壳:
$doc = new-object System.Xml.XmlDocument
$doc.Load($filename)

$items = Select-Xml -Xml $doc -XPath '//item'
$items | foreach {
    $item = $_
    write-host $item.name
}

我没有输出

XML:

<?xml version="1.0" encoding="UTF-8"?>
<submission version="2.0" type="TREE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="TREE.xsd" xmlns="some/kind/of/tree/v1">
  <group>
    <item></item>
    <item></item>
    <item></item>
  </group>
<submission>
你有一些问题在继续.首先,您需要在XPath模式中指定命名空间,XML格式不正确(结束标记不是结束标记),而Select-Xml直接返回XmlInfo而不是XmlElement.试试这个:
$xml = [xml]@'
<submission version="2.0" type="TREE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="TREE.xsd" xmlns="some/kind/of/tree/v1">
  <group>
    <item></item>
    <item></item>
    <item></item>
  </group>
</submission>
'@

$ns = @{dns="some/kind/of/tree/v1"}
$items = Select-Xml -Xml $xml -XPath '//dns:item' -Namespace $ns
$items | Foreach {$_.Node.Name}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读