RE: [xsl] Group various elements with empty tags (flat XML structure to hierarcial XML)

Subject: RE: [xsl] Group various elements with empty tags (flat XML structure to hierarcial XML)
From: cknell@xxxxxxxxxx
Date: Thu, 06 Jan 2005 14:29:15 -0500
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="xml" indent="yes" encoding="UTF-8" />
   <xsl:strip-space elements="*" />

   <xsl:template match="/">
      <xsl:apply-templates />
   </xsl:template>

   <xsl:template match="Record">
      <Record>
         <xsl:apply-templates />
      </Record>
   </xsl:template>

   <xsl:template match="id|type|count|language">
      <xsl:copy-of select="." />
   </xsl:template>

   <xsl:template match="firstname">
      <NAME>
         <xsl:copy-of select="." />
         <xsl:copy-of select="following-sibling::lastname[1]" />
      </NAME>
   </xsl:template>

   <xsl:template match="street">
      <ADDRESS>
         <xsl:copy-of select="." />
         <xsl:copy-of select="following-sibling::city[1]" />
         <xsl:copy-of select="following-sibling::country[1]" />
         <xsl:copy-of select="following-sibling::postal_code[1]" />
      </ADDRESS>
   </xsl:template>

   <xsl:template match="initial|lastname|city|country|postal_code|amount1|amount2|amount3" />

</xsl:stylesheet>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     M Glenties <mglenties@xxxxxxxxxxx>
Sent:     Thu, 06 Jan 2005 13:55:59 -0500
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  [xsl] Group various elements with empty tags (flat XML structure to hierarcial XML)

I need to convert a flat xml document to something more hierarcial by 
surrounding some elements with other empty elements.

Original XML:
------------
<Record>
   <id>102</id>
   <type>O</Type>
   <count>37</count>
   <firstname>Joe</firstname>
   <lastname>Smith</lastname>
   <initial>A</initial>
   <street>35 Main Street</street>
   <city>Moosejaw</city>
   <country>Sasaktchewan</country>
   <postal_code>TOEOPO</postal_code>
   <amount1>1.23</amount1>
   <amount2>4.56</amount2>
   <amount3>7.89</amount3>
   <language>E</language>
</Record>


Desired Output:
--------------
<Record>
   <id>102</id>
   <type>O</Type>
   <count>37</count>
   <NAME>
      <firstname>Joe</firstname>
      <lastname>Smith</lastname>
   </NAME>
   <ADDRESS>
      <street>35 Main Street</street>
      <city>Moosejaw</city>
      <country>Saskatchewan</country>
      <postal_code>TOEOPO</postal_code>
   </ADDRESS>
   <language>E</language>
</Record>


My stylesheet:
-------------
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="Record">
<Record>
   <xsl:apply-templates select="lastname" mode="Name"/>
   <xsl:apply-templates select="postal_code" mode="PC"/>
</Record>
</xsl:template>

<xsl:template match="lastname" mode="Name">
<NAME>
<xsl:apply-templates 
select="preceding::firstname[generate-id(following::lastname[1]) = 
generate-id(current())]"/>
<xsl:apply-templates select="."/>
</NAME>
</xsl:template>

<xsl:template match="postal_code" mode="PC">
<ADDRESS>
<xsl:apply-templates 
select="preceding::street[generate-id(following::postal_code[1]) = 
generate-id(current())]"/>
<xsl:apply-templates select="."/>
</ADDRESS>
</xsl:template>

<xsl:template match="Record/*">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

My output:
--------------
<NAME>
   <firstname>Joe</firstname>
   <lastname>Smith</lastname>
</NAME>
<ADDRESS>
   <street>35 Main Street</street>
   <postal_code>TOEOPO</postal_code>
</ADDRESS>


Can anyone tell me where I have gone wrong? I'm missing <id>, <type> etc, 
and my xsl returns only 2 elements when more are sometimes required.

Thanks for your time,
M Glenties

_________________________________________________________________
MSN. Calendar keeps you organized and takes the effort out of scheduling 
get-togethers. 
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines 
  Start enjoying all the benefits of MSN. Premium right now and get the 
first two months FREE*.

Current Thread