xslt转换成html后没有html标签

用xalan转化时的问题:

这是网上的示例代码:

String xsltPath="C:\\XTX.xsl";
String xmlPath="C:\\76552.xml";
String htmlPath="C:\\Users\\Administrator\\Desktop\\654.html";
TransformerFactory tFactory = TransformerFactory.newInstance();
		 Transformer transformer =
		      tFactory.newTransformer
		         (new javax.xml.transform.stream.StreamSource
		            (xsltPath));
		 transformer.transform
	      (new javax.xml.transform.stream.StreamSource
	            (xmlPath),
	       new javax.xml.transform.stream.StreamResult
	            ( new FileOutputStream(htmlPath)));

因为原xml文档中定义的命名空间,而xsl模板中没有引用该空间,所以<xsl:choose><xsl:when>里的xpath都没有识别,结果生成的html是一大坨不带标签的纯文本。

但是用struts的action可以成功转换,像这样:

<action name="reportDisplay" class="com.ReportAction">
<result name="S005" type="xslt">
<param name="stylesheetLocation">/WEB-INF/webpage/xsl/BC.xsl</param>
<param name="exposedValue">report</param>
</result>
</action>

因为struts是用DOMSource去转化的,解决如下:

String xsltPath="C:\\XTX.xsl";
String xmlPath="C:\\76552.xml";
String htmlPath="C:\\Users\\Administrator\\Desktop\\654.html";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();    
DocumentBuilder builder=factory.newDocumentBuilder();    
Document document=builder.parse(new File(xmlPath));    
document.normalize(); 
			
DOMSource source=new DOMSource(document);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer
			         (new javax.xml.transform.stream.StreamSource
			            (xsltPath));
transformer.transform(source, new javax.xml.transform.stream.StreamResult(
					new FileOutputStream(htmlPath)));






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值