Zee
Zee

Reputation: 103

XSLT transformation on Android

I am trying to transform XSLT into HTML on Android, someone recommended XALAN. I was able to get the xalan jar and jarjar(renaming packages) it, this successfully transformed the xslt with 1 error. The error is that one of the divs isn't rendered in the html output.

I think the reason for the error is when the transformer hits the ms-xsl node-set, it not able to parse it and throws the declared error.

Snippet

<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>

Full XSLT file: https://pastebin.com/enEs6PHb

EDIT:

XSL file code block throwing error

<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>

ERROR MESSAGE : SystemId Unknown; Line #1842; Column #5; WARNING: missing required function node-set, this xslt processor may not work correctly

FULL XML file: https://pastebin.com/2Za4Vq2m

Java Snippet that does the transformation

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

Upvotes: 0

Views: 986

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

This is not (yet) an answer, just a way to verify your assumption.

Run the following two stylesheets (against any valid XML input) and report the results:

Stylesheet #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>

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>

Upvotes: 0

Related Questions