XalanJ2
  1. XalanJ2
  2. XALANJ-1906

[xhtml output] Empty element form for non EMPTY declared elements

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.2.0
    • Fix Version/s: None
    • Component/s: Serialization
    • Labels:
      None
    • Environment:
      Operating System: All
      Platform: All

      Description

      • The stylesheet:

      > <?xml version="1.0" encoding="UTF-8"?>
      >
      > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      >
      > <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"
      > omit-xml-declaration="no" doctype-public="-//W3C//DTD XHTML 1.0
      Strict//EN"
      > doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
      >
      > <xsl:template match="/">
      > <html>
      > <head>
      > <title>bug test</title>
      > </head>
      > <body>
      > <div></div>
      > </body>
      > </html>
      > </xsl:template>
      >
      > </xsl:stylesheet>

      • The output:

      > <?xml version="1.0" encoding="iso-8859-1"?>
      > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      > <html>
      > <head>
      > <title>bug test</title>
      > </head>
      > <body>
      > <div />
      > </body>
      > </html>

      • The standards:

      > Empty-element tags MAY be used for any element which has no content,
      > whether or not it is declared using the keyword EMPTY.
      > For interoperability, the empty-element tag SHOULD be used, and
      > SHOULD only be used, for elements which are declared EMPTY.
      (http://www.w3.org/TR/REC-xml/#sec-starttags)

      > C.3. Element Minimization and Empty Element Content
      > Given an empty instance of an element whose content model
      > is not EMPTY (for example, an empty title or paragraph) do
      > not use the minimized form (e.g. use <p> </p> and not <p />).
      (http://www.w3.org/TR/xhtml1/#guidelines)

      • The patch:
        Elements like <p> or <div> aren't declared as EMPTY in the XHTML DTD.
        According to XML and XHTML standards, the <div></div> form should be used
        instead of <div/>. Both are XML (and XHTML) valid, but the second leads to
        strange problems with some browsers (guess who — IE).

      Hope this helps,
      Marco

        Activity

        Hide
        Matteo TURRA added a comment -

        You can do like this:

        Suppose the content of your testarea is value
        <textarea id="my_id" name="textareaname">
        <xsl:choose>
        <xsl:when test="not($value/node())"><xsl:value-of select="''"/></xsl:when>
        <xsl:otherwise><xsl:copy-of select="$value"/></xsl:otherwise>
        </xsl:choose>
        </textarea>

        This way an empty textNode will be output if value have no nodes.

        Show
        Matteo TURRA added a comment - You can do like this: Suppose the content of your testarea is value <textarea id="my_id" name="textareaname"> <xsl:choose> <xsl:when test="not($value/node())"><xsl:value-of select="''"/></xsl:when> <xsl:otherwise><xsl:copy-of select="$value"/></xsl:otherwise> </xsl:choose> </textarea> This way an empty textNode will be output if value have no nodes.
        Hide
        Fabrizio Giudici added a comment -

        Eight years since this bug has been filed. I don't understand why it's "minor" priority, given that producing correct XHTML might be one of the most common tasks a web developer has got to fullfill these days.

        Matteo's solution seems the most logical. Any chance to see it implemented soon?

        In the meantime, is there any workaround that one can apply to Xalan embedded in JDK6 / 7? Thanks.

        Show
        Fabrizio Giudici added a comment - Eight years since this bug has been filed. I don't understand why it's "minor" priority, given that producing correct XHTML might be one of the most common tasks a web developer has got to fullfill these days. Matteo's solution seems the most logical. Any chance to see it implemented soon? In the meantime, is there any workaround that one can apply to Xalan embedded in JDK6 / 7? Thanks.
        Hide
        Matteo TURRA added a comment -

        This patch disable collapsing for all element (i.e. br will be rendered as <br></br>) and this is not the right behavior.

        The issue is there is not xsl output method xhtml. You can choose between xml where <textarea></textarea> and <textarea/> have the one meaning, and html where each tag have special attribute to guide the serialization.

        Looking at the sources I see org.apache.xml.serialize.ToStream abstract class implemented by three classes ToHTMLStream, ToXMLStream and ToTextStream (the last not used to produce (x)html).

        In ToHTMLStream.initTagReference an instance variable is initialized with all elements flags. For empty-Element like BR there is a ElemDesc.EMPTY flag to let the serialization close the element with > character. Other elements without this flag are closed with ></tagName> sequence if empty (i.e. textarea elements).

        Conclusion: if xhtml should "For interoperability, the empty-element tag SHOULD be used, and SHOULD only be used, for elements which are declared EMPTY." ( see http://www.w3.org/TR/REC-xml/#sec-starttags ) this mean a ToXHTMLStream should be created with similar elements flags of ToHTMLStream, and a new output method named xhtml added.

        Show
        Matteo TURRA added a comment - This patch disable collapsing for all element (i.e. br will be rendered as <br></br>) and this is not the right behavior. The issue is there is not xsl output method xhtml. You can choose between xml where <textarea></textarea> and <textarea/> have the one meaning, and html where each tag have special attribute to guide the serialization. Looking at the sources I see org.apache.xml.serialize.ToStream abstract class implemented by three classes ToHTMLStream, ToXMLStream and ToTextStream (the last not used to produce (x)html). In ToHTMLStream.initTagReference an instance variable is initialized with all elements flags. For empty-Element like BR there is a ElemDesc.EMPTY flag to let the serialization close the element with > character. Other elements without this flag are closed with ></tagName> sequence if empty (i.e. textarea elements). Conclusion: if xhtml should "For interoperability, the empty-element tag SHOULD be used, and SHOULD only be used, for elements which are declared EMPTY." ( see http://www.w3.org/TR/REC-xml/#sec-starttags ) this mean a ToXHTMLStream should be created with similar elements flags of ToHTMLStream, and a new output method named xhtml added.
        Hide
        Sébastien Launay added a comment -

        Patch for disabling collapsing of empty elements.

        A new property xalan output property has been added (

        {http://xml.apache.org/xalan}

        disable-empty-element-collapsing)
        in order to disable collapsing for empty elements.
        A more flexible approach would have been to disable collapsing only for a given set of elements (script, textarea, a, p, div, ...) like in ToHTMLStream (this still can be done by overriding endElement(String, String, String) and by setting the protected property).
        But i think this workaround is more efficient and the resulting output size growth is acceptable.

        At first, i also wanted to activate this property if the current public doctype is XHTML but this may lead to unwanted behaviour when updating from Xalan 2.7.1 (i can send a new patch if this improvement is acceptable).

        The default value for this property is false, so current behaviour is respected.

        Show
        Sébastien Launay added a comment - Patch for disabling collapsing of empty elements. A new property xalan output property has been added ( {http://xml.apache.org/xalan} disable-empty-element-collapsing) in order to disable collapsing for empty elements. A more flexible approach would have been to disable collapsing only for a given set of elements (script, textarea, a, p, div, ...) like in ToHTMLStream (this still can be done by overriding endElement(String, String, String) and by setting the protected property). But i think this workaround is more efficient and the resulting output size growth is acceptable. At first, i also wanted to activate this property if the current public doctype is XHTML but this may lead to unwanted behaviour when updating from Xalan 2.7.1 (i can send a new patch if this improvement is acceptable). The default value for this property is false, so current behaviour is respected.
        Hide
        Johans Marvin Taboada Villca added a comment -

        Vote to solve this issue.

        I'm having same issue but this time with empty anchor elements and CSS. I'll quote Sourceforge's https://sourceforge.net/tracker/?func=detail&atid=373747&aid=1803803&group_id=21935

        "
        Resulting xhtml output arises Mozilla's #265324 in Core component (same
        problem in IE).

        Generated empty anchor (<a id="some-id" />) element give CSS problems when
        applying CSS styling, example css snippet:

        a

        { color: blue; text-decoration: underline; }

        a:hover

        { background: rgb(75%,75%,100%); color: blue; text-decoration: underline; }

        "

        > C.3. Element Minimization and Empty Element Content

        It's not a rule, rather guidelines, but I think compatibility it's Xalan-J's concern. Maybe there could be a ''compatibility'' flag for XHTML output where noticed in XSL stylesheets (an extension)?.

        Show
        Johans Marvin Taboada Villca added a comment - Vote to solve this issue. I'm having same issue but this time with empty anchor elements and CSS. I'll quote Sourceforge's https://sourceforge.net/tracker/?func=detail&atid=373747&aid=1803803&group_id=21935 " Resulting xhtml output arises Mozilla's #265324 in Core component (same problem in IE). Generated empty anchor (<a id="some-id" />) element give CSS problems when applying CSS styling, example css snippet: a { color: blue; text-decoration: underline; } a:hover { background: rgb(75%,75%,100%); color: blue; text-decoration: underline; } " > C.3. Element Minimization and Empty Element Content It's not a rule, rather guidelines, but I think compatibility it's Xalan-J's concern. Maybe there could be a ''compatibility'' flag for XHTML output where noticed in XSL stylesheets (an extension)?.
        Hide
        Henry Zongaro added a comment -

        Priorty was inadvertently changed to "Blocker." Priority in bugzilla was "minor."

        Show
        Henry Zongaro added a comment - Priorty was inadvertently changed to "Blocker." Priority in bugzilla was "minor."
        Hide
        Robert van Loenhout added a comment -

        I wanted to make this bug more clear.
        Outputting elements such as textarea without contents as <textarea .../> is NOT XHTML valid.
        Even though the W3C validator seems to accept it, the XHTML 1.0 standard declares:

        All elements other than those declared in the DTD as EMPTY must have an end tag. Elements that are declared in the DTD as EMPTY can have an end tag or can use empty element shorthand

        http://www.w3.org/TR/xhtml1/#h-4.3

        Internet Explorer, but also Mozilla and Firefox wil not be able to render such documents.
        This is clear in a document that includes <textarea/>.
        Also <a name="123"/> and CSS to change the style op anchor tags, will give problems.

        Show
        Robert van Loenhout added a comment - I wanted to make this bug more clear. Outputting elements such as textarea without contents as <textarea .../> is NOT XHTML valid. Even though the W3C validator seems to accept it, the XHTML 1.0 standard declares: All elements other than those declared in the DTD as EMPTY must have an end tag. Elements that are declared in the DTD as EMPTY can have an end tag or can use empty element shorthand http://www.w3.org/TR/xhtml1/#h-4.3 Internet Explorer, but also Mozilla and Firefox wil not be able to render such documents. This is clear in a document that includes <textarea/>. Also <a name="123"/> and CSS to change the style op anchor tags, will give problems.

          People

          • Assignee:
            Unassigned
            Reporter:
            Marco
          • Votes:
            7 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

            • Created:
              Updated:

              Development