Solved

custom function resolver in xslt transformation (called from Java)?

Posted on 2008-11-09
7
1,084 Views
Last Modified: 2013-11-18
Hello everybody,

I am trying to make a custom function resolver available to my xslt transformations.
Specificlly, the following should work:
<xsl:variable select="myNS:myFunction($arg1)" />
where my function returns a node or node set.

I pretty much have everything I need for executing single XPaths:
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setXPathFunctionResolver(new MyFunctionResolver());

Well, this works.

However, when I do an xslt transformation like this:
Transformer transformer = factory.newTransformer(source);
transformer.setURIResolver(new MyResolver());
final Result result =    new javax.xml.transform.stream.StreamResult(System.out);
transformer.transform(xmlSource, result);

I don't see any place where I can pass a custom function resolver :-(
I need my functions to be available in a complex stylesheet, not just in a "simple xpath".

(how) Can this be done?

Thanks in advance for any help!

P.S.: This should work "out of the box" in Java 5 and higher. It should not require additional libraries and should not be specific to Xalan or Saxon it at all possible...
0
Comment
Question by:Ronald112197
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:rbrindl
ID: 22929080
I dont know, if there is a public api for this. As far as i know, there is only the xalan ExtensionHandler SPI, but that is xalan specific. This is also used internally by xalans implementation of XPathFunctionResolver.
0
 
LVL 2

Author Comment

by:Ronald112197
ID: 22929812
Thanks for the comment. I am now looking for a solution that is NOT specific to Xalan or Saxon.

I'm a little surprised that this is so hard - doing the same for an XPath is simple :-(

I have now found a solution that covers 90% of my "immediate needs": I am using the custom URIResolver and I return the result as XML.

I can call and load it as
<xsl:variable name="demo" select="document('/some/parameters/here')" />

After that, my variable contains an arbitrarily complex "function result". Well... It works well, but is still not as flexible and "nice"  as I'd like it to be.

I'm still looking for a solution that lets me implement custom functions and I'm offering 500 points for that - if it works :-)
0
 
LVL 1

Expert Comment

by:rbrindl
ID: 22975326
There is another xalan specific solution. This requires xalan, but at least no change in the java code that does the xslt translation.
You just need the namespace definitions below in the xslt and a jjava implementation.

2 Downsides:
1.) xalan specific

also see http://xml.apache.org/xalan-j/extensions.html
XSLT: 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:ext="expertsexchange.jaxpextension.TestExtension"
	xmlns:java="http://xml.apache.org/xalan/java"
	exclude-result-prefixes="ext java"
	>
    <xsl:template match="/">
    	
		<xsl:variable name="test" select="ext:new('Test works')"/>
		<result>
		test() returns "<xsl:value-of select="ext:test($test)"/>"
		</result>
	</xsl:template>
</xsl:stylesheet>
 
 
 
JAVA:
package expertsexchange.jaxpextension;
public class TestExtension {
	private String value;
	
	public TestExtension(String v)
	{
		value=v;
	}
 
	public String test() 
	{
		return value;
	}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:rbrindl
ID: 22975331
sorry, only one downside ;)
0
 
LVL 2

Author Comment

by:Ronald112197
ID: 23103459
*haha* - don't worry, one downside is quite enough :-)

No really, thanks for your suggestion, but I can't use it.
I *explicitly* said that I am now looking for something that is NOT specific to one processor. Well.... and if it is specific to one processor, it should at least be Saxon. I think Xalan is dead, sadly enough... I use either the "Java 5 built-in" or Saxon (for XSLT 2.0)....

Seems like this is not possible? I'm surprised and shocked... :-(
0
 
LVL 1

Accepted Solution

by:
rbrindl earned 250 total points
ID: 23113923
i know, but i cant help there, sorry.
i dont know saxon yet, but i am sure it also has some decent extension mechanism.

Yes, there is:
http://saxon.sourceforge.net/saxon6.5.3/extensibility.html
looks even simpler and it seems to be aligned
But i havent tried this yet, i have to admit.
0
 
LVL 2

Author Comment

by:Ronald112197
ID: 24348721
Oh well... since there doesn't seem to be a "perfect" solution to my question, your tips are probably as close as it gets, so I decided to just accept them as a solution rather than closing the question without solution :-)

Thanks for the tips, I'll probably combine them with my workaround for a good solution.
Also, I'm currently doing "preprocessing" on my stylesheet, i.e. I introduced a custom tag and I transform it into the URI-syntax before I do the actual processing. I can probably use the same approach to transform it into the processor-specific function extension...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question