Solved

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

Posted on 2008-11-09
7
1,091 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb mdb examples 1 39
Read CLOB data from Oracle using JAVA 3 43
Selenium findElement(By.classname  identifier 15 36
Overriding a method 3 13
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

751 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