Java XSLT combine two inputs xml -
Java XSLT combine two inputs xml -
i want combine 2 input xml string 1 output using xslt , java, here code. don't know why doesn't work. how prepare ?
thanks.
import java.io.stringreader; import java.io.stringwriter; import javax.xml.transform.transformer; import javax.xml.transform.transformerfactory; import javax.xml.transform.stream.streamresult; import javax.xml.transform.stream.streamsource; public class test { final static string sourcexml1 ="<languages-list>\r\n" + "<language>\r\n" + "<family>john family</family>\r\n" + "<users>asfasd</users>\r\n" + "</language>\r\n" + "</languages-list>"; final static string sourcexml2 = "<languages-list>\r\n" + "<language>\r\n" + "<name>john</name>\r\n" + "<region>nyc</region>\r\n" + "</language>\r\n" + "</languages-list>"; final static string xsl = "<xsl:stylesheet version=\"2.0\"\r\n" + "xmlns:xsl=\"http://www.w3.org/1999/xsl/transform\">\r\n" + "\r\n" + "<xsl:param name=\"doc1\" />\r\n" + "<xsl:param name=\"doc2\" />\r\n" + "\r\n" + " <xsl:template match=\"/\">\r\n" + " <html>\r\n" + " <body>\r\n" + " <table border=\"1\">\r\n" + " <xsl:for-each select=\"language-list/language\">\r\n" + " <tr>\r\n" + " <td><xsl:value-of select=\"$doc1/family\"/></td>\r\n" + " <td><xsl:value-of select=\"$doc1/users\"/></td>\r\n" + " <td><xsl:value-of select=\"$doc2/name\"/></td>\r\n" + " <td><xsl:value-of select=\"$doc2/region\"/></td>\r\n" + " </tr>\r\n" + " </xsl:for-each>\r\n" + " </table>\r\n" + " </body>\r\n" + " </html>\r\n" + " </xsl:template>\r\n" + "</xsl:stylesheet> "; public static void main(string[] args) { system.out.println(sourcexml1 + "\n=======\n" + sourcexml2+"\n=======\n"+xsl); seek { streamsource stream1 = new streamsource(new stringreader(sourcexml1)); streamsource stream2 = new streamsource(new stringreader(sourcexml2)); stringreader xslreader = new stringreader(xsl); stringwriter author = new stringwriter(); transformerfactory tfactory = transformerfactory.newinstance(); transformer transformer = tfactory.newtransformer(new streamsource(xslreader)); // style sheet transformer.setparameter("doc1", stream1); transformer.setparameter("doc2", stream2); transformer.transform(stream1, new streamresult(writer)); string result = writer.tostring(); system.out.println("==============\n"+result); } grab (exception e) { e.printstacktrace(); } } }
try process each stream 1 time utilize 1 main input document , 1 parameter, then, assuming xslt processor supports parameters of type source (like saxon 9 does), use
import java.io.stringreader; import java.io.stringwriter; import javax.xml.transform.transformer; import javax.xml.transform.transformerfactory; import javax.xml.transform.stream.streamresult; import javax.xml.transform.stream.streamsource; public class test2014062001 { final static string sourcexml1 ="<languages-list>\r\n" + "<language>\r\n" + "<family>john family</family>\r\n" + "<users>asfasd</users>\r\n" + "</language>\r\n" + "</languages-list>"; final static string sourcexml2 = "<languages-list>\r\n" + "<language>\r\n" + "<name>john</name>\r\n" + "<region>nyc</region>\r\n" + "</language>\r\n" + "</languages-list>"; final static string xsl = "<xsl:stylesheet version=\"2.0\"\r\n" + "xmlns:xsl=\"http://www.w3.org/1999/xsl/transform\">\r\n" + "\r\n" + "<xsl:param name=\"doc2\" />\r\n" + "\r\n" + " <xsl:template match=\"/\">\r\n" + " <html>\r\n" + " <body>\r\n" + " <div><xsl:value-of select=\"system-property('xsl:vendor')\"/></div>\r\n" + " <table border=\"1\">\r\n" + " <xsl:for-each select=\"languages-list/language\">\r\n" + " <xsl:variable name=\"pos\" select=\"position()\"/>\r\n" + " <tr>\r\n" + " <td><xsl:value-of select=\"family\"/></td>\r\n" + " <td><xsl:value-of select=\"users\"/></td>\r\n" + " <td><xsl:value-of select=\"$doc2/languages-list/language[$pos]/name\"/></td>\r\n" + " <td><xsl:value-of select=\"$doc2/languages-list/language[$pos]/region\"/></td>\r\n" + " </tr>\r\n" + " </xsl:for-each>\r\n" + " </table>\r\n" + " </body>\r\n" + " </html>\r\n" + " </xsl:template>\r\n" + "</xsl:stylesheet> "; public static void main(string[] args) { system.out.println(sourcexml1 + "\n=======\n" + sourcexml2+"\n=======\n"+xsl); seek { streamsource stream1 = new streamsource(new stringreader(sourcexml1)); streamsource stream2 = new streamsource(new stringreader(sourcexml2)); stringreader xslreader = new stringreader(xsl); stringwriter author = new stringwriter(); transformerfactory tfactory = transformerfactory.newinstance(); transformer transformer = tfactory.newtransformer(new streamsource(xslreader)); // style sheet transformer.setparameter("doc2", stream2); transformer.transform(stream1, new streamresult(writer)); string result = writer.tostring(); system.out.println("==============\n"+result); } grab (exception e) { e.printstacktrace(); } } }
output saxon 9 is
<html> <body> <div>saxonica</div> <table border="1"> <tr> <td>john family</td> <td>asfasd</td> <td>john</td> <td>nyc</td> </tr> </table> </body> </html>
java xslt
Comments
Post a Comment