Transform 使用的一点心得。 (转)

本文分享了在使用XSLT进行XML转换时遇到的问题及解决办法,详细介绍了如何避免在输出HTML文件时包含XML声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Transform 使用的一点心得。 (转)[@more@]

TransfoRM 使用的一点心得。
xslT 文件

如果在html代码之前有select=""/> 比如下面的XSLT代码:


XML version="1.0" encoding="UTF-8" ?>
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

Mail"/>
emailTopic"/>


microsoft Visual Studio 6.0"/>

...... 

它跟 XML 一起解析后的结果中,最前面部分就会变成:

<?xml version="1.0" encoding="utf-8"?>ghj1976@csdn.NETDearBook 货到付款订单
http://www.w3.org/1999/XSL/Format">



...

.....


.....

但是:

如果在html代码之前没有 比如下面的XSLT代码:


<?xml version="1.0" encoding="UTF-8" ?>
http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">







...... 

解析后的结果:

ghj1976@youkuaiyun.comDearBook 货到付款订单




...

....

这样就没有: <?xml version="1.0" encoding="utf-8"?> 了。

 
这时候调用的代码为:

 private void button2_Click(object sender, System.EventArgs e)
 {
 string Savefile = @"C:1.htm";
 string XmlFile = @"E:MyWorks estTransTestWinOrder.xml";
 string XslFile = @"E:MyWorks estTransTestWinOrder.xslt";
 if (File.Exists(Savefile))
 {
 File.Delete(Savefile);
 }
 FileStream fs = new FileStream(Savefile,FileMode.Create);
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(XmlFile);
 xpathNavigator nav = xmlDoc.DocumentElement.CreateNavigator();
 XslTransform xmlXsl = new XslTransform();
 xmlXsl.Load(XslFile);
 xmlXsl.Transform(nav,null,fs,null);
 xmlXsl = null;
 nav = null;
 xmlDoc = null;
 fs.Close();
 fs = null;
 }


后来根据 gooGideA 的意见,把代码改为:

 private void button1_Click(object sender, System.EventArgs e)
 {
 string Savefile = @"C:1.htm";
 string XmlFile = @"E:MyWorks estTransTestWinOrder.xml";
 string XslFile = @"E:MyWorks estTransTestWinOrder.xslt";
 if (File.Exists(Savefile))
 {
 File.Delete(Savefile);
 }
 FileStream fs = new FileStream(Savefile,FileMode.Create);

 XmlTextWriter writer = new XmlTextWriter( fs, System.Text.Encoding.Default );
 writer.Formatting = Formatting.Indented;

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(XmlFile);
 XPathNavigator nav = xmlDoc.DocumentElement.CreateNavigator();
 XslTransform xmlXsl = new XslTransform();
 xmlXsl.Load(XslFile);
 xmlXsl.Transform(nav,null,writer,null);
 xmlXsl = null;
 nav = null;
 xmlDoc = null;
 fs.Close();
 fs = null;
 
 }

就没有这个问题了。

同样,思归提供的,再 XSLT 中增加

以上两个代码都是返回正确的。


原因:
老兄,你要是指定了xsl:output=Html,随便你怎么写都不会出来<?xml version="1.0" encoding="UTF-8" ?>。
实际上你这二个xslt为什么会这样在output的help里也可以找到说明:
The default for the method attribute is chosen as follows. If any of the following conditions are true, the default output method is "html":

The root node of the result tree has an element child.
The expanded-name of the first element child of the root node (that is, the document element) of the result tree has local part "html" (in any combination of uppercase and lowercase) and a null namespace URI.
Any text nodes preceding the first element child of the root node of the result tree contain only white space characters.
Otherwise, the default output method is "xml".
Zee 提供的。

具体讨论过程看:


ASPx">http://blog.joycode.com/ghj/posts/4080.aspx


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-963568/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-963568/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值