Search XML in powershell with colon
I have an XML in the format:
<val:root>
<bla>
<value>1234</value>
</val:root>
I can do get-content of the file to an $xml variable, but I'm having trouble with the "val:root".
Thanks for any help!
Edit:
I tried $xml."val:root", $xml.val, $xml.'val:root' and $xml.{val:root}. Found t开发者_高级运维he solution in Mizo's answer:
$xml.root.value
Your XML file is missing a declaration for the val
namespace. Also, the <bla>
element is unterminated.
Place a 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
val
namespace in a 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 a relevant identifier if you wish.
So I'm going to take a stab at the issue even without all the info. Based on the sample XML that you provided, your XML is malformed. You have at least two issues. The first is the tag without a matching close tag. The second is that you are using a namespace without declaring it. To address this issue, change this:
<val:root>
To this:
<val:root xmlns:val="http://www.w3.org/TR/html4/">
Or use a more appropriate URI if you would prefer.
You just need to define the namespace(s) when you load the XML file.
http://huddledmasses.org/xpath-and-namespaces-in-powershell/
精彩评论