Android上的XSLT转换

时间:2017-07-21 22:25:27

标签: android xslt xalan exslt msxsl

我正在尝试将XSLT转换为Android上的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="*"/>

1 个答案:

答案 0 :(得分:0)

这不是一个答案,只是一种验证你的假设的方法。

运行以下两个样式表(针对任何有效的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>