代码之家  ›  专栏  ›  技术社区  ›  Amit Panasara

没有错误:但没有通过javax.xml.transform进行XSLT转换

  •  0
  • Amit Panasara  · 技术社区  · 6 年前

    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 回复  |  直到 6 年前
        1
  •  0
  •   Amit Panasara    6 年前

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

    1。对setNamespace感知错误

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