没有错误:但是通过Javax.xml.transform不会进行XSLT转换

时间:2019-01-16 10:35:04

标签: java xml xslt

XSLT已在线测试并可以正常工作,但是在Java上运行时,对XML无效,并且在服务器控制台上未报告任何错误

请参考XML Link。在http://xsltransform.net上进行测试时,通过XSLT转换可以得到正确的结果。但是当我在Java上应用相同的XSLT时,没有任何转换发生,并且服务器控制台上也没有bug。

已使用的库

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代码

         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());

1 个答案:

答案 0 :(得分:0)

以下更改已解决的问题(不知道根本原因)

1。设置名称空间意识为假

dbFactory.setNamespaceAware(false);

2。从xslt字符串中删除名称空间标识符

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>";