Question

The XSLT 2.0 xsl:value-of element separator attribute appears to not be working with the built-in Java 1.7 XSLT processor. Here's the example (slightly edited from Chapter 4 of XSLT 2nd ed. to reduce size):

XML file

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

XSLT file

<?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

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:

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

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. So it would seem that the XSLT 2.0 separator attribute is not recognized at all ??

Was it helpful?

Solution

You have to provide another factory as Xalan does not support xslt2 transformations. Try to use 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top