Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML is:
XML
<order display="2204">
  <productgroup display="Kitchen Units" lookup="1">
    <supplier display="Chippendale" lookup="90">
      <product display="Base End Support Panel  Natural Oak" what="1" pos="1" ordercat="D" />
      <product display="600MM Highline Integrated Dishwasher Fascia" what="1" pos="2" ordercat="B" />
      <product display="600MM Highline Base Unit" what="1" pos="3" ordercat="D" />
      <product display="600MM Highline Integrated Fridge/Freeezer Fascia" what="1" pos="4" ordercat="D" />
      <product display="600MM 3 Drawer Pan Unit" what="1" pos="5" ordercat="D" />
      <product display="600MM Highline Base Unit" what="1" pos="6" ordercat="D" />
      <product display="500MM Highline Base Unit" what="1" pos="7" ordercat="D" />
      <product display="600MM Fridge/Freezer Housing Unit - Type 6" what="1" pos="8" ordercat="D" />
      <product display="600MM Double Oven Housing Unit - Type 2" what="1" pos="9" ordercat="D" />
      <product display="2650 Plinth Natural Oak Inc Seal Strip" what="1" pos="10" ordercat="D" />
      <product display="Worktop 600 Single P/F 4000 Black Brazil" what="1" pos="11" ordercat="D" />
      <product display="30MM Bullnose Sq Cornice 3600MM Natural" what="1" pos="12" ordercat="D" />
      <product display="10Mtr Roll Edging Tape Natural Oak" what="1" pos="13" ordercat="D" />
      <product display="Schock Stnd Inset 1.5 Bowl  Drainer Sink" what="1" pos="14" ordercat="D" />
      <product display="Schock Universal Plumbing Kit 1.5 Bowl" what="1" pos="15" ordercat="A" />
    </supplier>
  </productgroup>
  <productgroup display="Appliances" lookup="3">
    <supplier display="N/A*******N/A" lookup="130">
      <product display="CDA - Integrated Combination Fridge/freezer 70/30 A+ Rated" what="3" pos="1" ordercat="D" />
      <product display="CDA - Integrated Washing Machine" what="3" pos="2" ordercat="A" />
      <product display="CDA - Double Oven Bi Aa Rated Main Oven Touch Control Clock S/steel" what="3" pos="3" ordercat="D" />
      <product display="CDA - Five Burner Front Control 70cm Gas Hob Wok Ffd Stainless Steel" what="3" pos="4" ordercat="D" />
      <product display="CDA - 70cm Chimney Extractor Hood - Stainless Steel" what="3" pos="5" ordercat="B" />
      <product display="CDA - Fully Integrated Dishwasher 60 Cm A++Aa Rated" what="3" pos="6" ordercat="D" />
    </supplier>
  </productgroup>
</order>


XSL is
XML
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output encoding="iso-8859-1"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <!-- group -->
    <xsl:key name="level0" match="//product" use="@ordercat"/> 
    	
    <xsl:template match="/">
        <data>
            <xsl:attribute name="type">K8 order creation control file</xsl:attribute>
            <process><xsl:value-of select="count(/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1])"/></process>
            <xsl:for-each select="/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1]">
 
                <order>
                <xsl:attribute name="number"><xsl:number format="1" value="position()"/></xsl:attribute>
				
                <xsl:variable name="onrOrdCat" select="@ordercat" />
                <orderCategory><xsl:value-of select="$onrOrdCat"/></orderCategory>
 
                    <orderLines>
                    <xsl:apply-templates select="../product[@ordercat = $onrOrdCat]" />
                    </orderLines>
                </order>
            </xsl:for-each>
        </data>
    </xsl:template>
	
    <xsl:template match="product">
        <line>
        <xsl:attribute name="what"><xsl:value-of select="@what" /></xsl:attribute>
        <xsl:attribute name="pos"><xsl:value-of select="@pos" /></xsl:attribute>
        </line>
    </xsl:template>
</xsl:stylesheet>


Anticipated output
XML
<data type="K8 order creation control file">
<process>3</process>
<order number="1">
<orderCategory>D</orderCategory>
<orderLines>
<line what="1" pos="1" />
<line what="1" pos="3" />
<line what="1" pos="4" />
<line what="1" pos="5" />
<line what="1" pos="6" />
<line what="1" pos="7" />
<line what="1" pos="8" />
<line what="1" pos="9" />
<line what="1" pos="10" />
<line what="1" pos="11" />
<line what="1" pos="12" />
<line what="1" pos="13" />
<line what="1" pos="14" />
<line what="3" pos="1" />
<line what="3" pos="3" />
<line what="3" pos="4" />
<line what="3" pos="6" />
</orderLines>
</order>
<order number="2">
<orderCategory>B</orderCategory>
<orderLines>
<line what="1" pos="2" />
<line what="3" pos="5" />
</orderLines>
</order>
<order number="3">
<orderCategory>A</orderCategory>
<orderLines>
<line what="1" pos="15" />
<line what="3" pos="2" />
</orderLines>
</order>
</data>


What I have tried:

George Jonsson brilliantly gave me the XSL, however after further testing the grouping needs to be refined a little and needs to look at multiple levels and iterate through them. There could be multiple or single ordercat, multiple or single supplier, multiple or single product group and mixtures thereof. Any help really appreciated.
Posted
Updated 24-May-16 14:49pm
v4
Comments
George Jonsson 24-May-16 5:35am    
Can you also provide an example output.
Much easier that way.
MKM_Matt 24-May-16 5:42am    
Hi. The desired output from the example is:-

<process>1</process>
<order number="1">
<ordercategory>D
<orderlines>
<line what="1" pos="1" />
<line what="1" pos="2" />
<line what="1" pos="3" />
<line what="1" pos="4" />
<line what="1" pos="5" />
<line what="1" pos="6" />
<line what="1" pos="7" />
<line what="1" pos="8" />
<line what="1" pos="9" />
<line what="1" pos="10" />
<line what="1" pos="11" />
<line what="1" pos="12" />
<line what="1" pos="13" />
<line what="1" pos="14" />
<line what="1" pos="15" />
<line what="3" pos="1" />
<line what="3" pos="2" />
<line what="3" pos="3" />
<line what="3" pos="4" />
<line what="3" pos="5" />
<line what="3" pos="6" />


George Jonsson 24-May-16 5:47am    
I will have a look tomorrow, if no one else have solved your problem.

1 solution

The problem was that you need to walk up the tree and select productgroup, then go down the tree again to find all lines.

Modified XSD
XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output encoding="iso-8859-1" />
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    <!-- group -->
    <xsl:key name="level0" match="//product" use="@ordercat" /> 
    	
    <xsl:template match="/">
        <data>
            <xsl:attribute name="type">K8 order creation control file</xsl:attribute>
            <process><xsl:value-of select="count(/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1])" /></process>
            <xsl:for-each select="/order/productgroup/supplier/product[count(. | key('level0', @ordercat)[1]) = 1]">
 
                <order>
					<xsl:attribute name="number"><xsl:number format="1" value="position()" /></xsl:attribute>
				
					<xsl:variable name="onrOrdCat" select="@ordercat" />
					<ordercategory><xsl:value-of select="$onrOrdCat" /></ordercategory>
 
                    <orderlines>
						<xsl:apply-templates select="../../../productgroup">
							<xsl:with-param name="onrOrdCat" select="$onrOrdCat" />
						</xsl:apply-templates>
                    </orderlines>
                </order>
            </xsl:for-each>
        </data>
    </xsl:template>
	
	<xsl:template match="productgroup">
		<xsl:param name="onrOrdCat" />
		<xsl:apply-templates select="supplier">
			<xsl:with-param name="onrOrdCat" select="$onrOrdCat" />
		</xsl:apply-templates>
	</xsl:template>
    
	<xsl:template match="supplier">
		<xsl:param name="onrOrdCat" />
		<xsl:apply-templates select="product[@ordercat = $onrOrdCat]" />
	</xsl:template>

	<xsl:template match="product">
        <line>
			<xsl:attribute name="what"><xsl:value-of select="@what" /></xsl:attribute>
			<xsl:attribute name="pos"><xsl:value-of select="@pos" /></xsl:attribute>
        </line>
    </xsl:template>
</xsl:stylesheet>
 
Share this answer
 
Comments
MKM_Matt 25-May-16 4:20am    
You star. Really appreciate the help and patience... Will try to improve my XML skills!! Thanks again. Matt
George Jonsson 25-May-16 6:59am    
Thanks and you are welcome.
Well, I learned a thing or two as well.
Never tried to group things before.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900