1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I added Saxon-HE v9 to my project and all works as expected. So it does seem to be the transformer embedded in Java that is causing the problem.
Thanks @MathiasMuller for the pointer, although I'm still none the wiser as to why it doesn't work without Saxon.
@PeteBrew If you'd like to find out, you have to include the Java code in your question. That's not my field of expertise, but I'm sure someone could tell you what's wrong. Saxon is the most reliable XSLT processor and would always be my first choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.