Transform 使用的一点心得。
2003-10-28 14:59
639 查看
Transform 使用的一点心得。
XSLT 文件:
如果在html代码之前有<xsl:value-of select=""/> 比如下面的XSLT代码:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl-stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:value-of select="/DearBookOrder/MainOrder/Email"/>
<xsl:value-of select="/DearBookOrder/MainOrder/EmailTopic"/>
<html>
<head>
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"/>
</head>
......
</html>
</xsl-stylesheet>
它跟 XML 一起解析后的结果中,最前面部分就会变成:
<?xml version="1.0" encoding="utf-8"?>ghj1976@csdn.netDearBook 货到付款订单
<html xmlns:fo="http://www.w3.org/1999/XSL/Format">
<head><meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
...
</head>
.....
</html>
.....
但是:
如果在html代码之前没有<xsl:value-of select=""/> 比如下面的XSLT代码:
<?xml version="1.0" encoding="UTF-8" ?>
<xsltylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<xsl:value-of select="/DearBookOrder/MainOrder/Email"/>
<xsl:value-of select="/DearBookOrder/MainOrder/EmailTopic"/>
<head>
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"/>
</head>
......
</html>
</xsl-stylesheet>
解析后的结果:
<html xmlns:fo="ghj1976@csdn.netDearBook'>http://www.w3.org/1999/XSL/Format">ghj1976@csdn.netDearBook 货到付款订单
<head><META http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
...
</head>
....
</html>
这样就没有: <?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/test/Trans/TestWin/Order.xml";
string XslFile = @"E:/MyWorks/test/Trans/TestWin/Order.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/test/Trans/TestWin/Order.xml";
string XslFile = @"E:/MyWorks/test/Trans/TestWin/Order.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 method="html" />
以上两个代码都是返回正确的。
原因:
老兄,你要是指定了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 提供的。
具体讨论过程看:
http://blog.joycode.com/ghj/posts/4080.aspx
XSLT 文件:
如果在html代码之前有<xsl:value-of select=""/> 比如下面的XSLT代码:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl-stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:value-of select="/DearBookOrder/MainOrder/Email"/>
<xsl:value-of select="/DearBookOrder/MainOrder/EmailTopic"/>
<html>
<head>
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"/>
</head>
......
</html>
</xsl-stylesheet>
它跟 XML 一起解析后的结果中,最前面部分就会变成:
<?xml version="1.0" encoding="utf-8"?>ghj1976@csdn.netDearBook 货到付款订单
<html xmlns:fo="http://www.w3.org/1999/XSL/Format">
<head><meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
...
</head>
.....
</html>
.....
但是:
如果在html代码之前没有<xsl:value-of select=""/> 比如下面的XSLT代码:
<?xml version="1.0" encoding="UTF-8" ?>
<xsltylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<xsl:value-of select="/DearBookOrder/MainOrder/Email"/>
<xsl:value-of select="/DearBookOrder/MainOrder/EmailTopic"/>
<head>
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"/>
</head>
......
</html>
</xsl-stylesheet>
解析后的结果:
<html xmlns:fo="ghj1976@csdn.netDearBook'>http://www.w3.org/1999/XSL/Format">ghj1976@csdn.netDearBook 货到付款订单
<head><META http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
...
</head>
....
</html>
这样就没有: <?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/test/Trans/TestWin/Order.xml";
string XslFile = @"E:/MyWorks/test/Trans/TestWin/Order.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/test/Trans/TestWin/Order.xml";
string XslFile = @"E:/MyWorks/test/Trans/TestWin/Order.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 method="html" />
以上两个代码都是返回正确的。
原因:
老兄,你要是指定了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 提供的。
具体讨论过程看:
http://blog.joycode.com/ghj/posts/4080.aspx
相关文章推荐
- 关于mysql下索引使用的一点心得
- 使用FreeMarker生成Word出错的一点心得
- JMS(Jboss Messaging)的一点使用心得(三)Spring扩展应用-Security
- 关于使用OpenGL的一点心得
- 关于使用mui的一点心得
- 使用oracle的一点心得(不错)
- 关于水晶报表使用的一点心得
- 使用Prototype的一点心得体会(五)
- LINQ使用的一点心得
- MFC中关于tab控件使用的一点心得
- spring mvc中使用freemark的一点心得
- 关于UILabel的一点使用心得
- pytroch的一点使用心得
- JMS(Jboss Messaging)的一点使用心得(四)Spring扩展应用-可修改的JMS ConnectionFactory
- Unity5的AssetBundle的一点使用心得
- Java各类IDE一点使用心得
- 使用Direction的include指令和errorPage的一点心得
- 关于使用markdown的一点心得
- LINQ使用的一点心得
- Struts2 Freemarker Jmesa(表格、分页) 使用的一点心得