Solved

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

Posted on 2008-11-09
7
1,097 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

624 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