0

I am a newbie to XSLT. I am trying to debug a problem of XSLT(version 2.0) which was written few years back. The part of the XSLT code is as below:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
        <xsl:output indent="yes" doctype-public="-//W3C//DTD HTML 4.0//EN" use-character-maps="m1"/>
        <xsl:character-map name="m1">
            <xsl:output-character character="&#141;" string=" "/>
        </xsl:character-map>
        <xsl:strip-space elements="*"/>
...
...
        <xsl:template match="PARA|PARASTYLE">
            <xsl:choose>
                <xsl:when test="@style-name-escaped or (ancestor::TABLE and not(text())) or (not(*) and not(text()))">
                    <div>
                        <xsl:if test="@style-name-escaped">
                            <xsl:attribute name="class">
                                <xsl:value-of select="@style-name-escaped"/>
                            </xsl:attribute>
                        </xsl:if>
                        <xsl:if test="(ancestor::TABLE and not(text())) or (not(*) and not(text()))">
                            <xsl:attribute name="style">
                                <xsl:text>margin-bottom=10pt</xsl:text>
                            </xsl:attribute>
                            <xsl:text/>
                        </xsl:if>
                        <xsl:apply-templates />
                    </div>
                </xsl:when>
...
..

This XSLT is converting XML into HTML as below. It is basically adding an attribute with a self closing tag as below

<div class="Normal-Level">
  <div style="margin-bottom=10pt"/>
</div>

which is causing a problem in displaying in some browsers because of the self closing tag. What I want to do is the output to look like below with attribute having open and close tag:

   <div class="Normal-Level">
      <div style="margin-bottom=10pt">
      </div>
    </div>

I did my research online, but the syntax seems right for adding an attribute. Any help is appreciated

Rose
  • 1,490
  • 5
  • 25
  • 56
  • I don't understand why you declare HTML 4.0 as the doctype, yet put your elements in the XHTML namespace. – michael.hor257k Dec 19 '18 at 22:52
  • @michael.hor257k I did not code this XSLT. So, I am not sure why it was added – Rose Dec 19 '18 at 22:53
  • Well, I am no expert on that aspect, but it doesn't seem right to me. You need to pick an output format and stick with it. And if you pick XHTML, then self-closing tags are within the specs. – michael.hor257k Dec 19 '18 at 22:58
  • Style="margin--bottom=10pt" is wrong. It should be css as in margin-bottom:10pt – Kevin Brown Dec 20 '18 at 17:10

1 Answers1

2

Try adding method="html" to the xsl:output element.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • It works when I try it. Compare http://xsltransform.hikmatu.com/bFDb2BQ with http://xsltransform.hikmatu.com/bFDb2BQ/1. – michael.hor257k Dec 19 '18 at 23:02
  • Thanks, Michael. I was using notepad++ to do my XSL transform. When I used your website which uses Saxon, it is automatically adding the end tag without even adding "method="html". So, I think the problem is in using the transformation method – Rose Dec 19 '18 at 23:19
  • You have an XSLT 2.0 processor in Notepad++? – michael.hor257k Dec 19 '18 at 23:25
  • I have downloaded an XML plugin for Notepad++ which has an option for XSLT transformation. You need to upload your XSL there and it will convert your XML into HTML – Rose Dec 19 '18 at 23:26
  • Your stylesheet requires XSLT 2.0 and I have doubts that's available in your setup. But I am afraid we're drifting away from the real problem. Which processor will you be using in the actual production? You should test with that. And you should see what happens when you remove the XHTML namespace declaration, as well as the doctype declaration, and remain with a simple `method="html"`. – michael.hor257k Dec 19 '18 at 23:32
  • Sure. I will check that directly on my staging environment and see if it solves the problem. Right now I am not aware of the conversion method that the application uses. It is a web application which is deployed on a tomcat. So, I can just change the XSLT and reload my application. I will let you know if that works. Thanks a lot for your help – Rose Dec 19 '18 at 23:35
  • You can (and should) always know which processor you're using: https://stackoverflow.com/questions/25244370/how-can-i-check-which-xslt-processor-is-being-used-in-solr/25245033#25245033 – michael.hor257k Dec 19 '18 at 23:42
  • I will check that. Removing "XHTML namespace declaration" and adding method "html" solved that problem. Thanks again – Rose Dec 19 '18 at 23:50
  • The stylesheet doesn't actually require XSLT 2.0. It uses some constructs from XSLT 2.0, but an XSLT 1.0 processor should run it in forwards compatibility mode without failure, it will just ignore the things it doesn't understand like character maps. – Michael Kay Dec 20 '18 at 00:58
  • @MichaelKay That's splitting hairs. If they included a character map, I presume they require the character map to be executed in order to get the expected result. – michael.hor257k Dec 20 '18 at 06:08
  • I was just pointing out that running a 2.0 stylesheet with a 1.0 processor is not necessarily going to give you an big in-your-face syntax error. – Michael Kay Dec 20 '18 at 14:45
  • @MichaelKay I see. That's indeed worth pointing out, since an incorrect result may be worse than a failed transformation. – michael.hor257k Dec 20 '18 at 14:50