2 Replies Latest reply: Feb 9, 2012 2:59 PM by pl_sequel RSS

    XSLT and html

    pl_sequel
      Ok..losing my mind here. Running on 11.2.0.2 AIX

      I have an xslt to generate html document. Some of the html will be provided by an end-user... which I will embed into the xslt, to create one final template. Some problems I'm having:

      - br tags... no matter what I try, can't seem to prevent the transform from outputting
      <br></br>
      The issue is this is causing double line-breaks when rendered in a browser or email client (using this to generate html emails)

      Example:
      declare
      l_xml xmltype;
      l_html_part clob;
      
      begin
      
      
      l_xml := xmltype('<content>This is a test</content>');
       
      SELECT  XMLSerialize(document XMLtransform(l_xml , xmltype('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes"/>
        <xsl:template match="/">
          <html><body>
      <p><xsl:value-of select="content" /></p>
      <blockquote>
      Some text <br/>
      on muliple <br/>
      lines<br/>
      here.
      </blockquote>
            </body>
          </html>
        </xsl:template>
      </xsl:stylesheet>')) as clob) into l_html_part from dual;
             
      p.l(l_html_part);
      
      end;
      Generates the following:
      <html><body><p>This is a test</p><blockquote>
      Some text <br></br>
      on muliple <br
      
      </br>
      lines<br></br> here. </blockquote></body></html>
      I've tried changing the method output from html to xml... but to no avail.

      Now given I'm accepting html input from end users to embed into my xslt (enabling end users to customize the html template), any other issues I should guard against? I plan on using a wysiwyg javascript editor on the front end, to tidy the html input as much as possible, so it's well-formed. However, dealing with entities, and URLs means I may have to scrub the html a little further?

      For instance, URLs will sometimes contain & in the query string... and parameter values... one has to be escaped to
      &amp;
      while the other URL encoded..

      Thanks for any tips or advice.

      Stephane

      Edited by: pl_sequel on Feb 9, 2012 3:04 PM

      Edited by: pl_sequel on Feb 9, 2012 3:05 PM
        • 1. Re: XSLT and html
          odie_63
          The issue is this is causing double line-breaks when rendered in a browser or email client (using this to generate html emails)
          Didn't know that effect...

          XMLSerialize with INDENT option will output empty elements in their short forms :
          SQL> SELECT XMLSerialize(document XMLtransform(xmltype('<content>This is a test</content>')
            2  , xmltype('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            3    <xsl:output method="html" />
            4    <xsl:template match="/">
            5      <html>
            6      <body>
            7  <p><xsl:value-of select="content" /></p>
            8  <blockquote>
            9  Some text <br/>
           10  on muliple <br/>
           11  lines<br/>
           12  here.
           13  </blockquote>
           14        </body>
           15      </html>
           16    </xsl:template>
           17  </xsl:stylesheet>')) as clob indent)
           18  FROM dual
           19  ;
           
          XMLSERIALIZE(DOCUMENTXMLTRANSF
          --------------------------------------------------------------------------------
          <html>
            <body>
              <p>This is a test</p>
              <blockquote>
          Some text <br/>
          on muliple <br/>
          lines<br/>
          here.
          </blockquote>
            </body>
          </html>
           
          Now given I'm accepting html input from end users to embed into my xslt (enabling end users to customize the html template), any other issues I should guard against?
          How are you doing it? (the embedding part)
          • 2. Re: XSLT and html
            pl_sequel
            Thanks for that... I missed that keyword in the docs.

            The embedding of the user supplied html will be done using regexp_replace most likely.

            The design I am going with, is an html template stored in a clob, with various "merge tags" defined in the body.

            for example:
            <html>
            <body>
            <p>This is a test. Hi *|FIRSTNAME|*</p>
            *|CONTENT|*
            </body>
            </html>
            User will supply the html:
            <p>This is the user html content</p>
            I will then replace the *|CONTENT|* in the body with the user-supplied html:
            <html>
            <body>
            <p>This is a test. Hi *|FIRSTNAME|*</p>
            <p>This is the user html content</p>
            </body>
            </html>
            At this point, I now take the final html template, and turn it into an XSLT. Any remaining merge tags, get replaced with appropirate xsl:value-of statements as well..

            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output method="html" />
            <xsl:template match="/">
            <html>
            <body>
            <p>This is a test. Hi <xsl:value-of select="FIRSTNAME" /></p>
            <p>This is the user html content</p>
            </body>
            </html>
            </xsl:template>
            </xsl:stylesheet>
            I will then have the respective xml data transformed using this stylesheet.

            Kinda creating a custom templating engine... to enable users to merge in custom data (which is already stored in the DB) and customize the template content. The final transformed html will be used to send as an html email.