PeteBrew
PeteBrew

Reputation: 434

Java XSLT not working as expected

I'm trying to run an XSL translation in Java to change the namespace URI on some XML files. Following some research I worked out the following XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tridasold="http://www.tridas.org/1.2.2"
    xmlns:t="http://www.tridas.org/1.2.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@tridasold:*">
        <xsl:attribute name="t:{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="tridasold:*">
        <xsl:element name="t:{local-name()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

I tried it out on this and other online translator tools and it all works as I expected so that when I provide it with the following very simple XML file:

<project xmlns="http://www.tridas.org/1.2.2">
    <title>title0</title>
</project>

...it returns this:

<t:project xmlns:t="http://www.tridas.org/1.2.3">
   <t:title>title0</t:title>
</t:project>

However, when I try to run the same translation in Java I'm getting:

java.lang.RuntimeException: Namespace for prefix 't' has not been declared.
at com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary.runTimeError(BasisLibrary.java:1603)
at com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary.runTimeError(BasisLibrary.java:1607)
at com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary.startXslElement(BasisLibrary.java:1490)
at GregorSamsa.template$dot$2()
at GregorSamsa.applyTemplates()
at GregorSamsa.applyTemplates()
at GregorSamsa.transform()
at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:617)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:748)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:359)

Is the problem in Java or my XSL? If I remove the namespace changing code from the XSL file and add some non-namespace oriented changes, Java runs fine so I don't think there is anything wrong with the way I'm running the translation.

I've seen mention in various posts that the embedded translator in Java sucks. Is this an example of this?

Upvotes: 1

Views: 1028

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22637

Is the problem in Java or my XSL?

Your XSLT stylesheet is fine with Saxon 6.5, Saxon 9.5 and Xalan 2.7. Are you sure that you apply exactly this stylesheet to exactly this input?

If I remove the namespace changing code from the XSL file and add some non-namespace oriented changes, Java runs fine so I don't think there is anything wrong with the way I'm running the translation.

I am certain there's something wrong with it. That does not necessarily mean your Java code is wrong, but the implementation might err somewhere and the runtime exeception might be caused by an actual bug.

Upvotes: 1

Related Questions