Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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


 
Old July 9th, 2006, 03:17 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
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>





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.