Project

Profile

Help

Different result between java and command line when calling named templates based on a variable

Added by Andy Walshe almost 6 years ago

Hi,

Attached I have simple test files investigating whether I could use a suggestion from Stackoverflow (https://stackoverflow.com/questions/1233702/how-to-call-named-templates-based-on-a-variable) and it seems to be working from the command line but not from a simple java test class.

I am using XSLT 2.0 and endeavoring to reduce footprint size of a large stylesheet by calling named templates based on a variable.

To run CLI:

$ java -jar Saxon-HE-9.7.0-15.jar -xsl:call-named-template.xsl -s:simple.xml

Output:

<FirstElem xmlns="urn:my:test:stuff">
   <SecondLevelElem/>
   <SecondAgain/>
</FirstElem>

Running the java class:

$ javac -classpath Saxon-HE-9.7.0-15.jar SaxonTestMain.java

$ java -classpath Saxon-HE-9.7.0-15.jar:. SaxonTestMain Output:

<FirstElem xmlns="urn:my:test:stuff"/>

Notice the absence of the 'second' tags here as the evaluation of the xsl:apply-templates in the java class does not succeed.

I must be missing options/configuration from my simple java class but can't figure this out. Any suggestions/corrections/etc on what I am doing wrong would be greatly appreciated.

Thanks, Andy


Replies (2)

RE: Different result between java and command line when calling named templates based on a variable - Added by Michael Kay almost 6 years ago

The problem is your use of document(''). This expression reads the document found at the static base URI of the transformation. People often assume that it reads the stylesheet itself; but this will only be the case if the base URI of the stylesheet is known. When you supply the stylesheet as an InputStream, without setting the systemId on the StreamSource object, the base URI is unknown and therefore document('') cannot work.

(I'm not quite sure why it doesn't fail, but that's a second-order question. )

RE: Different result between java and command line when calling named templates based on a variable - Added by Andy Walshe almost 6 years ago

I have refactored in light of your update and it's working very nicely now.

I've attached my updated test class SaxonTestMain.java if it's of any help to others in future.

Thanks very much.

    (1-2/2)

    Please register to reply