Re: [xsl] Group and change heading element

Subject: Re: [xsl] Group and change heading element
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 12 Sep 2018 17:55:12 -0000
On 12.09.2018 19:49, Charles O'Connor coconnor@xxxxxxxxxxxx wrote:

I am attempting to wrap headings and paragraphs into <sec> elements while changing the <h1> to <title>. So, given this XML

<root>
     <body><div class="abstract">
         <h1><b>Bold</b> Intro!</h1>
         <p>This is an intro <i>with <b>various</b> formatting</i> and other stuff.</p>
         <p>This is a second para in the intro</p>
         <h1>Methods</h1>
         <p>There is no method to our madness</p>
         <h1>Results</h1>
         <p>The results are soooo good . . . </p>
         <p> . . . they require . . . </p>
         <p> . . . three paragraphs</p>
         <h1>Conclusion</h1>
         <p>This is all that is necessary</p>
     </div></body>
</root>

I want:

<root>
     <body>
         <abstract>
             <sec>
                 <title><bold>Bold</bold> Intro!</title>
                 <p>This is an intro <italic>with <bold>various</bold> formatting</italic> and other
                     stuff.</p>
                 <p>This is a second para in the intro</p>
             </sec>
             <sec>
                 <title>Methods</title>
                 <p>There is no method to our madness</p>
             </sec>
             <sec>
                 <title>Results</title>
                 <p>The results are soooo good . . . </p>
                 <p> . . . they require . . . </p>
                 <p> . . . three paragraphs</p>
             </sec>
             <sec>
                 <title>Conclusion</title>
                 <p>This is all that is necessary</p>
             </sec>
         </abstract>
     </body>
</root>

But with this template:

   <xsl:template match="div">
         <abstract>
             <xsl:for-each-group select="*" group-starting-with="h1">
                 <sec>
                     <title>
                         <!--<xsl:value-of select="."/>-->
                         <xsl:apply-templates/>
                     </title>
                     <xsl:for-each select="current-group()">
                         <xsl:copy>
                             <xsl:apply-templates/>
                         </xsl:copy>
                     </xsl:for-each>
                 </sec>
             </xsl:for-each-group>
         </abstract>
     </xsl:template>

As you want to wrap all elements you group into the "sec" element it seems you can use


  <xsl:for-each-group select="*" group-starting-with="h1">
    <sec>
      <xsl:apply-templates select="current-group()"/>
    </sec>
  </xsl:for-each-group>



and then write a template for those "h1" elements

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

of course together with the identity transformation template for those elements you simply want to copy.

Current Thread