Reputation: 65
I want to append child node into parent node.
Here is the XML. I want to append order_d into order_h if my order_no under order_h is equal to the following order_no in order_h
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<order>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783">
<Size_b assoc_id="69398" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
<size_d assoc_id="69398" ship_pack="BULK" ctn_sku="5602060" size_code="XS" alloc_qty="18" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
</Size_b>
</order_d>
</order_h>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783">
</order_d>
</order_h>
</order>
</document>
and i need output as below
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<order>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783"/>
</order_h>
</order>
</document>
Thanks in advance
Upvotes: 0
Views: 82
Reputation: 243469
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order_h|order_d/node()"/>
<xsl:template match="order_h[1]">
<xsl:copy>
<xsl:apply-templates select=
"@*|node()|following-sibling::*/node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (corrected to be made a well-formed XML document):
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<order>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783">
<Size_b assoc_id="69398" row_no="0" pct_ratio="PCT" total_pct="100" priority="0" ship_pack="BULK">
<size_d assoc_id="69398" ship_pack="BULK" ctn_sku="5602060" size_code="XS" alloc_qty="18" plan_pct_ratio="23.5" row_no="0" sort_value="5"/>
</Size_b>
</order_d>
</order_h>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783">
</order_d>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783">
</order_d>
</order_h>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783">
</order_d>
</order_h>
</order>
</document>
produces the wanted, correct result:
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<order>
<order_h owner="GD" order_no="421424" profoma_po="421425">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421424" color_code="730" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421424" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421424" color_code="257" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421425" profoma_po="421426">
<order_d owner="GD" item_no="100000014" line_no="1" order_no="421425" color_code="730" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="3" order_no="421425" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000014" line_no="2" order_no="421425" color_code="257" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421426" profoma_po="421427">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421426" color_code="000" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421426" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421426" color_code="450" manufacturer="100783"/>
</order_h>
</order>
<order>
<order_h owner="GD" order_no="421427" profoma_po="421428">
<order_d owner="GD" item_no="100000015" line_no="1" order_no="421427" color_code="000" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="3" order_no="421427" color_code="002" manufacturer="100783"/>
<order_d owner="GD" item_no="100000015" line_no="2" order_no="421427" color_code="450" manufacturer="100783"/>
</order_h>
</order>
</document>
Explanation:
Proper use of and overriding of the identity rule.
Upvotes: 1