0

I'm having trouble to figure out a way to give URI from classpath in <xsl:import href="uri"/>. I need to give classpath uri of an xsl file in sample_project (gradle structure) located in resources folder. The import runs fine with Path from Repository Root in local environment but gives IO exception in development env. Hence, thinking of classpath solution for import. I'm using XSLT 1.0 processor and Xalan.

sample_gradle_project
-src/main
-- java
-----service
--------- class.java
-- resources
----- Afolder
---------- step.xsl

class.java

import org.apache.commons.lang.StringEscapeUtils;                   
import org.apache.commons.io.IOUtils;                                         
import java.io.InputStream;
import javax.xml.transform.Transformer;           
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

String unescaped_xsl = StringEscapeUtils.unescapeXml("&lt;xsl:import href = &quot;src/main/resources/Afolder/step.xsl&quot;/&gt;");

InputStream inputStream = IOUtils.toInputStream(unescaped_xsl);           
Transformer transformer = transformerFactory.newTransformer(new StreamSource(inputStream));`

step.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
Article - <xsl:value-of select="/Article/Title"/>
Authors: <xsl:apply-templates select="/Article/Authors/Author"/>
</xsl:template>
</xsl:stylesheet>

It works fine with Repository root path, but I want to instead give path with reference from classpath. How to give that path in import href of xsl?

In development environment, I'm getting

Had IO Exception with stylesheet file: src/main/resources/Afolder/step.xsl
sheenaLal
  • 3
  • 1
  • 7
  • Show the code for how your are loading the top-level XSLT that has the import, and provide some information about where that XSLT lives. Is it bundled in a jar or is it laying in a directory on the filesystem? – Mads Hansen Oct 07 '22 at 12:58
  • Possibly relevant, but hard to tell without more information: https://stackoverflow.com/a/3700296/14419 – Mads Hansen Oct 07 '22 at 12:59
  • @MadsHansen can you pls help here ? Added detailed description. – sheenaLal Oct 09 '22 at 13:49
  • Have you tried setting the SystemId for the SteamSource, to say "where" that XSLT should be resolving relative paths for the import (as indicated in the link I posted above)? Also, why load an XSLT that is just an import? Why not just load THAT XSLT to run? (or is this just a simplified version to demonstrate the issue)? – Mads Hansen Oct 09 '22 at 13:58
  • 1
    yeah that was a simplified version for demonstration purpose. Yeah setting SystemId did worked !!! Thanks a lot @Mads Hansen :) _/\_ – sheenaLal Oct 09 '22 at 14:52
  • @MadsHansen can you add your "SystemId" suggestion as an answer so it can be accepted? Better than having it buried here in comments. – Conal Tuohy Oct 10 '22 at 05:00

0 Answers0