0

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"?

3
  • It should go to Code Riview Commented Jan 13, 2017 at 17:38
  • 1
    @RanadipDutta Not really. There is a problem with the code that the OP wants resolved (see the last paragraph). Commented Jan 13, 2017 at 17:57
  • Oops.. Sorry @AnsgarWiechers: right.. I will check that thoroughly next time onwards. Commented Jan 14, 2017 at 6:37

1 Answer 1

1

<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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.