Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 8th, 2006, 08:00 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Flat XML structure to a tree by XSLT

Hi all,

I have a flat XML structure like this ...

 <Heading1>This ist my heading one</Heading1>
 <Text>Here is some text</Text>
 <Text>Another text</Text>
 <Text>And this is text too</Text>
 <Heading1>This ist my heading two</Heading1>
 <Text>Here is some text</Text>
 <Text>Another text</Text>
 <Heading1>This ist my heading thre</Heading1>
 <Text>Here is some text</Text>
 <Text>Another text</Text>

... and would like to transform it to a tree structure by XSLT, like this.

 <chapter>
  <Heading1>This ist my heading one</Heading1>
  <Text>Here is some text</Text>
  <Text>Another text</Text>
  <Text>And this is text too</Text>
 </chapter>
 <chapter>
  <Heading1>This ist my heading two</Heading1>
  <Text>Here is some text</Text>
  <Text>Another text</Text>
 </chapter>
 <chapter>
  <Heading1>This ist my heading thre</Heading1>
  <Text>Here is some text</Text>
  <Text>Another text</Text>
 </chapter>

Does anybody know how I can do this or give me an example? I tried to do it with a recursive template, but it didn't work.

Thank you very much in advance
Borg0011


Reply With Quote
  #2 (permalink)  
Old July 9th, 2006, 03:17 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

This problem is known as "positional grouping" and you will find examples if you google under that name. In XSLT 2.0 you can tackle it using xsl:for-each-group. In 1.0 there are basically two approaches: recursive traversal of the siblings, or reduction to a value-based grouping problem using generate-id(preceding-sibling::Heading1[1]) as the grouping key (which you can then use in a standard Muenchian grouping pattern).

If your recursive solution didn't work then it had a bug, and there's little I can do to help you find the bug if you don't post your code.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old July 9th, 2006, 08:58 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

thank you very much for your Tips. Meanwhile I found the bugs and the recursive stylesheet works. Here is my solution, if somebody is interested:

<?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" version="1.0" encoding="UTF-8"/>

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

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

<xsl:template match="Content">
 <xsl:element name="Content">
  <xsl:if test="name(child::*[1])!='Heading1'">
   <xsl:element name="Prefix">
    <xsl:call-template name="generatePrefix">
     <xsl:with-param name="pNode" select="child::*"/>
     <xsl:with-param name="pPos" select="1"/>
    </xsl:call-template>
   </xsl:element>
  </xsl:if>
  <xsl:for-each select="./Heading1">
   <xsl:element name="chapter">
    <xsl:copy-of select="."/>
     <xsl:call-template name="generateChapter">
      <xsl:with-param name="cNode" select="following-sibling::*"/>
      <xsl:with-param name="cPos" select="1"/>
     </xsl:call-template>
    </xsl:element>
   </xsl:for-each>
  </xsl:element>
</xsl:template>

<xsl:template name="generateChapter">
 <xsl:param name="cNode"/>
 <xsl:param name="cPos"/>
 <xsl:if test="name($cNode[$cPos])!='Heading1'">
  <xsl:copy-of select="$cNode[$cPos]"/>
   <xsl:if test="$cNode[$cPos]!=$cNode[last()]">
    <xsl:call-template name="generateChapter">
     <xsl:with-param name="cNode" select="$cNode"/>
     <xsl:with-param name="cPos" select="$cPos+1"/>
    </xsl:call-template>
   </xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template name="generatePrefix">
 <xsl:param name="pNode"/>
 <xsl:param name="pPos"/>
 <xsl:if test="name($pNode[$pPos])!='Heading1'">
  <xsl:copy-of select="$pNode[$pPos]"/>
   <xsl:if test="$pNode[$pPos]!=$pNode[last()]">
    <xsl:call-template name="generatePrefix">
     <xsl:with-param name="pNode" select="$pNode"/>
     <xsl:with-param name="pPos" select="$pPos+1"/>
    </xsl:call-template>
   </xsl:if>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flat DB structure into XML Hierarchy DaveQuested XSLT 1 January 29th, 2007 10:25 AM
Flat XML to tree Dia XSLT 1 November 16th, 2006 05:11 AM
mapping repeating xml structure to flat csv f vasmallela Biztalk 0 May 29th, 2005 11:53 PM



All times are GMT -4. The time now is 06:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.