Hans
Hans

Reputation: 165

java xslt transformation local-name

I have a xslt stylesheet which should do some generic transformation. The relevant part is

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="myForm/*">
    <xsl:element name="formularfeld">
        <xsl:attribute name="name">
            <xsl:value-of select="fn:local-name()" />
        </xsl:attribute>
        <xsl:value-of select="." />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

The stylesheet works as expected when I run in XMLSpy. However when I call from Java 8 SE or ServiceMix 7 I get ERROR: 'Klasse "xpath-functions" kann nicht gefunden werden.' Externe Methode "xpath-functions.localName" kann nicht gefunden werden (muss "public" sein). FATAL ERROR: 'Externe Methode "xpath-functions.localName" kann nicht gefunden werden (muss "public" sein).' (Class xpath-functions cannot be found. External method xpath-funckions.localName cannot be found)

        StreamResult streamResult = new StreamResult(xmlOutWriter);
        Reader inputReader = new StringReader(input);
        Reader xsltReader = new StringReader(stylesheet);
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(xsltReader);
        Transformer transformer = factory.newTransformer(xslt);

        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        transformer.setParameter("timestamp", timestamp);
        Source inputSource = new StreamSource(inputReader);
        transformer.transform(inputSource, streamResult);

Upvotes: 0

Views: 109

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

Apparently you are using an XSLT 1.0 processor. In XSLT 1.0, there is no namespace for functions. Even in XSLT 2.0 the namespace is optional. You can simply change:

<xsl:value-of select="fn:local-name()" />

to

<xsl:value-of select="local-name()" />

and remove the xmlns:fn="http://www.w3.org/2005/xpath-functions" declaration.

Upvotes: 1

Related Questions