使用 PowerShell,如何使用多个命名空间
Using PowerShell, how can I use multiple namespaces
我想创建一个 XML 具有多个命名空间的文件,就像这样。我需要在标签的开头插入正确的前缀
<?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>
使用这个 powershell 代码
$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
只能这样做
<?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>
如何添加正确的前缀?
您是否尝试过像这样创建 $info 元素:
#Order node
$Info = $XMLFile.CreateElement("O2", "Info", "urn:osis:names:specification:gtr:schema:xsd:Info-2")
对我来说:
<?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>
已更新以解释如何为内部标签添加前缀:
您可以对内部标签使用相同的调用,该属性不会因为它在父节点中出现一次而被重复。
$ID = $XMLFile.CreateElement("O2", "ID", "urn:osis:names:specification:gtr:schema:xsd:Info-2")
我想创建一个 XML 具有多个命名空间的文件,就像这样。我需要在标签的开头插入正确的前缀
<?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>
使用这个 powershell 代码
$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
只能这样做
<?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>
如何添加正确的前缀?
您是否尝试过像这样创建 $info 元素:
#Order node
$Info = $XMLFile.CreateElement("O2", "Info", "urn:osis:names:specification:gtr:schema:xsd:Info-2")
对我来说:
<?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>
已更新以解释如何为内部标签添加前缀:
您可以对内部标签使用相同的调用,该属性不会因为它在父节点中出现一次而被重复。
$ID = $XMLFile.CreateElement("O2", "ID", "urn:osis:names:specification:gtr:schema:xsd:Info-2")