在Powershell中如何让Select-Xml搜索多个节点

时间:2018-03-28 15:28:13

标签: powershell xml-namespaces select-xml

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

frame['B'] = frame['B'].apply(lambda x: pd.to_numeric(x, errors='coerce')

1 个答案:

答案 0 :(得分:0)

我无法理解为什么你需要找到修补程序和发布节点。 Release是修补程序的父级,因此只需查找所有发布节点并在循环中访问它的子节点即可找到相关的修补程序。例如:

$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查询来查找这两种类型,那么使用|(OR)来分隔xpath查询。例如:

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

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