0

I have an XML document $books that uses namespaces, and I've tried to create an XML namespacemanager by using something like $ns = New-Object System.Xml.XmlNamespaceManager($books.NameTable). I'm not quite sure what I'm doing wrong after that though. I'm trying to use SelectSingleNode() to search the XML document for specific nodes I need, but all my XPath queries return null.

Here's a bit of the xml document:

<ernm:NewReleaseMessage xmlns:ernm="http://ddex.net/xml/ern/341" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" LanguageAndScriptCode="en" MessageSchemaVersionId="/ern/341" xs:schemaLocation="http://ddex.net/xml/ern/341 http://ddex.net/xml/ern/341/release-notification.xsd">
    <MessageHeader>
        <MessageId>123123</MessageId>
        <MessageRecipient>
            <PartyId>567567</PartyId>
                <PartyName>
                    <FullName>John Smith</FullName>
                </PartyName>
        </MessageRecipient>
    </MessageHeader>
</ernm:NewReleaseMessage>

Also, here's a bit of what I've tried so far in order to get SelectSingleNode() working correctly:

[xml]$books = Get-Content xmlpath.xml
$ns = New-Object System.Xml.XmlNamespaceManager($books.NameTable)
$ns.AddNamespace("ns", $books.DocumentElement.NamespaceURI)

$books.SelectSingleNode("//newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//ns:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//ernm:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//xmlns:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//xml:newreleasemessage", $ns) #returns null

I've also tried other nodes than newreleasemessage and it still comes back blank, I just wanted to provide some examples of what I've tried. What's the correct way of using namespaces here?

1 Answer 1

2

XPath queries are case-sensitive. This works:

$books.SelectSingleNode("//ns:NewReleaseMessage", $ns)
Sign up to request clarification or add additional context in comments.

2 Comments

Sweet lord I feel stupid. Thank you, have your upvote. Why isn't that plastered at the top of every single xpath introduction!?
Because it's only a problem for people who have spent the last few years writing in languages that are not case-sensitive, and they are in a minority.

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.