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

Subject: [xsl] Group various elements with empty tags (flat XML structure to hierarcial XML)
From: "M Glenties" <mglenties@xxxxxxxxxxx>
Date: Thu, 06 Jan 2005 13:55:59 -0500
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