简体   繁体   English

Java XSLT无法正常工作

[英]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. 我试图在Java中运行XSL转换,以更改某些XML文件上的名称空间URI。 Following some research I worked out the following XSL: 经过研究,我得出了以下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: 我在此工具和其他在线翻译器工具上进行了尝试,它们均按预期工作,因此当我向其提供以下非常简单的XML文件时:

<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中运行相同的翻译时,我得到了:

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? 是Java还是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. 如果我从XSL文件中删除了更改命名空间的代码,并添加了一些非面向命名空间的更改,则Java运行良好,因此我认为运行翻译的方式没有任何问题。

I've seen mention in various posts that the embedded translator in Java sucks. 我在各种文章中都提到Java的嵌入式翻译很烂。 Is this an example of this? 这是一个例子吗?

Is the problem in Java or my XSL? 是Java还是XSL中的问题?

Your XSLT stylesheet is fine with Saxon 6.5, Saxon 9.5 and Xalan 2.7. 您的XSLT样式表适合Saxon 6.5,Saxon 9.5和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. 如果我从XSL文件中删除了更改命名空间的代码,并添加了一些非面向命名空间的更改,则Java运行良好,因此我认为运行翻译的方式没有任何问题。

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. 这不一定意味着您的Java代码是错误的,但是实现可能在某处出错,并且运行时感知可能是由实际的错误引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM