ka3ak ka3ak - 3 months ago 17
HTML Question

Why do I have to close <br> tag in XSL?

I do XSLT from time to time. So I may not understand something about it.

Not sure whether an example is necessary but here it is:
The XML is very simple:



<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="a"/>
<xsl:template match="a">

Java code:

public static String transform(String xml, String xsl) throws TransformerException {

StreamSource xslT = new StreamSource(new StringReader(xsl));

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(xslT);

StreamSource source = new StreamSource(new StringReader(xml));

StringWriter out = new StringWriter();
StreamResult result = new StreamResult(out);

transformer.transform(source, result);
return out.toString();


I thought that if I use
as output type in the stylesheet then it should recognize
as a valid html tag.

And in general... Why does it care for html validity at all? I've also noticed that if I declare a variable. E.g.:

<xsl:variable name="someTable">
<table class="common" bgcolor="#DDDDDD">

the same error will be reported.

My goal is not a creation of an invalid html. I just want to declare a variable (I'll close
depending on the processing logic somewhere later).

Is there a way to workaround this or do I have to use
as the output type.


If the output method is html, then the processor will output <br/> as <br>. However, both the input and the stylesheet must be well-formed XML documents.

As for your question regarding the variable: you cannot store only a start-tag in a variable. You must store an entire element (which at this point exists only as an abstraction, and has no tags).

If you find yourself "needing" to store a start-tag which you'll "close depending on the processing logic somewhere later", then you need to reconsider your approach. It's not a matter of finding a workaround; that's just not how XSLT works.