4

I've got 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 a java method:

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

If I just have the following snippet:

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

the result transformation will soemthing like:

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

However when the Java method is invoked, the system out output is unexpected only printing out:

val1
val2

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

EDIT: Answer to questions from those helping me: I am using Saxon9. Someone in another thread showed me the use of value-of and disable-output-escaping="yes" which allowed me to print out the xxx element tag in the output. However I am still stumped on the input side where I would like my java class to have full awareness of the full xml snippet I pass to it.

The foo and bar tags are the xml I want to pass into the java function. Inside the java function I want to further wrap the xml in xxx tag.

EDIT 2: Hints below allowed me to derive 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
  • well. I can guess one reason, you are passing a node set but reading it as string! When function reads it as an argument as a string, only value(text) is loaded! Commented Dec 11, 2012 at 15:13
  • give a try defining a as node-set rather than string! Then convert it to string (hope innerHTML would turn nodeset to XML) .. Don't know if it works but worth giving a try! Commented Dec 11, 2012 at 15:16
  • If the comment above does solve this, let us know so its author can post it as an answer. If it does not work, then providing a SSCCE might help you to get an answer on this. I for one find your question slightly confusing, e.g. because your function writes xxx but your output foo and bar. Commented Dec 11, 2012 at 16:12
  • your hint gave me a direction and i am posting the solution in my EDITs Commented Dec 12, 2012 at 17:22

1 Answer 1

2

The semantics of extension functions depend on the processor you are using, which you haven't told us, but I think it would be highly unlikely for any processor to work the way you expect - that is, to treat the string returned by a function as lexical XML to be parsed and turned into a node. If you want to return a node, you must construct a node before returning it. The kind of node (eg. whether it's a DOM node, what document it belongs to etc) will be system-dependent; and on the whole I would recommend NOT manipulating nodes in extension functions - XSLT does the job much better.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.