1

I have the following xml:

<?xml version="1.0" encoding="utf-8"?>
<userSettings>
  <setting name="TelephonyServerHost">
    <value>sipserver.domain.local</value>
  </setting>
  <setting name="SipServerFqdn">
    <value>sipserver.domain.local</value>
  </setting>
  <setting name="WebServicesHost">
    <value>websvc.domain.local</value>
  </setting>
  <setting name="KMSettings">
    <value>
      <KMIndexSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <AutoIndexEnabled>false</AutoIndexEnabled>
     </KMIndexSettings>
    </value>
  </setting>
</userSettings>

I am able to retrieve the values of the setting elements using xpath but I cannot figure out the correct syntax for querying the AutoIndexEnabled element using the namespace.

This works as expected for reading the KMSettings or other nodes which do not have a namespace:

$xml = New-Object -TypeName 'System.XML.XMLDocument'
$xml.Load($xmlFilePath)
$node = $xml.SelectSingleNode("//userSettings/setting[@name='KMSettings']")

But I can't figure out the syntax on how to query the AutoIndexEnabled element.

1
  • I'm not a PowerShell user, so won't be able to help with the PowerShell specific syntax, but in terms the xml, the AutoIndexEnabled element is within the xsi namespace, therefore you should use PowerShell's facilities to access the namespace and then the nodes within that namespace. Doing a quick search I've found this answer which looks like matches your issue Commented May 13, 2016 at 21:06

3 Answers 3

2

I don't understand the problem. The namespaces doesn't matter here because your xml-sample doesn't contain prefixed elements or a default namespace. You can access the element like this:

$xml.SelectNodes("//AutoIndexEnabled")

or

$xml.SelectNodes("//setting[@name='KMSettings']//AutoIndexEnabled")

Output:

#text
-----
false

PS> $xml.SelectNodes("//AutoIndexEnabled").InnerText
false
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I was getting confused with the xpath syntax.
2

Within PowerShell you can access XML nodes like properties, so this works:

($xml.DocumentElement.setting | ? name -eq 'KMSettings').value.KMIndexSettings.AutoIndexEnabled

And here is a working XPATH solution:

[string]$xpath="//userSettings/setting[@name='KMSettings']/value/KMIndexSettings/AutoIndexEnabled"       
$xml.SelectSingleNode($xpath)

3 Comments

Did the sample change? I can't see any SOAP-namespace and you're not even using it in your xpath.. :S
Upps, you are right @FrodeF. I just copied the solution from a previous answer. Thanks for the hint!
Thanks, your xpath helped.
0

I this particular example, how would you select the value of the attribute with name "xmlns:xsi"?

<value>
  <KMIndexSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ... >
    <AutoIndexEnabled>false</AutoIndexEnabled>
 </KMIndexSettings>
</value>

I would expect to see the output: "http://www.w3.org/2001/XMLSchema-instance"

This is what I'm trying.The colon is throwing off my script. I get the following error: "Unexpected token ':id' in expression or statement."

([xml](gc $files[$i])).contentHaul.constant.typedValue.value.a:id

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.