在 Powershell 脚本中使用 SelectSingleNode 提取值

Extracting values using SelectSingleNode in Powershell script

我是 powershell 的新手,正在尝试编写我的第一个脚本。请查看以下内容,看看您是否可以提供任何建议/

这是我的TEST.text

<?xml version="1.0" encoding="UTF-8"?>
<A xmlns="http://www.w3.org/HTML/1998/html4"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <B>
        <C>
            <DESCRIPTION>This is O1</DESCRIPTION>
            <ONAME>O1</ONAME>
            <D>
                <TNAME>T1</TNAME>
            </D>
            <E1>
                <ANAME>A1</ANAME>
            </E1>
            <E1>
                <ANAME>A2</ANAME>
            </E1>
            <E1>
                <ANAME>A3</ANAME>
            </E1>
            <E2>
                <NAME>N1</NAME>
                <F>
                    <INAME>I1</INAME>
                </F>
                <F>
                    <INAME>I2</INAME>
                </F>
            </E2>
            <E2>
                <NAME>N2</NAME>
                <F>
                    <INAME>I1</INAME>
                </F>
                <F>
                    <INAME>I2</INAME>
                </F>
            </E2>
        </C>
        <C>
            <DESCRIPTION>This is O2</DESCRIPTION>
            <ONAME>O2</ONAME>
            <D>
                <TNAME>T2</TNAME>
            </D>
            <E1>
                <ANAME>A1</ANAME>
            </E1>
            <E1>
                <ANAME>A2</ANAME>
            </E1>
            <E1>
                <ANAME>A3</ANAME>
            </E1>
            <E2>
                <NAME>N1</NAME>
                <F>
                    <INAME>I1</INAME>
                </F>
                <F>
                    <INAME>I2</INAME>
                </F>
                <F>
                    <INAME>I3</INAME>
                </F>
            </E2>
            <E2>
                <NAME>N1</NAME>
                <F>
                    <INAME>I1</INAME>
                </F>
                <F>
                    <INAME>I2</INAME>
                </F>
                <F>
                    <INAME>I3</INAME>
                </F>
            </E2>
        </C>
    </B>
</A>

这里是 Powershell 脚本:

#Run the PowerShell with Run As Administrator:
#Set-ExecutionPolicy -ExecutionPolicy Unrestricted
#.\TEST.ps1 TEST.xml

param([string]$xmlFile)

$currentPath=Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
[xml]$xml = Get-Content $currentPath$xmlFile -Raw

# If a Namespace URI was not given, use the Xml document's default namespace.
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $xml.DocumentElement.NamespaceURI }   
#$NamespaceURI #-- Used for Debugging
$nameSpace = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) 
$nameSpace.AddNamespace("ns", $NamespaceURI)

#This works but selects the single node with the ANAME of "A1" 
#under both the nodes that has the ONAME of "O2" and the ONAME of "O1" 
$XMLoaName = $xml.SelectSingleNode("//ns:ONAME[.='O2']", $nameSpace)

#THIS DOES NOT WORK - Is there any way to select ONLY the single node with the ANAME of "A1" 
#under the C node that has the ONAME of "O2"? 
$XMLoaName = $xml.SelectSingleNode("//ns:ONAME[.='O2']//ns:E1//ns:ANAME[.='A1']", $nameSpace)

我的问题:有什么方法可以select只有ONAME为"O2"的C节点下ANAME为"A1"的单个节点?

<ONAME><E1> 处于同一层级,因此 XPath 表达式 //ONAME//E1 永远无法匹配。这应该有效:

$xml.SelectSingleNode("//*[ns:ONAME='O2']/ns:E1/ns:ANAME[.='A1']", $nameSpace)