debharlock
debharlock

Reputation: 69

Using PowerShell, how can I use multiple namespaces

I would like to create an XML file with multiple namespace like this. I need to insert the correct prefix at the beginning of the tag

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<HXT:Sending xmlns:HXT="http://www.HiTooT.com/HXT-Rec">   
     <O2:Info xmlns:O2="urn:osis:names:specification:gtr:schema:xsd:Info-2" xmlns:dad="urn:osis:names:specification:gtr:schema:xsd:AggregateInfo" xmlns:dbd="urn:osis:names:specification:gtr:schema:xsd:BasicInfo">
          <dad:ID>12015</dad:ID>
          <dbd:IssueDate>05032015</dbd:IssueDate>
          <dbd:TypeCode>ORA_PF</dbd:TypeCode>   
     </O2:Info> 
</HXT:Sending>

with this powershell code

$XMLFilePath = "c:\tmp\test1.xml"

    #---Create empty XML File
    New-Item $XMLFilePath -Type File -Force | Out-Null

    #---Creating Base Structure
    $XMLFile = New-Object XML
    [System.XML.XMLDeclaration]$XMLDeclaration = $XMLFile.CreateXMLDeclaration("1.0", "UTF-8", "yes")
    $XMLFile.AppendChild($XMLDeclaration) | Out-Null

    #---RootObject
    $Sending = $XMLFile.CreateElement("HXT", "Sending", "http://www.HiTooT.com/HXT-Rec")
    $XMLFile.AppendChild($Sending)


    #Order node
    $Info = $XMLFile.CreateElement("Info"); 

    $Info.SetAttribute("xmlns:O2", "urn:osis:names:specification:gtr:schema:xsd:Info-2")
    $Info.SetAttribute("xmlns:dad", "urn:osis:names:specification:gtr:schema:xsd:AggregateInfo")
    $Info.SetAttribute("xmlns:dbd", "urn:osis:names:specification:gtr:schema:xsd:BasicInfo")

    $Sending.AppendChild($Info)
    #---

    $ID    = $XMLFile.CreateElement("ID")
    $ID.InnerText    = "12015"
    $Info.AppendChild($ID)

    $IssueDate    = $XMLFile.CreateElement("cbc:IssueDate")
    $IssueDate.InnerText    = "05032015" 
    $Info.AppendChild($IssueDate)

    $TypeCode    = $XMLFile.CreateElement("TypeCode")
    $TypeCode.InnerText    = "ORA_PF"
    $Info.AppendChild($TypeCode)

    $XMLFile.Save($XMLFilePath);

    notepad $XMLFilePath

can only do this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HXT:Sending xmlns:HXT="http://www.HiTooT.com/HXT-Rec">
  <Info xmlns:O2="urn:osis:names:specification:gtr:schema:xsd:Info-2" xmlns:dad="urn:osis:names:specification:gtr:schema:xsd:AggregateInfo" xmlns:dbd="urn:osis:names:specification:gtr:schema:xsd:BasicInfo">
    <ID>12015</ID>
    <IssueDate>05032015</IssueDate>
    <TypeCode>ORA_PF</TypeCode>
  </Info>
</HXT:Sending>

How can i add the correct prefix?

Upvotes: 2

Views: 668

Answers (1)

JPBlanc
JPBlanc

Reputation: 72630

Have you try to create your $info element like this :

#Order node
$Info = $XMLFile.CreateElement("O2", "Info", "urn:osis:names:specification:gtr:schema:xsd:Info-2")

For me it gives :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HXT:Sending xmlns:HXT="http://www.HiTooT.com/HXT-Rec">
  <O2:Info xmlns:O2="urn:osis:names:specification:gtr:schema:xsd:Info-2" xmlns:dad="urn:osis:names:specification:gtr:schema:xsd:AggregateInfo" xmlns:dbd="urn:osis:names:specification:gtr:schema:xsd:BasicInfo">
    <ID>12015</ID>
    <IssueDate>05032015</IssueDate>
    <TypeCode>ORA_PF</TypeCode>
  </O2:Info>
</HXT:Sending>

Updated to explain how to prefix also inner tags :

You can use the same call for inner tags, the attribute will not be repated as it appear one time in the parents node.

$ID = $XMLFile.CreateElement("O2", "ID", "urn:osis:names:specification:gtr:schema:xsd:Info-2")

Upvotes: 2

Related Questions