0

我是 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)

我的问题:有什么方法可以在 ONAME 为“O2”的 C 节点下仅选择 ANAME 为“A1”的单个节点?

4

1 回答 1

1

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

$xml.SelectSingleNode("//*[ns:ONAME='O2']/ns:E1/ns:ANAME[.='A1']", $nameSpace)
于 2017-01-13T17:56:11.693 回答