我正在尝试在 Android 上将 XSLT 转换为 HTML,有人推荐 XALAN。我能够得到 xalan jar 和 jarjar(重命名包)它,这成功地转换了 xslt,出现 1 个错误。错误是其中一个 div 未在 html 输出中呈现。
我认为错误的原因是当转换器遇到ms-xsl节点集时,它无法解析它并抛出声明的错误。
片段
<xsl:when test="function-available('msxsl:node-set')">
<xsl:apply-templates select="msxsl:node-set($mixinRtf)/*"/>
</xsl:when>
<xsl:otherwise>
<!-- XSLT 2 would be thus: xsl:apply-templates select="$mixinRtf/*"/ -->
<xsl:message terminate="yes">required function node-set is not available, this XSLT processor cannot handle the transform</xsl:message>
</xsl:otherwise>
完整的 XSLT 文件:https ://pastebin.com/enEs6PHb
编辑:
XSL 文件代码块抛出错误
<xsl:when test="function-available('msxsl:node-set')">
<xsl:variable name="sortedTokensRtf">
<xsl:for-each select="msxsl:node-set($splitRtf)/token">
<xsl:sort select="@value"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:call-template name="uniqueStyleCodes">
<xsl:with-param name="in" select="msxsl:node-set($sortedTokensRtf)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- this one below should work for all parsers as it is using exslt but will keep the above code for msxsl for now -->
<xsl:message>WARNING: missing required function node-set, this xslt processor may not work correctly</xsl:message>
<xsl:for-each select="str:tokenize($allCodes, ' ')">
<xsl:sort select="."/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:otherwise>
错误信息 :SystemId Unknown; Line #1842; Column #5; WARNING: missing required function node-set, this xslt processor may not work correctly
完整的 XML 文件:https ://pastebin.com/2Za4Vq2m
进行转换的 Java Snippet
Source xmlSource = new StreamSource(new BufferedInputStream(getAssets().open("sample1.xml")));
Source xsltSource = new StreamSource(new BufferedInputStream(getAssets().open("spl.xsl")));
xmlSource.setSystemId(xmlSource.getSystemId());
xsltSource.setSystemId(xsltSource.getSystemId());
TransformerFactoryImpl transFact = new TransformerFactoryImpl();
transFact.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base) throws TransformerException {
try {
return new StreamSource(new BufferedInputStream(getAssets().open(href)));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
});
TransformerImpl trans = (TransformerImpl) transFact.newTransformer(xsltSource);
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/mydata.html");
StreamResult result = new StreamResult(f);
trans.transform(xmlSource, result);
sql.xsl
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v3="urn:hl7-org:v3" exclude-result-prefixes="v3 xsl">
<xsl:import href="spl-common.xsl"/> --------------------->PASTE BIN FILE
<!-- Where to find JavaScript resources -->
<xsl:param name="resourcesdir">http://www.accessdata.fda.gov/spl/stylesheet/</xsl:param>
<!-- Whether to show the clickable XML, set to "/.." instead of "1" to turn off -->
<xsl:param name="show-subjects-xml" select="/.."/>
<!-- Whether to show the data elements in special tables etc., set to "/.." instead of "1" to turn off -->
<xsl:param name="show-data" select="1"/>
<!-- This is the CSS link put into the output -->
<xsl:param name="css">spl.css</xsl:param>
<!-- Whether to show section numbers, set to 1 to enable and "/.." to turn off-->
<xsl:param name="show-section-numbers" select="/.."/>
<!-- Whether to process mixins -->
<xsl:param name="process-mixins" select="true()"/>
<xsl:param name="core-base-url">http://www.accessdata.fda.gov/spl/core</xsl:param>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>