Running XSLT Transform with Java with a parameter

I am performing XSLT conversion from my java web application without problems:

Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.transform(xmlInput, xmlOutput);

In my XSLT transformation, I now add a function call document()to load a RESTful web service response:

<!-- do stuff -->
<xsl:variable name="url">
   http://server/service?id=<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:call-template name="doMoreStuff">
   <xsl:with-param name="param1" select="document($url)/foo"/>
</xsl:call-template>

Good cool, no problem. But now I want to read the base URL from the utils class in java and pass it to the stylesheet.

//java
String baseUrl = myUtils.getBaseUrl();

<!-- xslt -->
<xsl:variable name="url">
   <xsl:value-of select="$baseUrl"/>
   <xsl:text>/service?id=</xsl:text>
   <xsl:value-of select="@id"/>
</xsl:variable>

Any suggestion on how to do this? My Java utils class loads the value from the myApp.properties file into the classpath, but I'm not sure I can use this from XSLT ...

+5
source share
2 answers

Declare xsl:paramin the stylesheet so that the value baseUrlcan be passed during the call:

<xsl:param name="baseUrl" />

Transformer:

Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.setParameter('baseUrl', myUtils.getBaseUrl());
transformer.transform(xmlInput, xmlOutput);

XSLT 2.0, resolve-uri(), url :

<xsl:variable name="url" 
              select="resolve-uri(concat('/service?id=', @id), $baseUrl)" />

resolve-uri() , baseUrl, URL-, $baseUrl @id.

+6

setParameter Transformer . XSLT <xsl:param name="yourParamName" />, XSLT, , : <xsl:value-of select="$yourParamName" />

+6

All Articles