[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Group various elements with empty tags (flat XML stru

Subject: 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
glenties
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.