Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ext/java/nokogiri/XsltStylesheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import nokogiri.internals.NokogiriXsltErrorListener;

Expand Down Expand Up @@ -185,7 +186,23 @@ public class XsltStylesheet extends RubyObject

java.util.Properties props = this.sheet.getOutputProperties();
if (props.getProperty(OutputKeys.METHOD) == null) {
props.setProperty(OutputKeys.METHOD, org.apache.xml.serializer.Method.UNKNOWN);
Node rootNode = xmlDoc.getNode().getFirstChild();

if (rootNode != null && rootNode.getNodeType() == Node.ELEMENT_NODE) {
String firstChildLocalName = rootNode.getLocalName();

if (firstChildLocalName.equalsIgnoreCase("html") && rootNode.getNamespaceURI() == null) {
Node textNode = rootNode.getPreviousSibling();

if (textNode == null || (textNode.getNodeType() == Node.TEXT_NODE && textNode.getTextContent().trim().isEmpty())) {
props.setProperty(OutputKeys.METHOD, "html");
}
}
}

if (props.getProperty(OutputKeys.METHOD) == null) {
props.setProperty(OutputKeys.METHOD, "xml");
}
}

Serializer serializer = SerializerFactory.getSerializer(props);
Expand All @@ -195,6 +212,7 @@ public class XsltStylesheet extends RubyObject
return context.getRuntime().newString(writer.toString());
}


@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject
transform(ThreadContext context, IRubyObject[] args)
Expand Down
50 changes: 50 additions & 0 deletions test/xslt/test_xml_header.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "minitest/autorun"
require "nokogiri"

class OutputTest < Minitest::Test
def test_output
input_xml = <<~XML
<?xml version="1.0" encoding="utf-8"?>
<report>
<title>My Report</title>
</report>
XML

input_xsl = <<~XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="report/title"/></title>
</head>
<body>
<h1><xsl:value-of select="report/title"/></h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSL

expected_output = <<~HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My Report</title>
</head>
<body><h1>My Report</h1></body>
</html>
HTML

xml = Nokogiri::XML(input_xml)
xsl = Nokogiri::XSLT(input_xsl)
actual_output = xsl.apply_to(xml)

expected_output_normalized = expected_output.gsub(/\s+/, "").downcase
actual_output_normalized = actual_output.gsub(/\s+/, "").downcase

assert_equal(expected_output_normalized, actual_output_normalized)
end
end