将XML映射和分组到XML [英] Mapping and grouping XML to XML

查看:101
本文介绍了将XML映射和分组到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在尝试映射以下XML

Hi. I am trying to map the following 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 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.任何协助赞赏。 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.






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 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...



What I have tried:

Various attempts at trying to group and only get the relevant ordercat, but failed miserably...

推荐答案

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.

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 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"/></orderCategory> <orderLines> <xsl:apply-templates select="../product[@ordercat =


onrOrdCat]\" />
</orderLines>
</order>
</xsl:for-each>
</data>
</xsl:template>
\t
<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>
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:



The output is as follows:

<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.


You can of course change the output to your preference, but I think I got the information right.


这篇关于将XML映射和分组到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆