I am trying to select and remove a single node from an XML document. Sample code and XML below:
[xml]$xml = Get-Content MyXml.xml
$xml.MigrationTable.Mapping[1].SelectSingleNode("DestinationSameAsSource")
This currently returns nothing. This answer shows a C# example for including the namespace when calling the SelectSingleNode() method.
How can I include the namespace with SelectSingleNode() in PowerShell?
XML:
<?xml version="1.0" encoding="UTF-16"?>
<MigrationTable xmlns="http://microsoft.com/GroupPolicy/GPOOperations/MigrationTable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Mapping>
<Type>LocalGroup</Type>
<Source>Group1#Contoso.local</Source>
<Destination>Group2#contoso.local</Destination>
</Mapping>
<Mapping>
<Type>Unknown</Type>
<Source>Network Service</Source>
<DestinationSameAsSource/>
</Mapping>
<Mapping>
<Type>Unknown</Type>
<Source>Local Service</Source>
<DestinationSameAsSource/>
</Mapping>
</MigrationTable>
The Powershell syntax for NameTable handling is pretty similar to C#. After loading the data, create a NamespaceManager based on the XML document.
To select elements, a dummy namespace prefix needs to be used, in this sample x is added and used in Xpath. Like so,
[xml]$xml = Get-Content MyXml.xml
$nsmgr = new-object Xml.XmlNamespaceManager($xml.NameTable)
$nsmgr.AddNameSpace("x", "http://microsoft.com/GroupPolicy/GPOOperations/MigrationTable")
$xml.MigrationTable.Mapping[1].SelectSingleNode("x:DestinationSameAsSource", $nsmgr)
Related
I have the following XML
<wmi xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd">
<wmiHeader>
<fileID>693401.20160229.130342.3541254</fileID>
<version>2.0.0</version>
<messageType>FSN</messageType>
<genDate>2016-02-29T13:03:42Z</genDate>
<from>
</from>
</wmiHeader>
<orderShipNotification>
<shipmentHeader dateTimeCreated="2016-02-29T13:03:42Z" requestNumber="2574445351883" />
<shipmentDetails actualShipmentDateTime="2016-02-29T12:18:54Z" carrierCode="XX" carrierMethodCode="XX-01">
<shipmentPackDetails trackingNumber="9361289672090007124848" trackingURL="https://example.com/go/TrackConfirmAction_input?qtc_tLabels1=323434">
<shipmentPackLineDetails requestLineNumber="1" partnerItemID="FXT-CC-LB" itemQtyShipped="1" />
</shipmentPackDetails>
</shipmentDetails>
</orderShipNotification>
</wmi>
I am getting error in Freemarker template when I am trying to access.
${orderShipNotification.shipmentDetails.#actualShipmentDateTime[0]!""}
If I delete the namespaces from the document it is working fine. I deleted the following content from the XML
xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd"
I did some investigation. The is a ftl directive. But it is still not clear how this will solve the problem. Please let me know how I can access the attributes.
http://freemarker.incubator.apache.org/docs/ref_directive_ftl.html#ref.directive.ftl
Start the template with
<#ftl ns_prefixes={"D":"http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment"}>
This sets the namespace as the default (D stands for default). Note that if you will also use XPath queries, there you will have to write out the D: before the element names (this is an XPath restriction).
This is documented here: http://freemarker.org/docs/xgui_imperative_learn.html
My powershell code actually reads data from an XML and stores the specific data to a csv file.
The XML file somewhat looks like below:
<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Targets>
<Target Name="\\bin\testBusiness.dll">
<Modules>
<Module Name="testing.dll" AssemblyVersion="1.0.1003.312" FileVersion="1.0.0.0">
<Metrics>
<Metric Name="Maintainability" Value="78" />
</Metrics>
</Module>
</Modules>
</Target>
</Targets>
</Report>
I need to extract only the "testing.dll" from the above XML code. The code I am using to do so is as below:
$testDLL = [regex]::matches($xmlfile[5], '<mod name=".*" ')[0].value -replace '<mod name="(.*)" ','$1'
#the above code line gets even the AssemblyVersion
$testAssembver = [regex]::matches($xmlfile[5], 'AssemblyVersion=".*" ')[0].value -replace 'AssemblyVersion=".*" ','$1'
I don't need AssemblyVersion to be concatenated to the "testing.dll (mod name in xml)" from the XML code.
Currently I get something like:
testing.dll" AssemblyVersion=1.0.1000.112"
I just need testing.dll, everything thereafter should be ommitted.
Please help.
Thanks,
Ashish
I don't think that regular expression is the best way to parse XML. Perhaps ou'd better use XMLDocument.
Using a better XML document :
<Dumy>
<Report Version="10.0"></Report>
<Goals>
<Name>"\\somepath\path.dll"</Name>
<Mods>
<Mod Name="testing.dll" AssemblyVersion="1.0.1000.112" FileVersion="1.0.0.1"></Mod>
</Mods>
</Goals>
</Dumy>
You can find your data like this :
$data = [XML](Get-Content "C:\temp\test.xml")
$data.Dumy.Goals.Mods.Mod.Name
Use XML as correctly suggested by JPBlanc. Then use XPath. Example, to extract the wanted value:
$data.selectnodes("//Modules/Module/#Name")
Note: has to be done in powershell.
I have searched and searched and have this one little problem that I can not seem to solve.
The page is in an unusual form of xml, and looks like this
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://foo.bar.moo.org/">WILBER</string>
This is the only thing on the page. the only part that will ever change is WILBER might be something else.
I have tried:
$site="http://lalaland.org/Getmoocow?input=$foo_name"
$data = (new-object System.Net.WebClient).DownloadString($site)
$foo = [regex]::Matches($data, '<string.*?>(.+)</string>') | % {$_.Captures[0].Groups[1].value}
and a few varients but no luck. the only thing I need is is what is located between the string tags
So, given the string that you presented,
[xml]$x = #"
>> <?xml version="1.0" encoding="utf-8" ?>
>> <string xmlns="http://foo.bar.moo.org/">WILBER</string>
>> "#
>>
$x.string.'#text'
WILBER
I need to parse a XML file from the website.I have went through some links like ray wenderlich,etc.,But first i need to know how the XML parsing is working. So i have decided to parse a simple xml file which is given below.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<emp id = "100" name = "Cyril">
<details>
<desc>This Employee is working in this company for the past 5 years
</desc>
<age>35</age>
</details>
</emp>
<emp id = "101" name = "Ram">
<details>
<desc>This Employee is working in this company for the past 3 years
</desc>
<age>28</age>
</details>
</emp>
</employees>
Can anyone help me ?
you can use NSXMLParser for the same and you need to implement its delegates where you will receive the parsed values.
Can somebody suggest me a template engine (preferably written in Java) that could generate any text that I like from given XML input?
StringTemplate, FreeMarker
How about XSLt? You may use JAXP to do the processing.
You can use XSLT, it is not restricted to generating only XML output. It is restricted to XML input. Use the xsl:output tag do define the type of output you will be generating.
E.g. to generate text output
<xsl:output method="text" encoding="UTF-8"/>
To generate XML output with indentation
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>