开发者

How to make javax Transformer output HTML (no self-closing tags)?

开发者 https://www.devze.com 2023-03-03 15:44 出处:网络
I\'m using a javax.xml.transform.Transf开发者_StackOverflow社区ormer to convert an XML file into an HTML file. It can happen that a div will have no content, which causes the Transformer to output <

I'm using a javax.xml.transform.Transf开发者_StackOverflow社区ormer to convert an XML file into an HTML file. It can happen that a div will have no content, which causes the Transformer to output <div/>, which breaks rendering.

I've searched and found that "You can change the xslt output to html instead of xml to avoid the problem with self closing tags", but that was for a different tool and I'm left wondering: how do I do that with a javax Transformer?


It looks like you create the transformer as normal, and then use Transformer.setOutputProperty to set the METHOD property to "html"


For example:

private static final DocumentBuilderFactory sDocumentFactory;
private static DocumentBuilder sDocumentBuilder;
private static DOMImplementation sDomImplementation;

private static final TransformerFactory sTransformerFactory =
  TransformerFactory.newInstance();
private static Transformer sTransformer;

static {
  sDocumentFactory = DocumentBuilderFactory.newInstance();

  sDocumentFactory.setNamespaceAware( true );
  sDocumentFactory.setIgnoringComments( true );
  sDocumentFactory.setIgnoringElementContentWhitespace( true );

  try {
    sDocumentBuilder = sDocumentFactory.newDocumentBuilder();
    sDomImplementation = sDocumentBuilder.getDOMImplementation();
    sTransformer = sTransformerFactory.newTransformer();

    sTransformer.setOutputProperty( OMIT_XML_DECLARATION, "yes" );
    sTransformer.setOutputProperty( INDENT, "no" );
    sTransformer.setOutputProperty( METHOD, "html" );
  } catch( final Exception ex ) {
    ex.printStackTrace();
  }
}


The way to output valid HTML with XSLT is to use the <xsl:output> instruction with its method attribute set to html.

Here is a small example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <div>
       <xsl:apply-templates select="x/y/z"/>
     </div>
 </xsl:template>

</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t/>

the wanted result is produced (the same result is produced by 8 different XSLT processors I am working with):

<div></div>

In case the unwanted output happens only with a specific XSLT processor, then this is an implementation issue with this particular processor and more an "xsltprocessors" than "xslt" question.


This answer in another thread doesn't seem to work in my case; even if I specify <xsl:output method="html"...> it still produces <div/> instead of <div></div>.

I don't know if my IDE or compiler is broken (IBM Rational Application Developer), but I'm using a work-around of detecting blank nodes and inserting single spaces in them. Less clean, but effective...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号