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


Into:-

XML
<?xml version="1.0" encoding="iso-8859-1"?>
<data type="K8 order creation control file">
	<process>3</process>
	<order number="1">
		<orderCategory>C</orderCategory>
		<orderLines>
			<line>
				<what>1</what>
				<pos>2</pos>
				<what>1</what>
				<pos>3</pos>
				<what>1</what>
				<pos>4</pos>
				<what>1</what>
				<pos>6</pos>
				<what>1</what>
				<pos>7</pos>
				<what>1</what>
				<pos>8</pos>
				<what>1</what>
				<pos>9</pos>
				<what>1</what>
				<pos>10</pos>
				<what>1</what>
				<pos>11</pos>
				<what>1</what>
				<pos>12</pos>
				<what>1</what>
				<pos>13</pos>
				<what>1</what>
				<pos>14</pos>
				<what>1</what>
				<pos>15</pos>
			</line>
		</orderLines>
	</order>
	<order number="2">
		<orderCategory>S</orderCategory>
		<orderLines>
			<line>
				<what>1</what>
				<pos>5</pos>
			</line>
		</orderLines>
	</order>
	<order number="3">
		<orderCategory>N</orderCategory>
		<orderLines>
			<line>
				<what>3</what>
				<pos>1</pos>
				<what>3</what>
				<pos>2</pos>
				<what>3</what>
				<pos>3</pos>
				<what>3</what>
				<pos>4</pos>
				<what>3</what>
				<pos>5</pos>
				<what>3</what>
				<pos>6</pos>
			</line>
		</orderLines>
	</order>
</data>



The xsl I have written gets the ordercat part grouped and the counts, but I am failing to get the what and pos parts mapped to the line sections. Any assistance appreciated. I have tried a few different things, but can't seem to get my head around grouping by ordercat and only displaying the what and pos for them.

XML
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" encoding="iso-8859-1"/>
	<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
	<xsl:key name="level0" match="//product" use="@ordercat"/>  <!-- group -->
	<xsl:key name="level1" match="//product" use="concat(@ordercat, '|', @what)"/>  <!-- group what-->
	<xsl:key name="level2" match="//product" use="concat(@ordercat, '|', @what, '|', @pos)"/>  <!-- group what pos-->
	
	<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>
				
				<orderCategory><xsl:value-of select="@ordercat"/></orderCategory>
				<xsl:variable name="onrOrdCat" select="@ordercat" />
				<xsl:variable name="onrWhat" select="@what" />

<orderLines>
</orderLines>
</order>


			</xsl:for-each>
		</data>
	</xsl:template>
</xsl:stylesheet>


What I have tried:

Various attempts at trying to group and only get the relevant ordercat, but failed miserably...
Posted
Updated 17-May-16 6:00am
v2
Comments
George Jonsson 17-May-16 6:01am    
Not sure if you are forced into this format, but wouldn't it be easier to do
<orderlines>
<line what="1" pos="2" />
<line what="1" pos="3" />
...
MKM_Matt 17-May-16 6:05am    
Hi George, thanks for the reply.
Not forced into a particular format at moment, just need to ensure that the grouping is correct. So yes I could make them attributes, but I still can't get the grouping part working.
Matt
MKM_Matt 17-May-16 12:07pm    
Hi George. That worked a treat, need to do more testing but all looks brilliant at the moment. You deserve a beer....
George Jonsson 17-May-16 12:11pm    
Time to watch Russia - Sweden in the World Cup.
And too late for more beer now here.
Can I have a rain check? :)
George Jonsson 17-May-16 12:13pm    
Sheit. One more hour to wait. Oh, well.

1 solution

It's a bit late and I had a beer or two, but I think this will work for you.

My additions to your XSL is in bold.
XML
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="iso-8859-1"/>
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
    <!-- 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>


The output is as follows:
XML
<data type="K8 order creation control file">
  <process>3</process>
  <order number="1">
    <ordercategory>C</ordercategory>
    <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="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" />
    </orderlines>
  </order>
  <order number="2">
    <ordercategory>S</ordercategory>
    <orderlines>
      <line what="1" pos="5" />
    </orderlines>
  </order>
  <order number="3">
    <ordercategory>N</ordercategory>
    <orderlines>
      <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" />
    </orderlines>
  </order>
</data>

You can of course change the output to your preference, but I think I got the information right.
 
Share this answer
 
Comments
Maciej Los 17-May-16 12:46pm    
Another beer (5) from me to you, George!
George Jonsson 17-May-16 12:54pm    
Dziękuję Ci, Maciej :)
Maciej Los 17-May-16 12:57pm    
Proszę bardzo (You're very welcome).
George Jonsson 17-May-16 13:01pm    
So Google translate worked!
I have mostly tried it with Chinese and that doesn't come out so well.
Kind of makes people laugh. ;P
Maciej Los 17-May-16 13:27pm    
Google translator is helpful tool, but sometimes it translates a "set of words" instead the sentence (loosing the meaning of sentence).

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