我正在尝试探索和学习XSLT映射,虽然我不确定如何继续。
基本上我有多个有效载荷。
有效载荷1
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<header>
<quoteType>YQT</quoteType>
<salesOrg>5010</salesOrg>
<distributionChannel>00</distributionChannel>
<division>00</division>
<deliveryDateHeader>2022-09-29T00:00Z</deliveryDateHeader>
<shippingCondition>01</shippingCondition>
</header>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>SP</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>WE</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>RE</partnerFunction>
</partner>
<materialsAvailibility>
<itemNumber>110</itemNumber>
<materialNumber>188521</materialNumber>
</materialsAvailibility>
</root>
有效载荷2
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<header>
<quoteType>YQT</quoteType>
<salesOrg>5010</salesOrg>
<distributionChannel>00</distributionChannel>
<division>00</division>
<deliveryDateHeader>2022-09-29T00:00Z</deliveryDateHeader>
<shippingCondition>01</shippingCondition>
</header>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>SP</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>WE</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>RE</partnerFunction>
</partner>
<materialsAvailibility>
<itemNumber>10</itemNumber>
<materialNumber>115517</materialNumber>
</materialsAvailibility>
</root>
预期输出
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<header>
<quoteType>YQT</quoteType>
<salesOrg>5010</salesOrg>
<distributionChannel>00</distributionChannel>
<division>00</division>
<deliveryDateHeader>2022-09-29T00:00Z</deliveryDateHeader>
<shippingCondition>01</shippingCondition>
</header>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>SP</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>WE</partnerFunction>
</partner>
<partner>
<partnerNumber>100193</partnerNumber>
<partnerFunction>RE</partnerFunction>
</partner>
<materialsAvailibility>
<itemNumber>110</itemNumber>
<materialNumber>188521</materialNumber>
</materialsAvailibility>
<materialsAvailibility>
<itemNumber>10</itemNumber>
<materialNumber>115517</materialNumber>
</materialsAvailibility>
</root>
头节点和伙伴节点总是相同的,我只需要一起整理materialAvailability。有人能帮我提供一个有效的XSLT映射吗?我会试着研究特定行的作用。提前谢谢。
尝试以下powershell脚本。代码在没有根节点的情况下工作。
using assembly System.Collections
using assembly System.Xml
using assembly System.Xml.Linq
$inputFilename1 = "C:temptest.xml"
$inputFilename2 = "C:temptest1.xml"
$outputFilename = "C:temptest2.xml"
$rSettings = New-Object System.Xml.XmlReaderSettings
$rSettings.ConformanceLevel = [System.Xml.ConformanceLevel]::Fragment
$reader1 = [System.Xml.XmlReader]::Create($inputFilename1,$rSettings)
$nodes1 = New-Object System.Collections.Generic.List[System.Xml.Linq.XElement]
while($reader1.EOF -eq $False)
{
if($reader1.IsStartElement() -eq $False) {$reader2.MoveToContent()}
$nodes1.Add([System.Xml.Linq.XElement]::ReadFrom($reader1));
}
$reader2 = [System.Xml.XmlReader]::Create($inputFilename2,$rSettings)
$nodes2 = New-Object System.Collections.Generic.List[System.Xml.Linq.XElement]
while($reader2.EOF -eq $False)
{
if($reader2.IsStartElement() -eq $False) {$reader2.MoveToContent()}
$nodes2.Add([System.Xml.Linq.XElement]::ReadFrom($reader2));
}
$materialsAvailibility2 = [System.Linq.Enumerable]::Where($nodes2, [Func[object,bool]]{ param($x) $x.Name.LocalName -eq "materialsAvailibility"})
$materialsAvailibility2 = [System.Linq.Enumerable]::First($materialsAvailibility2)
$materialsAvailibility2 = [System.Xml.Linq.XElement]$materialsAvailibility2
#Write-Host $materialsAvailibility2
$nodes1.Add($materialsAvailibility2)
$sWriter = New-Object System.IO.StreamWriter($outputFilename)
$sWriter.WriteLine("<?xml version=""1.0"" encoding=""UTF-8"" ?>");
$wSettings = New-Object System.Xml.XmlWriterSettings
$wSettings.ConformanceLevel = [System.Xml.ConformanceLevel]::Fragment
$wSettings.Indent = $True;
$xWriter = [System.Xml.XmlWriter]::Create($sWriter,$wSettings);
foreach ($node1 in $nodes1)
{
#Write-Host $node1
$node1.WriteTo($xWriter);
#Write-Host "end"
}
$xWriter.Flush();
$xWriter.Close();
最简单的可能是
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="*, doc('payload2.xml')//materialsAvailability"/>
</xsl:copy>
</xsl:template>