简体   繁体   English

XSLT 2.0 xsl:value-of separator属性在Java 1.7中与apache.xalan不兼容

[英]XSLT 2.0 xsl:value-of separator attribute not working in Java 1.7 with apache.xalan

The XSLT 2.0 xsl:value-of element separator attribute appears to not be working with the built-in Java 1.7 XSLT processor. XSLT 2.0 xsl:value-of element separator属性似乎不适用于内置的Java 1.7 XSLT处理器。 Here's the example (slightly edited from Chapter 4 of XSLT 2nd ed. to reduce size): 这是一个例子(从XSLT第2版的第4章略微编辑以减小大小):

XML file XML文件

<?xml version="1.0" encoding="utf-8"?>
<cars>
    <manufacturer name="Chevrolet">
    </manufacturer>
    <manufacturer name="Ford">
    </manufacturer>
    <manufacturer name="Volkswagen">
    </manufacturer>
</cars>  

XSLT file XSLT文件

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
         <xsl:value-of select="cars/manufacturer/@name" separator=", "/>
    </xsl:template>
</xsl:stylesheet>

Java code Java代码

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class FooMain {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        System.out.println("transformer factory class: "+factory.getClass()); // line-a
        Source xslt = new StreamSource(new File("transform.xslt"));
        Transformer transformer = factory.newTransformer(xslt);                                                                                                                                                                     
        Source text = new StreamSource(new File("input.xml"));
        transformer.transform(text, new StreamResult(new File("output.html")));
    }
}

When the code runs, it produces the following output file: 代码运行时,会生成以下输出文件:

Chevrolet

... as opposed to what the book describes: ......而不是书中描述的内容:

Chevrolet, Ford, Volkswagen

I am using Java 1.7 in Ubuntu Precise and the classname of the TransformerFactory instance is reported (in line-a of the Java code above) as: 我在Ubuntu Precise中使用Java 1.7,并报告了TransformerFactory实例的类名(在上面的Java代码的a行中 ):

com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

UPDATE UPDATE

I experimented and renamed the separator attribute in the XSLT file to separatorBOO and the code continues to run without complaining and produces the exact same output. 我试验并将XSLT文件中的separator属性重命名为separatorBOO ,代码继续运行而不会抱怨,并产生完全相同的输出。 So it would seem that the XSLT 2.0 separator attribute is not recognized at all ?? 所以似乎根本无法识别XSLT 2.0 分隔符属性?

You have to provide another factory as Xalan does not support xslt2 transformations. 您必须提供另一个工厂,因为Xalan不支持xslt2转换。 Try to use Saxon. 尝试使用Saxon。 I guess you only have to drop the jar into your classpath as Java is using it's embedded version if there's no provider in the classpath. 我猜你只需要将jar放入你的类路径,因为如果类路径中没有提供者,Java正在使用它的嵌入式版本。

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

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