Relatively new to XSLT.
Here's a sample XML:
<Shipments>
<Shipment>
<ErpOrder>415580479</ErpOrder>
<Containers>
<ShippingContainer>
<TreeUnit>12211620</TreeUnit>
<WeightUm>L</WeightUm>
</ShippingContainer>
</Containers>
</Shipment>
<Shipment>
<ErpOrder>415580479</ErpOrder>
</Shipment>
<Shipment>
<ErpOrder>5124516</ErpOrder>
</Shipment>
</Shipments>
I want to copy the <Containers>
node and all child tag within that node to any <Shipment>
tag that matches the <ErpOrder>
value and is missing the <Containers>
node.
Output:
<Shipments>
<Shipment>
<ErpOrder>415580479</ErpOrder>
<Containers>
<ShippingContainer>
<TreeUnit>12211620</TreeUnit>
<WeightUm>L</WeightUm>
</ShippingContainer>
</Containers>
</Shipment>
<Shipment>
<ErpOrder>415580479</ErpOrder>
<Containers>
<ShippingContainer>
<TreeUnit>12211620</TreeUnit>
<WeightUm>L</WeightUm>
</ShippingContainer>
</Containers>
</Shipment>
<Shipment>
<ErpOrder>5124516</ErpOrder>
</Shipment>
</Shipments>
What I have so far, but I think is completely off [EDITED]:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml"/>
<xsl:key name = "erpordermatch" match = "Shipment" use = "@ErpOrder"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Shipment[not(Containers)]">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:for-each select="key('erpordermatch', @ErpOrder)">
<xsl:if test="@ErpOrder = ErpOrder">
<xsl:copy-of select="Containers"/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>