xmlpowershellselectsinglenode

Extracting values using SelectSingleNode in Powershell script


I am new to powershell and am trying to write my first script. Please review the following and see if you can provide any suggestions/

Here is my 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>

Here is Powershell script:

#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)

My Question: 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"?


Solution

  • <ONAME> and <E1> are on the same level of hierarchy, hence an XPath expression //ONAME//E1 can never match. This should work:

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