1

<?xml-stylesheet type="text/xml" href="#style1"?>
<!DOCTYPE message  [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]>
<message>
  <xsl:stylesheet id="style1" version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="html"/>
    <xsl:template match="/">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <body>
          line 1
          <br/>
          line 2
        </body>

      </html>
    </xsl:template>
  </xsl:stylesheet>
</message>

I validated the XML syntax at https://www.w3schools.com/xml/xml_validator.asp : enter image description here


In Chrome, the above XML file renders with two <br>s:

enter image description here In Firefox, the above XML file renders with one <br>: enter image description here

I wonder which is correct? One <br> or two <br>?

10
  • What happens if you use xsl:output method="xml" as you use XHTML? Commented Jul 15 at 11:48
  • I am pretty sure the correct answer is 1. Does the same problem occur with <xsl:element name="br"/> in place of <br/>? What about other empty HTML elements like <meta> - do they have the same problem? Commented Jul 15 at 12:17
  • 2
    The XSLT implementations of Chrome/Chromium based browsers and Firefox/Mozilla are much different, Firefox transforms the XML input tree with XSLT into an HTML or XHTML result tree. Chrome, meanwhile, as far as I understand it, use libxslt to create a serialized transformation result that is then fed to a parser, if you use output method="html", if will be the text/html / HTML5 parser, if you use output method="xml", it will be the XML parser. The HTML5 parser might parse the serialized transformation result into two br elements, it would be worth checking what the HTML5 validators do. Commented Jul 15 at 13:07
  • 3
    I think libxslt serializes the empty <br/> as XML <br></br>, if it is then parsed as HTML that indeed creates two HTML br elements martin-honnen.github.io/xslt/2024/test2024071501.html in Chrome. Commented Jul 16 at 9:28
  • 1
    And even Firefox renders <html><br></br></html> as two line breaks. But it is invalid HTML according to validator.w3.org/check (it is valid XHTML, however). To avoid the problem, omit the xmlns="http://www.w3.org/1999/xhtml" from the <html> element. Commented Jul 16 at 11:39

0

Browse other questions tagged or ask your own question.