File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Breaking up an XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Breaking up an XSLT" Watch "Breaking up an XSLT" New topic
Author

Breaking up an XSLT

Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
Hello all,

I'm having difficulty resolving hrefs in my stylesheet. The problem is, my stylesheet wants to import other stylesheets because it is getting complex and bloated. I want to write the stylesheet as modular sub-stylsheets each focusing on a different template. That would allow me to unit test each template in isolation and speed my development. It's not working as planned though. I just tried breaking a template into a separate stylesheet and using <xsl:include/> to include it. The stylesheet is loaded in my unit test using a classloader so I'm thinking the href could be relative to the classpath. It doesn't work that way. The href is considered relative to the working folder of the JVM, in this case my project root. Is there a way I can inject the href of the main stylesheet with the hrefs of all of the sub-stylesheets? Is what I'm doing even good practice? What's a better way of breaking up a complicated stylesheet?


Holla at me...<br /><a href="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The rule is, a relative URL for <xsl:import> is relative to the base URL of the stylesheet doing the import. But if the transformer doesn't know the base URL of that stylesheet, then it falls back to something that's generally useless. (Current working directory?)

If you're passing a URL, or a File object, to the transformer to identify the XSLT, then it can find the base URL of that. But if you pass an InputStream or a Reader or a DOM, then it can't.

So there's a couple of possible fixes:

1. Change your call to newTransformer() to pass it something whose URL can be identified.

2. Pass newTransformer() a Source object on which you called setSystemId() with the desired base URL.

3. Attach a URIResolver to the TransformerFactory to provide the URL for imported or included stylesheets.
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
More important, I want to be able to invoke a single template and assert the results in my stylesheet. That's what originally drove me to break the template appart. I don't want to have to come in through the root template match and sift through all of the stuff generated there. For example, I have:



I want to create a string like "<b>blah blah<c/></b>" and invoke only the template for b. I then want to do the same thing for c and any further sub-elements. It would be simple if each template were in its own stylesheet. Then I could write individual tests for the separate stylesheets and then somehow assemble them into one big stylesheet (and maybe compile it?) I'm having trouble getting testing isolation. Can you help?
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
Originally posted by Paul Clapham:
The rule is, a relative URL for <xsl:import> is relative to the base URL of the stylesheet doing the import. But if the transformer doesn't know the base URL of that stylesheet, then it falls back to something that's generally useless. (Current working directory?)

If you're passing a URL, or a File object, to the transformer to identify the XSLT, then it can find the base URL of that. But if you pass an InputStream or a Reader or a DOM, then it can't.

So there's a couple of possible fixes:

1. Change your call to newTransformer() to pass it something whose URL can be identified.

2. Pass newTransformer() a Source object on which you called setSystemId() with the desired base URL.

3. Attach a URIResolver to the TransformerFactory to provide the URL for imported or included stylesheets.


Paul,

You're about to be my bestest buddy! That works like a charm! Thanx again for your helpful comments. With people like you and Bill on the forums there's nothing that I can't do. Thatnx again for making me look smarter than I really am in front of my boss.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Breaking up an XSLT