根据xslt定义xml文件生成html文件
有xls文件如下:
xml文件如下:
C#代码如下:
生成result.html如下:
这样可以生成页面
主要是xls 需要好好学习
<!--
- XSLT is a template based language to transform Xml documents
It uses XPath to select specific nodes
for processing.
- A XSLT file is a well formed Xml document
-->
<!-- every StyleSheet starts with this tag -->
< xsl:stylesheet
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
version ="1.0" >
<!-- indicates what our output type is going to be -->
< xsl:output method ="html" />
<!--
Main template to kick off processing our Sample.xml
From here on we use a simple XPath selection query to
get to our data.
-->
< xsl:template match ="/" >
< html >
< head >
< title > Welcome to < xsl:value-of select ="/company/name" /></ title >
< style >
body,td {font-family:Tahoma,Arial; font-size:9pt;}
</ style >
</ head >
< body >
< h2 > Welcome to < xsl:value-of select ="/company/name" /></ h2 >
< p />
< b > Our contact details: </ b >
< br />
< br />
< xsl:value-of select ="/company/name" />
< br />
< xsl:value-of select ="/company/address1" />
< br />
< xsl:value-of select ="/company/address2" />
< br />
< xsl:value-of select ="/company/city" />
< br />
< xsl:value-of select ="/company/country" />
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
- XSLT is a template based language to transform Xml documents
It uses XPath to select specific nodes
for processing.
- A XSLT file is a well formed Xml document
-->
<!-- every StyleSheet starts with this tag -->
< xsl:stylesheet
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
version ="1.0" >
<!-- indicates what our output type is going to be -->
< xsl:output method ="html" />
<!--
Main template to kick off processing our Sample.xml
From here on we use a simple XPath selection query to
get to our data.
-->
< xsl:template match ="/" >
< html >
< head >
< title > Welcome to < xsl:value-of select ="/company/name" /></ title >
< style >
body,td {font-family:Tahoma,Arial; font-size:9pt;}
</ style >
</ head >
< body >
< h2 > Welcome to < xsl:value-of select ="/company/name" /></ h2 >
< p />
< b > Our contact details: </ b >
< br />
< br />
< xsl:value-of select ="/company/name" />
< br />
< xsl:value-of select ="/company/address1" />
< br />
< xsl:value-of select ="/company/address2" />
< br />
< xsl:value-of select ="/company/city" />
< br />
< xsl:value-of select ="/company/country" />
</ body >
</ html >
</ xsl:template >
</ xsl:stylesheet >
xml文件如下:
<
company
>
< name > XYZ Inc. </ name >
< address1 > One Abc Way </ address1 >
< address2 > Some avenue </ address2 >
< city > Tech city </ city >
< country > Neverland </ country >
</ company >
2个文件命名分别为:
sample.xsl和
sample.xml< name > XYZ Inc. </ name >
< address1 > One Abc Way </ address1 >
< address2 > Some avenue </ address2 >
< city > Tech city </ city >
< country > Neverland </ country >
</ company >
C#代码如下:
public
static
void
Transform(
string
sXmlPath,
string
sXslPath)
{
try
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XslTransform myXslTrans = new XslTransform();
//load the Xsl
myXslTrans.Load(sXslPath);
//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
("result.html", null);
//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
{
try
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XslTransform myXslTrans = new XslTransform();
//load the Xsl
myXslTrans.Load(sXslPath);
//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
("result.html", null);
//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
生成result.html如下:
<
html
>
< head >
< title > Welcome to XYZ Inc. </ title >
< style >
body,td {font-family:Tahoma,Arial; font-size:9pt;}
</ style >
</ head >
< body >
< h2 > Welcome to XYZ Inc. </ h2 >
< p />
< b > Our contact details: </ b >
< br />
< br />
XYZ Inc.
< br />
One Abc Way
< br />
Some avenue
< br />
Tech city
< br />
Neverland
</ body >
</ html >
< head >
< title > Welcome to XYZ Inc. </ title >
< style >
body,td {font-family:Tahoma,Arial; font-size:9pt;}
</ style >
</ head >
< body >
< h2 > Welcome to XYZ Inc. </ h2 >
< p />
< b > Our contact details: </ b >
< br />
< br />
XYZ Inc.
< br />
One Abc Way
< br />
Some avenue
< br />
Tech city
< br />
Neverland
</ body >
</ html >
这样可以生成页面
主要是xls 需要好好学习