XML search in powershell with a colon

I have XML in the format:

<val:root> <bla> <value>1234</value> </val:root> 

I can do the contents of the $ xml variable file, but I'm having problems with "val: root".

Thanks for any help!

Edit:

I tried $ xml. "val: root", $ xml.val, $ xml.'val: root 'and $ xml. {val: root}. Found a solution in Miso's answer:

$ Xml. .value root

+4
source share
3 answers

Your XML file is missing an declaration for the val namespace. In addition, the <bla> element is not destroyed.

  • Place the namespace declaration in the <root> element:

     <val:root xmlns:val="urn:dummy"> 

    Then you can do

      PS C:\> $xml = [xml] (get-content test.xml) PS C:\Users\mizo\test> $xml.root.value 1234 
  • If you don't have control over the XML data, you can declare the namespace val in the dummy root as a workaround:

     $xml = [xml] ("<dummyroot xmlns:val='urn:dummy'>" + (get-content test.xml) + "</dummyroot>") 

    Then you can access the elements:

     PS C:\> $xml.dummyroot.root.value 1234 

Replace urn:dummy with the appropriate id if you want.

+5
source

So, I'm going to take a hit on this issue, even without any information. Based on the XML sample that you provided, your XML is incorrect. You have at least two questions. The first is a tag without the corresponding close tag. Secondly, you use a namespace without declaring it. To fix this problem, change this:

 <val:root> 

For this:

 <val:root xmlns:val="http://www.w3.org/TR/html4/"> 

Or use a more appropriate URI if you want.

+3
source

You just need to define the namespace when loading the XML file.

http://huddledmasses.org/xpath-and-namespaces-in-powershell/

-1
source

All Articles