Solved

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

Posted on 2008-11-09
7
1,068 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This video teaches viewers about errors in exception handling.
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)

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now