1

I am trying to select a specific node in an xml file for the purposes of comparing it to the same node in another file.

I can navigate through the file using the generic nodes, but I can't figure out how to get an exact Xpath.

Here's the XML:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Domain wide values -->
  <PropertyGroup>
    <LoggingLevel>0</LoggingLevel>
    <LongTimeouts>0</LongTimeouts>
  </PropertyGroup>
  <ItemGroup>
    <!--AB-->
    <Token Include="{AB_ANALYTICSSERVER}">
      <Value>Test</Value>
    </Token>
    <Token Include="{AB_CERTFINDVALUE}">
      <Value>$(CertName)</Value>
    </Token>
    <Token Include="{AB_SERVICECERTFINDVALUE}">
      <Value>$(CertName)</Value>
    </Token>
    <Token Include="{AB_CONSUMERSGROUP}">
      <Value>DLG-AB-users</Value>
    </Token>
    <!--AB End-->
    <!--Database-->
    <Token Include="{Database_Domain}">
      <Value>$(Domain)</Value>
    </Token>
    <Token Include="{Audit_Domain}">
      <Value>$(Domain)</Value>
    </Token>
    <!--Database End-->
  </ItemGroup>
</Project>

But I am clearly missing something in my xpath based queries:

e.g. $xmlNew.SelectNodes("/node()[1]/node()[3]/node()[2]").outerxml

Returns:

<Token Include="{AB_ANALYTICSSERVER}" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Value>Test</Value></Token>

But neither of these return anything:

$xmlNew.SelectNodes("/Project/ItemGroup/Token").outerxml

$xmlnew.selectnodes('//token[contains(@Include,"{AB_ANALYTICSSERVER}")]').OuterXml

Similarly this works:

$xmlNew.SelectNodes("/node()[1]/node()[2]/node()[2]").outerxml

but this doesn't:

$xmlnew.selectnodes('/Project/PropertyGroup/LongTimeouts').outerxml doesnt.

What obvious thing am i missing? :/

Thanks

2
  • The default namespace set on the Project element. Commented May 15, 2019 at 14:09
  • Looks like you would have to call .addnamespace() stackoverflow.com/questions/54352461/… Commented Aug 22, 2024 at 13:05

1 Answer 1

2

The problem is that your XPath isn't valid because of the namespace.

You can abuse PowerShell's power however and simply access Token as if it was an object structure.

[xml]$xmlNew= Get-Content "<path-to-xml-file>"
$xmlNew.Project.ItemGroup.Token.outerxml

You can also use Select-Xml cmdlet and specify the namespace explicitly with your XPath as it is shown in this answer.

$ns = @{dns = 'http://schemas.microsoft.com/developer/msbuild/2003'}
Select-Xml -Xml $xmlNew -XPath '//dns:Token' -Namespace $ns
Sign up to request clarification or add additional context in comments.

3 Comments

Forgive my ignorance, but i tried to literally follow the example in your link, but couldn't get it to work: $ns =@{dns="schemas.microsoft.com/developer/msbuild/2003"} $tokens = Select-Xml -Xml $xmlnew -XPath '//dns:token' -Namespace $ns $tokens | Foreach {$_.Node.Name}
@MalcolmLock Probably a case-sensitivity issue in your XPath, I added a working example in my answer.
Thanks! I'm still having trouble getting selectnodes and selectsinglenode to work with the NS, but i think this gets me what i need.

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.