【问题标题】:In Powershell how do I get Select-Xml to search multiple Nodes在 Powershell 中,如何让 Select-Xml 搜索多个节点
【发布时间】:2018-03-28 15:28:12
【问题描述】:

所以我不得不说我是使用 PowerShell 解析 XML 的新手。话虽如此,我该如何组合多个 -XPath 以便我可以完成构建我的表达式报告。请告诉我,我已经尝试了几种组合,但似乎都不适用于命名空间 XML。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" niaVersion="12.0.0.756" xmlns="http://something.com/something/hotfix/manifest">
    <releases>
        <release name="mid November 2017">
            <hotfixes>
                <hotfixref name="DE002" description="" defectSuite="n/a" supportEscalation="n/a" internalNotes="" customer="n/a">
                    <packages>
                        <package type="All" />
                    </packages>
                    <components>
                        <component type="" />
                        <component type="" />
                    </components>
                </hotfixref>
                <hotfixref name="DE5728" description="" defectSuite="DS001" supportEscalation="n/a" internalNotes="" customer="n/a">
                    <packages>
                        <package type="Full" />
                    </packages>
                    <components>
                        <component type="" />
                    </components>
                </hotfixref>
            </hotfixes>
        </release>
    </releases>
</manifest>




[xml]$xdoc=Get-Content $PSScriptRoot\Manifest.xml
$ns = @{test ="http://something.com/something/hotfix/manifest"}
$ver = Select-Xml -Xml $xdoc -XPath '//test:release' -Namespace $ns 
$hfu = Select-Xml -Xml $xdoc -XPath '//test:hotfixref' -Namespace $ns 
foreach ($v in $ver){
$v | Select-Object -ExpandProperty node 
 $hfu |  Select-Object -ExpandProperty node

【问题讨论】:

  • 您的 XML 是什么样的?请向我们展示我们需要什么来复制您的问题。 Creating an MCVE
  • 如果 XML 使用命名空间,我不知道您为什么需要查看 XML。 Select-Xml -Xml $xdoc -XPath 可以在单个节点上找到,但我只想问您是否知道一种将多个节点组合到 Xpath 的方法,请告诉我。
  • 它确实有助于在$ns 中指定正确的命名空间。更新的问题。不确定您对组合多个节点的意思。你有你想要什么(输出)的例子吗?
  • 嗯,这确实是要求我们提供一种方法来复制您所看到的问题。
  • 嗨 EBGreen,我可能对这个问题没有明确表示,我在上面的代码对于单个节点来说可以正常工作。这意味着如果我只想查看发布的所有值,它将向我显示所有值。如果我想查看修补程序的所有值,它将显示与这些标签关联的所有值。我想要做的是一个带有版本的标题行,然后它正下方将具有来自 hotfixref ex 的值。释放值 /r /n hfref DE001 blah blah blah

标签: powershell xml-namespaces select-xml


【解决方案1】:

我不明白为什么您需要同时找到修补程序和发布节点。 Release 是 hotfixes 的父节点,因此只需找到所有 release-nodes 并在循环中访问其子节点即可找到相关的 hotfixes。例如:

$xdoc = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" niaVersion="12.0.0.756" xmlns="http://something.com/something/hotfix/manifest">
    <releases>
        <release name="mid November 2017">
            <hotfixes>
                <hotfixref name="DE002" description="" defectSuite="n/a" supportEscalation="n/a" internalNotes="" customer="n/a">
                    <packages>
                        <package type="All" />
                    </packages>
                    <components>
                        <component type="" />
                        <component type="" />
                    </components>
                </hotfixref>
                <hotfixref name="DE5728" description="" defectSuite="DS001" supportEscalation="n/a" internalNotes="" customer="n/a">
                    <packages>
                        <package type="Full" />
                    </packages>
                    <components>
                        <component type="" />
                    </components>
                </hotfixref>
            </hotfixes>
        </release>
    </releases>
</manifest>
"@



$ns = @{test ="http://something.com/something/hotfix/manifest"}
$releases = Select-Xml -Xml $xdoc -XPath '//test:release' -Namespace $ns  
foreach ($r in $releases){
    "$($r.node.name) contains the following hotfixes:"
    $r.node.hotfixes.hotfixref | Select-Object Name, description, defectSuite
}

输出:

mid November 2017 contains the following hotfixes:

name   description defectSuite
----   ----------- -----------
DE002              n/a
DE5728             DS001

如果您确实希望一个 xpath-query 找到这两种类型,请使用 | (OR) 分隔 xpath-queries。例如:

Select-Xml -Xml $xdoc -XPath '//test:release|//test:hotfixref' -Namespace $ns

Node      Path        Pattern
----      ----        -------
release   InputStream //test:release|//test:hotfixref
hotfixref InputStream //test:release|//test:hotfixref
hotfixref InputStream //test:release|//test:hotfixref

这样做的问题是您需要逻辑来检测您正在访问的节点类型,因为它可能是发布的,也可能是hotfixref。您还需要额外的逻辑来了解哪个 hotfixref 属于哪个版本。

【讨论】:

  • 谢谢,昨晚我得出了同样的结论,这是我写的一个 sn-p:` foreach( $n in $ns){ $items =Select-Xml -Xml $ xdoc -XPath '//e:release' -Namespace $n $items1 =Select-Xml -Xml $xdoc -XPath '//e:hotfixref' -Namespace $n } $values += $items + $items1 $values | Select-Object -ExpandProperty 节点 |选择名称,描述`
  • 我必须说我走在正确的道路上,但再次使用 PowerShell 和 XML 对我来说是一种新的体验。我写的大部分内容都涉及搜索日志和构建其他工具来解决问题。
  • 你会到达那里的。我不明白你为什么要循环$ns。它不起作用,因为它是一个哈希表,而且你只有一个命名空间,所以不需要循环
  • 如果您需要一个脚本来搜索将构建 unc 路径的“选择字符串”,它将搜索任何远程服务器,无论驱动器号是什么,并找到您正在寻找的模式日志。我有那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
相关资源
最近更新 更多