質問

I'm trying to generate an XML-File that looks like this:

<ns5:attributionRequest xmlns:ns2="http://www.ech.ch/xmlns/eCH-0011/3" xmlns="http://www.ech.ch/xmlns/eCH-0007/3" xmlns:ns4="http://www.ech.ch/xmlns/eCH-0010/3" xmlns:ns3="http://www.ech.ch/xmlns/eCH-0008/2" xmlns:ns5="http://www.ech.ch/xmlns/eCH-0083/1" xmlns:ns6="http://www.ech.ch/xmlns/eCH-0044/1" xmlns:ns7="http://www.ech.ch/xmlns/eCH-0090/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns8="http://www.ech.ch/xmlns/eCH-0006/2" xsi:schemaLocation="http://www.ech.ch/xmlns/eCH-0083/1 http://www.ech.ch/xmlns/eCH-0083/1/eCH-0083-1-1.xsd">
  <ns5:header>
    <ns5:senderId xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">PKsample</ns5:senderId>
  </ns5:header>
  <ns5:record>
    <ns5:localPersonId>
      <ns6:personIdCategory>gentle people</ns6:personIdCategory>
      <ns6:personId>10001</ns6:personId>
    </ns5:localPersonId>
    <ns5:lastname>Froidevaux</ns5:lastname>
    <ns5:firstNames>Jean</ns5:firstNames>
    <ns5:birthDate>
      <ns6:yearMonthDay>1945-08-13</ns6:yearMonthDay>
    </ns5:birthDate>
    <ns5:sex>1</ns5:sex>
    <ns5:nationality>
      <ns5:nationalityStatus>0</ns5:nationalityStatus>
    </ns5:nationality>
  </ns5:record>
</ns5:attributionRequest>

However, I'm having some difficulties with the namespaces. How can I define and use the namespaces ns5, ns6 and so on.

Here's what I have so far:

#------------------------------------------------------------#
# Function CreateRequest > Creates xml file for data request #
#------------------------------------------------------------#
Function CreateRequest()
{
    $XMLFilePath = "H:\Stuff\Dateien\Scripts\XML\GetSozVersID\Test\NewRequest.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", $null)

    $XMLFile.AppendChild($XMLDeclaration) | Out-Null

    #---RootObject
    $newAR = $XMLFile.CreateElement("attributionRequest"); 

    $newAR.SetAttribute("xmlns:ns2", "http://www.ech.ch/xmlns/eCH-0011/3")
    $newAR.SetAttribute("xmlns", "http://www.ech.ch/xmlns/eCH-0007/3")
    $newAR.SetAttribute("xmlns:ns4", "http://www.ech.ch/xmlns/eCH-0010/3")
    $newAR.SetAttribute("xmlns:ns3", "http://www.ech.ch/xmlns/eCH-0008/2")
    $newAR.SetAttribute("xmlns:ns5", "http://www.ech.ch/xmlns/eCH-0083/1")
    $newAR.SetAttribute("xmlns:ns6", "http://www.ech.ch/xmlns/eCH-0044/1")
    $newAR.SetAttribute("xmlns:ns7", "http://www.ech.ch/xmlns/eCH-0090/1")
    $newAR.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    $newAR.SetAttribute("xmlns:ns8", "http://www.ech.ch/xmlns/eCH-0006/2")
    $newAR.SetAttribute("xsi:schemaLocation", "http://www.ech.ch/xmlns/eCH-0083/1 http://www.ech.ch/xmlns/eCH-0083/1/eCH-0083-1-1.xsd")

    $XMLFile.AppendChild($newAR)
    #---

    #---Header
    $Header = $XMLFile.CreateElement("header") 

    $SenderID = $XMLFile.CreateElement("senderId")

    $SenderID.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema")
    $SenderID.SetAttribute("xsi:type", "xs:string")

    $SenderID.InnerText = "PKsample"

    $Header.AppendChild($SenderID)

    $newAR.AppendChild($Header)
    #---

    #---Record
    $Record = $XMLFile.CreateElement("record")

    $newAR.AppendChild($Record)
    #---

    #---LocalPersonID
    $LocPersID = $XMLFile.CreateElement("localPersonId")

    $PersIDCat = $XMLFile.CreateElement("personIdCategory")
    $PersID    = $XMLFile.CreateElement("personId")

    $PersIDCat.InnerText = "gentle people"
    $PersID.InnerText    = "10001"

    $LocPersID.AppendChild($PersIDCat)
    $LocPersID.AppendChild($PersID)

    $Record.AppendChild($LocPersID)
    #---

    #---CurrentValues
    $CurrVal = $XMLFile.CreateElement("currentValues")

    $LName   = $XMLFile.CreateElement("lastname")
    $FName   = $XMLFile.CreateElement("firstNames")
    $Gender  = $XMLFile.CreateElement("sex")
    $DOB     = $XMLFile.CreateElement("birthDate")
    $YMD     = $XMLFile.CreateElement("yearMonthDay")
    $Gender  = $XMLFile.CreateElement("sex")
    $Nat     = $XMLFile.CreateElement("nationality")
    $NatStat = $XMLFile.CreateElement("nationalityStatus")

    $LName.InnerText   = "Froidevaux"
    $FName.InnerText   = "Jean"
    $Gender.InnerText  = "1"
    $YMD.InnerText     = "1945-08-13"
    $NatStat.InnerText = "0"

    $Record.AppendChild($LName)
    $Record.AppendChild($FName)
    $Record.AppendChild($DOB)
    $Record.AppendChild($Gender)

    $DOB.AppendChild($YMD)

    $Record.AppendChild($Nat)

    $Nat.AppendChild($NatStat)
    #---

    $XMLFile.Save($XMLFilePath);
}

Which generates an XML-File like this:

<attributionRequest xmlns:ns2="http://www.ech.ch/xmlns/eCH-0011/3" xmlns="http://www.ech.ch/xmlns/eCH-0007/3" xmlns:ns4="http://www.ech.ch/xmlns/eCH-0010/3" xmlns:ns3="http://www.ech.ch/xmlns/eCH-0008/2" xmlns:ns5="http://www.ech.ch/xmlns/eCH-0083/1" xmlns:ns6="http://www.ech.ch/xmlns/eCH-0044/1" xmlns:ns7="http://www.ech.ch/xmlns/eCH-0090/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns8="http://www.ech.ch/xmlns/eCH-0006/2" schemaLocation="http://www.ech.ch/xmlns/eCH-0083/1 http://www.ech.ch/xmlns/eCH-0083/1/eCH-0083-1-1.xsd">
  <header>
    <senderId xmlns:xs="http://www.w3.org/2001/XMLSchema" type="xs:string">PKsample</senderId>
  </header>
  <record>
    <localPersonId>
      <personIdCategory>gentle people</personIdCategory>
      <personId>10001</personId>
    </localPersonId>
    <lastname>Froidevaux</lastname>
    <firstNames>Jean</firstNames>
    <birthDate>
      <yearMonthDay>1945-08-13</yearMonthDay>
    </birthDate>
    <sex>1</sex>
    <nationality>
      <nationalityStatus>0</nationalityStatus>
    </nationality>
  </record>
</attributionRequest>

It also ignores the namespace xsi: when defining the attribute xsi:type for the node "senderId" in the header.

Thanks in advance for any help.

Best regards

marius

役に立ちましたか?

解決

If you want to create elements with a particular namespace and prefix, you should use the version of CreateNamespace that takes a prefix and namespace URI:

$newAR = $XMLFile.CreateElement("ns5", "attributionRequest", "http://www.ech.ch/xmlns/eCH-0083/1"); 

If you do that, you should get the expected output.

他のヒント

I've not explored working with XML side of Powershell yet. But, If it is only to generate an XML like you have given in example, i would propose a different approach, which will provide more flexibility.

You can create a csv file which will serve as the input for XML generating script, like:

enter image description here

Then the script will be

## Formatting The XML

$scriptblock = {
@"
<ns5:attributionRequest xmlns:ns2="http://www.ech.ch/xmlns/eCH-0011/3" xmlns="http://www.ech.ch/xmlns/eCH-0007/3" xmlns:ns4="http://www.ech.ch/xmlns/eCH-0010/3" xmlns:ns3="http://www.ech.ch/xmlns/eCH-0008/2" xmlns:ns5="http://www.ech.ch/xmlns/eCH-0083/1" xmlns:ns6="http://www.ech.ch/xmlns/eCH-0044/1" xmlns:ns7="http://www.ech.ch/xmlns/eCH-0090/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns8="http://www.ech.ch/xmlns/eCH-0006/2" xsi:schemaLocation="http://www.ech.ch/xmlns/eCH-0083/1 http://www.ech.ch/xmlns/eCH-0083/1/eCH-0083-1-1.xsd">
  <ns5:header>
    <ns5:senderId xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">$($_.type)</ns5:senderId>
  </ns5:header>
  <ns5:record>
    <ns5:localPersonId>
      <ns6:personIdCategory>$($_.personIdCategory)</ns6:personIdCategory>
      <ns6:personId>$($_.personId)</ns6:personId>
    </ns5:localPersonId>
    <ns5:lastname>$($_.lastname)</ns5:lastname>
    <ns5:firstNames>$($_.firstNames)</ns5:firstNames>
    <ns5:birthDate>
      <ns6:yearMonthDay>$($_.yearMonthDay)</ns6:yearMonthDay>
    </ns5:birthDate>
    <ns5:sex>$($_.sex)</ns5:sex>
    <ns5:nationality>
      <ns5:nationalityStatus>$($_.nationalityStatus)</ns5:nationalityStatus>
    </ns5:nationality>
  </ns5:record>
</ns5:attributionRequest>
"@
}

## Creating New XMLs

Import-csv .\file.csv | ForEach-Object{ 
 $personId = $_.personId
 & $scriptblock | out-file ".\$personId.xml"}

Put the csv and the script in same folder. This will generate XMLs with PersionId id,e.g., 10001.xml, 20001.xml in the same folder.

This way you can create N number of XMLs without actually editing the script. Just keep adding the values in the CSV file.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top