Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-11-09
7
Medium Priority
?
1,107 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 750 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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

660 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