xsl-list
[Top] [All Lists]

Re: [xsl] Split table row on paragraphs

2018-10-30 12:08:59
On 30.10.2018 17:46, kevinbrianbird(_at_)gmail(_dot_)com 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.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>