XML prolog / instruction not removed from XHTML output
Asked Answered
B

2

13

I'm starting to learn JavaServer Faces (JSF). I'm using GlassFish 3+. I've just created a new JSF project in NetBeans and run the project. It worked fine, but upon examining the XHTML output, I noticed the XML declaration was left in. This messes up the DOCTYPE declaration (which is always supposed to be first in the document).

enter image description here

Is JSF supposed to remove the XML declaration, or is there something I've done wrong?

Brainsick answered 22/5, 2012 at 8:53 Comment(2)
If there is an XML declaration then it must appear before the Doctype. This will only cause problems on IE6, so I wouldn't worry about it unless IE6 is a target platform.Sike
As to the bounty description, it'll be rectified in a future edition of Core JSF book. As to Netbeans, I don't have influence on it, nor am I interested to have.Seaboard
S
12

Facelets will by default only remove it from compositions (include files and composite components) and tag files. It won't remove it from the master template. Just remove it yourself. You shouldn't be using the XML prolog at all when authoring HTML.

Whether the XML prolog will be removed from the master template is specified in appendix 1.1.1.1 of JSF 2.2 specification which describes the configuration of <facelets-processing> element in faces-config.xml. The XML prolog is described as "processing instructions". In the table, you'll see that it is only removed (consumed) when the template is processed as a XML or JSPX view.

1.1.1.1 The facelets-processing element

The <facelets-processing> element is used to affect the processing of Facelets VDL files. Therefore, this setting only applies to those requests that reach the Facelets ViewDeclarationLanguage implementation, as specified to the runtime via the javax.faces.FACELETS_VIEW_MAPPINGS and javax.faces.DEFAULT_SUFFIX <context-param> entries. The specification defines three processing modes for Facelets files: Facelets XHTML syntax, XML View syntax, and Facelets JSPX syntax. This last syntax is intended to ease the migration to Facelets for applications already using the JSP document syntax (also known as JSPX syntax). The affect on the processing of files in each of these three modes is specified in the following table.

Valid <process-as> values and their implications on the processing of Facelets.
-----------------------------------------------------------------------------------------
              <process-as>         <process-as>         <process-as>       <process-as>
              html5</process-as>   xhtml</process-as>   xml</process-as>   jspx</process-as>
              HTML 5 (default)     Facelets XHTML       XML View           Facelets JSPX
-----------------------------------------------------------------------------------------
XML Doctype   Simplified to        passed through       consumed           consumed
              <!DOCTYPE html>  

XML           passed through       passed through       consumed           consumed
declaration 

Processing    passed through       passed through       consumed           consumed
instructions

CDATA         passed through       passed through       consumed           consumed
section

Escaping of   escaped              escaped              escaped            not escaped
inline text    

XML           passed through       passed through       consumed           consumed
Comments 

In the preceding table, “passed through” means that the content is passed through unmodified to the user agent. “consumed” means the content is silently consumed on the server. Note that for CDATA sections, the content of the CDATA section itself is passed through, even if the start and end tags should be consumed. “escaped” means that sensivite content in the response is automatically escaped: & becomes &amp;, for example. “not escaped” means that such content is not escaped.

In other words, when you're authoring HTML5/XHTML, you have to remove it yourself. A better wording is actually: you shouldn't be including the XML prolog yourself in HTML5 and XHTML pages as that's not required; it's only required in XML and JSPX pages (and thus Facelets will automatically remove it).

See also:


Unrelated to the concrete problem, you should be using <h:outputStylesheet> instead of <link rel="stylesheet"> to be independent from the request URL.

<h:outputStylesheet name="css/default.css" />
<h:outputStylesheet name="css/cssLayout.css" />

See also:

Seaboard answered 22/5, 2012 at 16:28 Comment(6)
with <process-as>xml</process-as>, the generated HTML lacks the HTML5 doctype declarationEldwon
@mjn: indeed, it's consumed. If you want to produce HTML5, just stick to default and get rid of all XML prologs.Seaboard
can you reproduce the problem in the question?Eldwon
Yes, the presence of the XML prolog will cause that. Just get rid of it. It doesn't belong in HTML5 targeted pages. It will only be removed from includes and tagfiles, but not from (template) clients. See also last paragraph in answer.Seaboard
It seems to work in GlassFish Server Open Source Edition 4.0 (89) when I remove the XML prolog. I will check in a different environment with the current GlassFish 4.1.Eldwon
In the GlassFish 4.1 release version (b13) and the promoted build b16 removing the prolog shows the expected effect.Eldwon
B
3

In order to get the XML declaration to not appear on my rendered pages, I found that I could configure JSF to process my .xhtml files as XML. When processing in XML mode, the xml declaration would not pass through from my source files to the output. I have not yet noticed any other side effects to making this change. (But if I find any, I will follow up here.)

To make the config change, I added the following to my faces-config.xml:

<faces-config-extension>
    <facelets-processing>
        <file-extension>.xhtml</file-extension>
        <process-as>xml</process-as>
    </facelets-processing>  
</faces-config-extension>    

Hope this helps

Beeswax answered 12/12, 2012 at 16:37 Comment(4)
I could not find FACELETS_SUPPRESS_XML_DECLARATION in the JSF 2 documentation, are you sure it exists?Eldwon
His answer does not say otherwise.Seaboard
It must have been an experiment by the spec team that I picked up on that didn't make it into production releases. I found a couple of links on google that make ancillary references to javax.faces.FACELETS_SUPPRESS_XML_DECLARATION (like this: lists.jboss.org/pipermail/jsr-314-open-mirror/2010-August/… ), but nothing recent.Beeswax
I removed the bit about javax.faces.FACELETS_SUPPRESS_XML_DECLARATION, which was misleading and not part of the actual answer.Beeswax

© 2022 - 2024 — McMap. All rights reserved.