我对一个简单的 XSL 转换很生气。经过大量测试后,我发现它依赖于 Xalan(因为 Saxon 可以工作)。我已经强制 Java 使用 Xalan 处理器,所以我敢肯定。
唯一匹配的规则是“身份规则”,而其他“匹配”没有完成。如果我使用撒克逊,一切正常!
你能看出这个 XSL 转换有什么问题吗?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xhtml xsl">
<xsl:output method="xml" omit-xml-declaration="no"
media-type="text/xml" indent="yes" encoding="UTF-8"
doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
<xsl:template match="/">
<xsl:message>root</xsl:message>
<xsl:apply-templates select="xhtml:html" />
</xsl:template>
<xsl:template match="xhtml:html">
<xsl:element name="html">
<xsl:attribute name="xml:lang">it</xsl:attribute>
<xsl:apply-templates select="xhtml:head|xhtml:body" />
</xsl:element>
<xsl:message>xhtml:html</xsl:message>
</xsl:template>
<xsl:template match="xhtml:head">
<xsl:element name="head">
<xsl:apply-templates select="xhtml:meta" />
<title>
<xsl:value-of select="xhtml:title" />
</title>
<link href="%stile.css%" rel="stylesheet" type="text/css" />
<xsl:apply-templates select="xhtml:script" />
</xsl:element>
<xsl:message>xhtml:head</xsl:message>
</xsl:template>
<xsl:template match="xhtml:body">
<xsl:element name="body">
<xsl:apply-templates select="descendant::xhtml:div[@class='sxSmall']" />
</xsl:element>
<xsl:message>xhtml:body</xsl:message>
</xsl:template>
<xsl:template match="xhtml:script">
<xsl:message>xhtml:script</xsl:message>
</xsl:template>
<xsl:template match="xhtml:meta">
<xsl:message>xhtml:meta</xsl:message>
</xsl:template>
<!-- identity -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<xsl:message>identita</xsl:message>
</xsl:template>
<xsl:template match="xhtml:div[@class='sxSmall']">
<xsl:element name="div">
<xsl:attribute name="class"><xsl:value-of select="@class" /></xsl:attribute>
<xsl:apply-templates />
</xsl:element>
<xsl:message>xhtml:div</xsl:message>
</xsl:template>
</xsl:stylesheet>
--------------更新----------------
问题与 Xalan 以及我将 XML 源代码放入其中的方式有关。Java 代码如下:
StringReader srXslContent = new StringReader(xslContent);
TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
Transformer transformer = null;
try
{
transformer = tFactory.newTransformer(new StreamSource(srXslContent));
Context.getInstance().getLogger().debug("Transformer created!");
} catch (TransformerConfigurationException e)
{
Context.getInstance().getLogger().error(ExceptionUtils.getStackTrace(e));
}
StringWriter sw = new StringWriter();
Document doc = XmlUtils.parseXmlFile(xmlContent);
DOMSource domSource = new DOMSource(doc.getDocumentElement());
try
{ // Could be this?
transformer.transform(domSource, new StreamResult(sw));
// with streamsource Xalan works fine!!!
//transformer.transform(new StreamSource(new StringReader(xmlContent)), new StreamResult(sw));
Context.getInstance().getLogger().debug("Transformation made!");
} catch (TransformerException e)
{
Context.getInstance().getLogger().error(ExceptionUtils.getStackTrace(e));
}
parseXml 方法在这里:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
dbf.setValidating(false);
// Sax
dbf.setFeature("http://xml.org/sax/features/validation", false);
// Xerces: to disable Internet searching...
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(contentToParse));
Document document =db.parse(is);
return document;
重要的是要注意,如果我将 XML 源作为字符串(通过 StringReader 和 StreamSource)提供,它也适用于 Xalan。我开始怀疑问题出在 DOMSource ...