View Single Post
  #6 (permalink)  
Old August 14th, 2010, 10:11 AM
Martin Honnen Martin Honnen is offline
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If the input is
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<listcontainer>
    <box id="a1" size="24" status="saled">
        <numberofball>3</numberofball>
    </box>
    <box id="a2" size="64" status="instore">
        <numberofball>3</numberofball>
    </box>
    <box id="a3" size="36" status="instore">
        <numberofball>3</numberofball>
    </box>
    <box id="a4" size="42" status="instore"/>
    <box id="a5" size="43" status="saled"/>
    
    <bag id ="b1" sive="16x20" status="instore"><numberofbook>12</numberofbook></bag>
    <bag id ="b2" sive="10x10" status="instore"><numberofbook>8</numberofbook></bag>
    <bag id ="b3" sive="16x10" status="instore"><numberofbook>10</numberofbook></bag>
    <bag id ="b4" sive="16x20" status="saled"></bag>
    <bag id ="b5" sive="16x20"><numberofbook>13</numberofbook></bag>
    
    <can id = "c1" size ="100m3" status="instore"><oil>3l</oil></can>
    <can id = "c2" size ="120m3" status="saled"><oil>4l</oil></can>
    <can id = "c3" size ="200m3" status="instore"></can>
    <can id = "c4" size ="210m3" status="saled"><oil>4l</oil></can>
    <can id = "c5" size ="220m3" status="instore"><oil>5l</oil></can>
    <can id = "c6" size ="300m3" status="saled" ><oil>13l</oil></can>

</listcontainer>
</root>
then the XSLT 2.0 stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:template match="listcontainer">
    <goods_status>
      <xsl:for-each-group select="*" group-by="@status">
        <xsl:element name="{current-grouping-key()}_goods">
          <xsl:apply-templates select="current-group()"/>
        </xsl:element>
      </xsl:for-each-group>
    </goods_status>
  </xsl:template>
  
  <xsl:template match="box[*] | bag[*] | can[*]">
    <xsl:element name="notempty_{local-name()}"/>
  </xsl:template>
  
  <xsl:template match="box[not(*)] | bag[not(*)] | can[not(*)]">
    <xsl:element name="empty_{local-name()}"/>
  </xsl:template>
  
</xsl:stylesheet>
outputs
Code:
<goods_status>
   <saled_goods>
      <notempty_box/>
      <empty_box/>
      <empty_bag/>
      <notempty_can/>
      <notempty_can/>
      <notempty_can/>
   </saled_goods>
   <instore_goods>
      <notempty_box/>
      <notempty_box/>
      <empty_box/>
      <notempty_bag/>
      <notempty_bag/>
      <notempty_bag/>
      <notempty_can/>
      <empty_can/>
      <notempty_can/>
   </instore_goods>
</goods_status>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
metinhoclam (August 14th, 2010)