Announcement Announcement Module
No announcement yet.
Example How to load xslt files from classpath Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example How to load xslt files from classpath

    Loading of xslt files is done by URI using xslt-transformer. The URI resolution is used by xslt file itself and by xsl:import, xsl:include. Loading the xslt file and xsl:import/include have been changed to use ClassLoader.

    Using ClassLoader to load xslt file and define the Resolver for xsl:import/include:

    <bean id="myResolver" class="MyResolver"/>
    <bean id="transformerFactory" class="javax.xml.transform.TransformerFactory" factory-method="newInstance">
    <property name="URIResolver" ref="myResolver"/>
    <bean id="classLoader" class="java.lang.ClassLoader" factory-method="getSystemClassLoader"/>
    <bean id="fpmlStream" class="" factory-bean="classLoader" factory-method="getResourceAsStream">
    <constructor-arg type="java.lang.String" value="fpml-2-target-v2-0.xslt"/>
    <bean id="fpmlSource" class="">
    <constructor-arg type="" ref="fpmlStream"/>
    <bean id="fpmlTemplates" factory-bean="transformerFactory" factory-method="newTemplates">
    <constructor-arg ref="fpmlSource"/>
    <spi-xml:xslt-transformer input-channel="fpmlChannel" output-channel="outputChannel" xsl-templates="fpmlTemplates"/>

    The Resolver for xsl:import/include tries to find the xslt file inside classpath:


    import javax.xml.transform.Source;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.URIResolver;

    import org.apache.log4j.Logger;

    public class MyResolver implements URIResolver
    private static Logger log = Logger.getLogger(MyResolver.class);

    public Source resolve(String href, String base) throws TransformerException
    log.debug("href=" + href + " : base=" + base);

    String fileName = null;

    try {
    int k = href.lastIndexOf('/') + 1;
    if (k > 0) {
    fileName = href.substring(k);
    } else {
    fileName = href;

    InputStream input = ClassLoader.getSystemResourceAsStream(fileName);
    if (input != null) {
    return (new StreamSource(input));
    } catch (Exception ie) {
    log.error("File " + fileName + " not found!", ie);

    // instruct the caller to use the default lookup
    return (null);

  • #2
    I've seen a bug that seems related to this (, but I'm not getting your point. Are you just stating what happens, or did you expect something else, or did you patch this bug?


    • #3
      I was just stating what happens.

      Apps like Xerces, Xalan, Saxon, etc. do not support classpath as an URI scheme. Therefore I filed an example how to do it.

      Having a second thought:
      Should a classpath scheme be added to Entity- and URIResolver?
      If yes, should this support be added to Spring or Xerces, Xalan and Saxon?