Reputation: 11
I have the following xml element:
<notes>
<isImportant>true</isImportant>
<content><p>Click on the Link below to direct you the page " Java tutorial"</p>
<p> <a title="Java tutorial" target="_blank" href="https://www.javatpoint.com/java-tutorial">https://www.javatpoint.com/java-tutorial</a></p></content>
</notes>
In xslt I need to pass isImportant and content elements content to java method as string. This is how I am saving those to a variable:
<xsl:variable name="notesIsImportant" select="$inputMetaXml//notes/isImportant"/>
<xsl:variable name="notesContent">
<xsl:copy-of select="$input//notes/content"/>
</xsl:variable>
Within template I am calling java method:
<xsl:value-of select="java:generateNotesJson($customContentInstance,string($notesIsImportant), string($notesContent))"/>
And this is my java class:
public class MetadataHandler {
public String generateNotesJson(String isImportant, String content) {
return String.format("{\"IsImportant\": \"%s\", \"Content\": \"%s\"}", isImportant, content);
}
}
The problem is that when I pass content of content element it is just passing text nodes. How can I make sure that I pass all element, attribute and text nodes, basically everything inside the content element as it is?
Above implementation for method call is giving me the following result:
Click on the Link below to direct you the page " Java tutorial" https://www.javatpoint.com/java-tutorial
What I am trying to get:
<p>Click on the Link below to direct you the page " Java tutorial"</p>
<p> <a title="Java tutorial" target="_blank" href="https://www.javatpoint.com/java-tutorial">https://www.javatpoint.com/java-tutorial</a></p>
Upvotes: 0
Views: 41
Reputation: 167716
As for creating JSON output directly, you can create XPath 3.1/XDM 3.1 maps/arrays and serialize them directly, with the xsl:output method="json"
or (in a local context) with the serialize
function setting 'method':'json'
in the option parameter.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:output method="json" indent="yes"/>
<xsl:template match="notes">
<xsl:sequence
select="map { 'IsImportant' : string(isImportant), 'Content' : serialize(content!node()) } "/>
</xsl:template>
</xsl:stylesheet>
Output as JSON:
{
"IsImportant": "true",
"Content": "<p>Click on the Link below to direct you the page \" Java tutorial\"<\/p>\n <p> <a title=\"Java tutorial\" target=\"_blank\" href=\"https:\/\/www.javatpoint.com\/java-tutorial\">https:\/\/www.javatpoint.com\/java-tutorial<\/a><\/p>"
}
Upvotes: 2