我试图生成一个如下所示的XML文件:
<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>
但是,我在命名空间方面遇到了一些困难。如何定义和使用名称空间ns5,ns6等。
这是我到目前为止所拥有的:
#------------------------------------------------------------#
# 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);
}
生成这样的XML文件:
<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>
它还忽略了命名空间xsi:在为节点&#34; senderId&#34;定义属性xsi:type时在标题中。
提前感谢您的帮助。
祝你好运
的Marius
答案 0 :(得分:0)
我还没有探索过使用Powershell的XML方面。但是,如果只是生成一个像你在示例中给出的XML,我会提出一种不同的方法,这将提供更多的灵活性。
您可以创建一个csv文件,该文件将作为XML生成脚本的输入,例如:
然后脚本将是
## 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"}
将csv和脚本放在同一个文件夹中。这将在同一文件夹中生成带有PersionId id的XML,例如10001.xml,20001.xml。
这样,您可以创建N个XML而无需实际编辑脚本。 只需在CSV文件中添加值即可。
答案 1 :(得分:0)
如果要创建具有特定命名空间和前缀的元素,则应使用带有前缀和名称空间URI的CreateNamespace
版本:
$newAR = $XMLFile.CreateElement("ns5", "attributionRequest", "http://www.ech.ch/xmlns/eCH-0083/1");
如果你这样做,你应该得到预期的输出。