0

XSLT is tested online & working fine, but while running on Java, no effect is applied on XML as well as no error is reported on server-console

Please refer XML onl Link. While I am testing on http://xsltransform.net, I am getting correct result thru XSLT transformation. but when I am applying same XSLT on Java, there is no any transformation is happening & there is no bug on server-console also.

Library used

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

Java Code

         File fXmlFile = new File("C:\\Java\\Web Project\\Merged_Excel\\WebContent\\source\\XML\\XML_Template.xml");

          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          dbFactory.setNamespaceAware(true);
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          doc = (Document) dBuilder.parse(fXmlFile);

          doc.getDocumentElement().normalize();

          /*..... XML buliding.....*/

              String sorter="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                            "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"+
                            "  <xsl:output method=\"xml\" encoding=\"utf-8\" indent=\"yes\"/>"+
                            "  <xsl:template match=\"@* | node()\">"+
                            "    <xsl:copy>"+
                            "      <xsl:apply-templates select=\"@* | node()\"/>"+
                            "    </xsl:copy>"+
                            "  </xsl:template>"+
                            "  <xsl:template match=\"ss:Row\">"+
                            "    <xsl:copy>"+
                            "      <xsl:apply-templates select=\"*\">"+
                            "        <xsl:sort select=\"@ss:Index\" order=\"ascending\" data-type=\"number\"/>"+
                            "      </xsl:apply-templates>"+
                            "    </xsl:copy>"+
                            "  </xsl:template>"+
                            "</xsl:stylesheet>";

              StreamSource stylesource = new StreamSource(new StringReader(sorter));
              System.out.println(stylesource);
              /*also tried to fetch External-XSLT but couldn't suceed
              Source stylesource = new StreamSource(new File("C:\\Java\\Web Project\\Merged_Excel\\WebContent\\source\\XML\\XSLT.xslt"));*/
              StringWriter writer = new StringWriter();
              TransformerFactory factory = TransformerFactory.newInstance();

              Transformer trans = factory.newTransformer(stylesource);
              trans.transform(new DOMSource(doc), new StreamResult(writer));
              System.out.println(writer.toString());
Amit Panasara
  • 600
  • 8
  • 16
  • Is the `DocumentBuilder` you have used to create the `doc` variable namespace aware (use https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setNamespaceAware-boolean- to ensure that)? – Martin Honnen Jan 16 '19 at 10:50
  • I used `org.w3c.dom.document` for doc & `javax.xml.parsers.DocumentBuilder` for Document builiding – Amit Panasara Jan 16 '19 at 11:51
  • Can you edit your question and show a minimal XML input document you have together with the result your code outputs in `System.out.println(writer.toString());`? I don't see anything wrong so far. But why do you say "tried to fetch External-XSLT but couldn't suceed", which error did you get? – Martin Honnen Jan 16 '19 at 12:49
  • no error in console... but this XSLT is made for SORTing `` based on `ss:Index` & output is unsorted.... [XML](https://stackoverflow.com/questions/54210819/sorting-cells-as-per-ssindex) – Amit Panasara Jan 16 '19 at 14:29

1 Answers1

0

Following changes solved problem (dont' know root cause)

1. FALSE to setnamespace awareness

dbFactory.setNamespaceAware(false);

2. Removing namespace identifier from xslt string

String sorter="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                            "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"+
                            "  <xsl:output method=\"xml\" encoding=\"utf-8\" indent=\"yes\"/>"+
                            "  <xsl:template match=\"@* | node()\">"+
                            "    <xsl:copy>"+
                            "      <xsl:apply-templates select=\"@* | node()\"/>"+
                            "    </xsl:copy>"+
                            "  </xsl:template>"+
                            "  <xsl:template match=\"Row\">"+ //if namespace sensitive then use ss:Row
                            "    <xsl:copy>"+
                            "      <xsl:apply-templates select=\"*\">"+
                            "        <xsl:sort select=\"@Index\" order=\"ascending\" data-type=\"number\"/>"+ //if namespace sensitive then use ss:Index
                            "      </xsl:apply-templates>"+
                            "    </xsl:copy>"+
                            "  </xsl:template>"+
                            "</xsl:stylesheet>";
Amit Panasara
  • 600
  • 8
  • 16