XSLT calling java embedded in XML

I have an xsl snippet:

<GOGO> <xsl:variable name="test"> <xsl:copy-of select="response"/> </xsl:variable> <xsl:copy-of select="javamap:echo($test)"/> </GOGO> 

This snippet calls the java method:

 public static String echo(String a) { System.out.println("HERE I AM:"+a+":"); return "<xxx>" + a + "</xxx>"; } 

If I have only the following snippet:

 <GOGO> <xsl:copy-of select="response"/> </GOGO> 

transformation of the result will look like this:

 <foo>val1</foo> <bar>val2</bar> 

However, when calling the Java method, logging out is displayed unexpectedly:

 val1 val2 

What am I doing wrong and how do I get the java method to output the expected xml fragment?

EDIT: Answer questions from those who help me: I use Saxon9. Someone from another thread showed me the use of value-of and disable-output-escaping = "yes", which allowed me to print the element tag xxx in the output. However, I'm still a dead end on the input side, where I would like my java class to have a full understanding of the full xml fragment that I passed to it.

The foo and bar tags are the xml that I want to pass to the java function. Inside the java function, I want to wrap xml in the xxx tag even more.

EDIT 2: The hints below allowed me to get the following solution: public static String echo (Node a) throws Exception {

  StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(a), new StreamResult(writer)); String xml = writer.toString(); return xml; } 
+4
source share
1 answer

The semantics of the extension functions depend on the processor you are using, which you did not tell us about, but I think it is unlikely that any processor will work as you expect, that is, process the returned string using a function like lexical XML for analysis and conversion in node. If you want to return a node, you must create a node before returning it. The type of node (for example, DOM node, which document belongs, etc.) will be system dependent; and in general I would recommend NOT manipulating nodes in extension functions - XSLT does a lot better.

+2
source

All Articles