RE: [xsl] problem with creating structure

Subject: RE: [xsl] problem with creating structure
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sun, 11 Nov 2007 23:33:01 -0000
There is a construct in XSLT 2.0 explicitly designed for problems like this:

<xsl:for-each-group group-starting-with="h2">

It can be done in XSLT 1.0, using recursion, but requires more effort. So
the first thing to establish is, are you using XSLT 2.0 or 1.0?

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Andreas Peter [mailto:info@xxxxxxxxxx]
> Sent: 11 November 2007 21:05
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] problem with creating structure
>
> Hello list,
>
> XSL exasperates me! I have the following XML structure:
>
> <Root>
>      <Textabschnitt>
>          <h1>Geistiger Volksbesitz der Kameruner im Blickfeld
> des Missionars</h1>
>          <h2>Einf|hrung </h2>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>          <h2>I.Teil: Der &#65279;Mensch  ein &#65279;Leib </h2>
>          <h3>Allgemeines </h3>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>          <p>...</p>
>      </Textabschnitt>
> </Root>
>
> This should be transformed into the following XML structure:
>
> <set>
>      <book>
>          <bookinfo/>
>          <title/>
>          <chapter>
>              <title/>
>              <para/>
>              <sect1>
>                  <title/>
>                  <para/>
>              </sect1>
>              <sect2>
>                  <title/>
>                  <para/>
>              </sect2>
>              <sect3>
>                  <title/>
>                  <para/>
>              </sect3>
>          </chapter>
>      </book>
> </set>
>
> I want to insert an element <chapter> before the element <h2>
> but only for the first element <h2>. The second element <h2>
> should be transformed to <title>. And I need to output every
> <p> element according to its proir element until the next
> <h2>, <h3>, ... occures.
> I have the following XSL code which generates for every <h2>
> element an element <chapter>.
>
>      <xsl:template match="Root">
>          <xsl:element name="set">
>              <xsl:element name="book">
>                  <xsl:element name="bookinfo"/>
>                  <xsl:element name="title">
>                      <xsl:value-of select="/h1"/>
>                  </xsl:element>
>                  <xsl:apply-templates/>
>              </xsl:element>
>          </xsl:element>
>      </xsl:template>
>
>      <xsl:template match="h2">
>          <xsl:for-each select=".">
>              <xsl:if test="position() = 1">
>                  <xsl:element name="chapter">
>                      <xsl:element name="title">
>                          <xsl:value-of select=".[position() = 1]"/>
>                      </xsl:element>
>                  </xsl:element>
>              </xsl:if>
>              <xsl:if test="position() != 1">
>                  <xsl:element name="title">
>                      <xsl:value-of select=".[position() != 1]"/>
>                  </xsl:element>
>              </xsl:if>
>          </xsl:for-each>
>          <xsl:apply-templates/>
>      </xsl:template>
>
>      <xsl:template match="h3">
>          <xsl:for-each select=".">
>              <xsl:element name="sect1">
>                  <xsl:element name="title">
>                      <xsl:value-of select="."/>
>                  </xsl:element>
>              </xsl:element>
>          </xsl:for-each>
>          <xsl:apply-templates/>
>      </xsl:template>
>
>      <xsl:template match="h4">
>          <xsl:for-each select=".">
>              <xsl:element name="sect2">
>                  <xsl:element name="title">
>                      <xsl:value-of select="."/>
>                  </xsl:element>
>                  <xsl:for-each select="Root/Textabschnitt/h4/p">
>                      <xsl:element name="para">
>                          <xsl:value-of select="p"/>
>                      </xsl:element>
>                  </xsl:for-each>
>              </xsl:element>
>          </xsl:for-each>
>          <xsl:apply-templates/>
>      </xsl:template>
>
> Unfortunately I cannot see the problem. Any hint from the
> experts? I hope this is enough code.
>
> Thanks so much,
> Andreas

Current Thread