Android 上的 XSLT 转换



我正在尝试在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 代码段

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="*"/>

这(还(不是答案,只是验证你的假设的一种方式。

运行以下两个样式表(针对任何有效的 XML 输入(并报告结果:

样式表 #1

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan" 
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xalan exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<results>
<function name="xalan:nodeset">
<xsl:value-of select="function-available('xalan:nodeset')"/>
</function>
<function name="exsl:node-set">
<xsl:value-of select="function-available('exsl:node-set')"/>
</function>     
</results>
</xsl:template>     
</xsl:stylesheet>

样式表 #2

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan" 
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xalan exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:variable name="items">
<item>A</item>
<item>B</item>
<item>C</item>
</xsl:variable>
<xsl:template match="/">
<results>
<test name="xalan:nodeset">
<xsl:copy-of select="xalan:nodeset($items)/item[2]"/>   
</test>
<test name="exsl:node-set">
<xsl:copy-of select="exsl:node-set($items)/item[2]"/>   
</test> 
</results>
</xsl:template>     
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新