没有错误:但是通过Javax.xml.transform没有发生XSLT转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了没有错误:但是通过Javax.xml.transform没有发生XSLT转换相关的知识,希望对你有一定的参考价值。
XSLT在线测试并且工作正常,但在Java上运行时,对XML没有任何影响,并且在服务器控制台上没有报告错误
请参考XML onl Link。当我在http://xsltransform.net上测试时,我通过XSLT转换获得了正确的结果。但是当我在Java上应用相同的XSLT时,没有任何转换发生,并且服务器控制台上也没有错误。
使用的库
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.对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>";
以上是关于没有错误:但是通过Javax.xml.transform没有发生XSLT转换的主要内容,如果未能解决你的问题,请参考以下文章
linux使用rsync备份目录,但是有的目录没有备份,请问是啥原因?并且在输出日志中没有现在错误!