Skip to content

Honour xsl:output/@method: add html, xhtml and text serialization - #15

Open
pReya wants to merge 2 commits into
egh:mainfrom
pReya:html-output-method
Open

pReya wants to merge 2 commits into
egh:mainfrom
pReya:html-output-method

Conversation

@pReya

@pReya pReya commented Nov 1, 2026

Copy link
Copy Markdown

Fixes #13.

What was wrong

  • serialize() always used slimdom's XMLSerializer, so method="html" produced <div/>, entity-escaped <script>/<style> content and no doctype. Browsers parse that as nested unclosed <div>s and the inline JavaScript breaks.
  • The unnamed xsl:output declaration was never applied to the default result document at all: outputDefinitions was only consulted by xsl:result-document format="...". So even omit-xml-declaration="yes" had no effect on the main output (xjslt run printed the XML declaration regardless).

What this PR does

src/serialize.ts (new) – serializers for the html, xhtml and text output methods, following the XSLT/XQuery Serialization spec:

  • html: <!DOCTYPE html> (or the doctype built from doctype-public/doctype-system), no XML declaration; void elements written as <br> with no end tag, other empty elements as <div></div>; script/style content written raw; boolean attributes minimized (checked="checked" → checked); < not escaped in attribute values; <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> inserted into head unless include-content-type="no"; processing instructions terminated with >. Elements in the XHTML namespace are treated as HTML elements, elements in other namespaces (SVG, MathML) are written as XML. Namespace declarations are emitted the same way XMLSerializer would.
  • xhtml: XML syntax with the HTML conventions (<br />, <div></div>, content-type meta, doctype).
  • text: the string value of the result tree.
  • If no method is declared and the document element is html in no namespace, the html method is used, as XSLT 2.0 §20 requires.

Output definitions

  • method and include-content-type are read from xsl:output and xsl:result-document.
  • The compiled transform now applies the unnamed output definition to the #default result (applyDefaultOutputDefinition), and xsl:result-document without format also falls back to the unnamed definition, as the spec says.
  • mkOutputDefinition only sets the options that were actually specified, so definitions can be layered without an unspecified option (e.g. standalone) clobbering a specified one. Several unnamed xsl:output elements are now actually merged (the merge branch in compileOutputNode was dead code: name was never falsy), later ones overriding earlier ones per option.
  • Named output definitions are matched by expanded QName, so xsl:output name="my:f" and xsl:result-document format="other:f" match when both prefixes are bound to the same namespace. An unknown format raises XTDE1460 instead of being silently ignored.

The xml method is unchanged.

🤖 Generated with Claude Code

pReya and others added 2 commits November 1, 2026 00:09
serialize() always used slimdom's XMLSerializer, so method="html"
produced self-closed <div/> elements, entity-escaped script/style
content and no doctype, which browsers render incorrectly.

The unnamed xsl:output declaration was also never applied to the
default result document; only xsl:result-document with a format
consulted the output definitions.

- Add src/serialize.ts with serializers for the html, xhtml and text
  output methods (doctype, void elements, raw script/style content,
  minimized boolean attributes, content-type meta, namespace fixup).
- Read method and include-content-type from xsl:output and
  xsl:result-document.
- Apply the unnamed output definition to the #default result, and use
  it as the base for xsl:result-document without a format.
- mkOutputDefinition only sets options that were specified, so
  definitions layer correctly. Multiple unnamed xsl:output
  declarations are now actually merged (the merge branch was dead
  code), later ones overriding earlier ones.
- Match named output definitions by expanded QName and raise
  XTDE1460 for an unknown format.
- Choose the html method by default when the document element is
  html in no namespace (XSLT 2.0 section 20).
- Add test/serialize.spec.ts. Eleven tests of the W3C suite now pass
  (output-0111, 0113, 0154, 0172, result-document-0214, 0215, 0230,
  0236, 0701, bug-0304, error-1460a).

Fixes egh#13

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Moritz Stückler <moritz@bitbetter.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xsl:output method="html" is ignored: output is always serialized as XML

1 participant