在Powershell脚本中使用SelectSingleNode提取值

时间:2017-01-13 16:19:33

标签: xml powershell selectsinglenode

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

我的问题:有没有办法只选择ANAME为&#34; A1&#34;的单个节点。在具有&#34; O2&#34;?

的ONAME的C节点下

1 个答案:

答案 0 :(得分:1)

SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1; <ONAME>位于同一层次结构中,因此XPath表达式<E1>永远不会匹配。这应该有效:

//ONAME//E1