Re: [xsl] Split table row on paragraphs

Subject: Re: [xsl] Split table row on paragraphs
From: "kevinbrianbird@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 30 Oct 2018 17:53:00 -0000
Dear Martin,

Thanks for the prompt reply and solution!

--
Kevin

-----Original Message-----
From: Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: 30 October 2018 17:09
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Split table row on paragraphs

On 30.10.2018 17:46, kevinbrianbird@xxxxxxxxx wrote:

> I have tables with arbitrary columns and each cell can have an
> arbitrary number of paragraphs. I want to split rows on paragraphs, so
> each row only contains one <p> element.
>
> Simplified input:
>
> <table>
>
>      <tr>
>
>          <td>
>
>              <p>Para 1</p>
>
>          </td>
>
>          <td>
>
>              <p>Para 1</p>
>
>              <p>Para 2</p>
>
>              <p>Para 3</p>
>
>          </td>
>
>          <td>
>
>              <p>Para 1</p>
>
>              <p>Para 2</p>
>
>          </td>
>
>      </tr>
>
> </table>
>
> Desired output:
>
> <table>
>
>      <tr>
>
>          <td>
>
>              <p>Para 1</p>
>
>          </td>
>
>          <td>
>
>              <p>Para 1</p>
>
>          </td>
>
>          <td>
>
>              <p>Para 1</p>
>
>          </td>
>
>      </tr>
>
>      <tr>
>
>          <td>
>
>          </td>
>
>          <td>
>
>              <p>Para 2</p>
>
>          </td>
>
>          <td>
>
>              <p>Para 2</p>
>
>          </td>
>
>      </tr>
>
>      <tr>
>
>          <td>
>
>          </td>
>
>          <td>
>
>              <p>Para 3</p>
>
>          </td>
>
>          <td>
>
>          </td>
>
>      </tr>
>
> </table>
>
> I am after suggestions as to the best way of tackling such a problem.

Assuming at least XSLT 2 and that the "td" elements only "p" elements I
think you can simply process each "tr" element
   1 to max(td/count(p))
times with e.g.

   <xsl:template match="tr">
       <xsl:variable name="this-row" select="."/>
       <xsl:for-each select="1 to max(td/count(p))">
           <tr>
               <xsl:apply-templates select="$this-row/*">
                   <xsl:with-param name="pos" select="."/>
               </xsl:apply-templates>
           </tr>
       </xsl:for-each>
   </xsl:template>

   <xsl:template match="td">
       <xsl:param name="pos"/>
       <xsl:copy>
           <xsl:apply-templates select="p[$pos]"/>
       </xsl:copy>
   </xsl:template>


Online sample using XSLT 3 (but only for setting up the identity
transformation with xsl:mode) is at
https://xsltfiddle.liberty-development.net/6qVRKx7.

Current Thread