在 Powershell 中如何获取 Select-Xml 来搜索多个节点

In Powershell how do I get Select-Xml to search multiple Nodes

所以我得说我是使用 PowerShell 解析 XML 的新手。话虽如此,如何结合多个 -XPath 以便我可以完成构建表达式报告。请告诉我,我已经尝试了几种组合,其中 none 似乎可以与命名空间 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

我不明白为什么您需要同时找到修补程序和 release-nodes。 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 属于哪个版本。