{"id":110,"date":"2012-10-08T20:38:26","date_gmt":"2012-10-08T20:38:26","guid":{"rendered":"http:\/\/softwareeveryday.wordpress.com\/?p=110"},"modified":"2012-11-17T12:17:20","modified_gmt":"2012-11-17T12:17:20","slug":"parametrize-your-xml-documents-using-xslt-and-java","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=110","title":{"rendered":"Parametrize your xml documents using XSLT and java"},"content":{"rendered":"<p>Recently, i used XSLT to parametrize an XML document. XSLT transformations are used to transform a Source document to a Destination document using a style sheet (XSL). To solve this problem the the source document is empty and the style sheet is the document itself with placeholders.<\/p>\n<pre lang=\"xml\" line=\"1\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\">\r\n\t<xsl:param name=\"param1\" select=\"'defaultval1'\" \/>\r\n\t<xsl:param name=\"param2\" select=\"'defaultval2'\" \/>\r\n\t<xsl:template match=\"\/\">\r\n\t\t<people>\r\n\t\t\t<person age=\"{$param1}\"><xsl:value-of select=\"$param2\" \/><\/person>\r\n\t\t<\/people>\r\n\t<\/xsl:template>\r\n<\/xsl:stylesheet>\r\n<\/pre>\n<pre lang=\"java\" line=\"1\">\r\nFile xsltFile = new File(\"template.xsl\");\r\nDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r\nDocumentBuilder builder = factory.newDocumentBuilder();\r\nDocument emptySourceDoc = builder.newDocument();\r\nTransformerFactory transformerFactory = TransformerFactory.newInstance();\r\nTransformer xsltTransformer = transformerFactory.newTransformer(new StreamSource(xsltFile));\r\nxsltTransformer.setParameter(\"param1\", \"value1\");\r\nxsltTransformer.setParameter(\"param2\", \"value2\");\r\nStreamResult result = new StreamResult(System.out);\r\nxsltTransformer.transform(new DOMSource(emptySourceDoc), result);\r\n<\/pre>\n<p>Cant we have the placeholders in the XML source document? I am unaware of any straightforward way to substitute placeholders in a Source document using style sheets and passing parameters. It also seems using a DOMSource for the xslt file seems to be an issue. More on that here.<\/p>\n<p>One more thing is you dont need to escape XML characters before passing the String as a parameter to the XSLT transformer. Try a simple experiment, disable-output-escaping=&#8221;yes&#8221; to\u00a0xsl:value-of tag.<\/p>\n<pre lang=\"xml\" line=\"1\">\r\n<xsl:value-of select=\"$param2\" disable-output-escaping=\"yes\" \/>\r\n<\/pre>\n<p>Now, if you pass any of XML&#8217;s escape characters { &#8220;, &#8216;, &lt;, &gt;, &amp;} in the string, it shouldn&#8217;t escape them and your xml file will be incorrectly generated.\u00a0By default\u00a0disable-output-escaping is set to &#8220;no&#8221; so escaping works fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, i used XSLT to parametrize an XML document. XSLT transformations are used to transform a Source document to a Destination document using a style sheet (XSL). To solve this problem the the source document is empty and the style sheet is the document itself with placeholders. File xsltFile = new File(&#8220;template.xsl&#8221;); DocumentBuilderFactory factory = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":5,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":2608,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/2608"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}