into paragraphs with XSLT 1.0 I am looking for a quick and easy way to convert my XM...">

XHTML. Include <div> text in paragraphs and convert <br/"> into paragraphs with XSLT 1.0

I am looking for a quick and easy way to convert my XML (which is similar to XHTML) with XSLT 1.0:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head/>
  <body>
    <div>Hello<a href="http://google.com">this is the first</a>line.<p>This the second.<br/>And this the third one.</p></div>
  </body>
 </html>

to that:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head/>
  <body>
    <div>
        <p>Hello<a href="http://google.com">this is the first</a>line.</p>
        <p>This the second.</p>
        <p>And this the third one.</p>
    </div>
  </body>
 </html>

I was thinking of a tree walk algorithm in XSLT 1.0. What is complicated, for example, the attached links <a>. And existing <p>one should not be deleted.

Can someone help me? Many thanks.

+5
source share
1 answer

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="div[text() and p]">
  <div>
   <p>
     <xsl:apply-templates select="node()[not(self::p or preceding-sibling::p)]"/>
   </p>
   <xsl:apply-templates select="p | p/following-sibling::node()"/>
  </div>
 </xsl:template>

 <xsl:template match="p[text() and br]">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match=
  "p/text()
    [preceding-sibling::node()[1][self::br]
    or
     following-sibling::node()[1][self::br]
    ]">
  <p><xsl:value-of select="."/></p>
 </xsl:template>

 <xsl:template match="p/br"/>
</xsl:stylesheet>

when applied to the provided XML document :

<html>
    <head/>
    <body>
        <div>Hello
            <a href="http://google.com">this is the first</a>line.
            <p>This the second.<br/>And this the third one.</p>
        </div>
    </body>
</html>

creates the desired, correct result :

<html>
   <head/>
   <body>
      <div>
         <p>Hello
            <a href="http://google.com">this is the first</a>line.
            </p>
         <p>This the second.</p>
         <p>And this the third one.</p>
      </div>
   </body>
</html>
+5
source

All Articles