下面是网上的一个例子:report.xml<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="reportxst.xsl"?> 
<document>
<report>
<class>
甲班
</class>
<q1>50</q1>
<q2>70</q2>
<q3>30</q3>
<q4>10</q4>
</report><report>
<class>
乙班
</class>
<q1>20</q1>
<q2>30</q2>
<q3>40</q3>
<q4>50</q4>
</report><report>
<class>
丙班
</class>
<q1>70</q1>
<q2>40</q2>
<q3>20</q3>
<q4>10</q4>
</report>
</document> -----------------------------------------------------------------------------------reportxst.xsl<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"><xsl:template match="/">
<HTML><HEAD><TITLE>1999年生产统计</TITLE></HEAD> 
<BODY><xsl:apply-templates select="document"/></BODY>
</HTML>
</xsl:template><xsl:template match="document">
<H3>1999年生产统计</H3> 
<TABLE border="1" cellspacing="0"> 
<TH>班组</TH>
<TH>一季度</TH>
<TH>二季度</TH>
<TH>三季度</TH>
<TH>四季度</TH>
<xsl:apply-templates select="report"/>
</TABLE>
</xsl:template><xsl:template match="report">
<TR>
<TD><xsl:value-of select="class"/></TD>
<TD><xsl:apply-templates select="q1"/></TD>
<TD><xsl:apply-templates select="q2"/></TD>
<TD><xsl:apply-templates select="q3"/></TD>
<TD><xsl:apply-templates select="q4"/></TD>
</TR>
</xsl:template><xsl:template match="q1|q2|q3|q4">
<!--此处测试产量,如小于等于20则添加一STYLE属性color,其值为red(红色)--> 
<xsl:if test=".[value()$le$20]"> 
<xsl:attribute name="style">color:red</xsl:attribute>
</xsl:if>
<xsl:value-of/>
</xsl:template></xsl:stylesheet>在两个文档,在浏览器中解析正常,但是我用.net的程序转换,老是出现异常:
提示: reportxst.xsl(2,2) 转换失败!我的C#转换程序主要程序如下,这个C#程序我测试过另外一个简单的例子,输出是正确的. public System.Xml.XmlDocument TransForm(string xmlsource,string xslsource)
{
System.Xml.XmlDocument xd = new XmlDocument();
XslTransform xslt = new XslTransform();
xslt.Load(xslsource);
XPathDocument xpathdocument = new
XPathDocument(xmlsource);
System.IO.Stream MemStream = new System.IO.MemoryStream(); 
XmlTextWriter writer = new XmlTextWriter(MemStream, Encoding.UTF8);
writer.Formatting=Formatting.Indented;
xslt.Transform(xpathdocument, null, writer, null);    
MemStream.Position = 0;
xd.Load(MemStream);
MemStream.Close();
return xd; }

解决方案 »

  1.   


    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    吧<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">只是草案标准
      

  2.   

    http://sz.luohuedu.net/xml/ShowList.asp?id=11
      

  3.   

    http://sz.luohuedu.net/xml/ShowList.asp?id=11
      

  4.   

    谢谢,但是我按你的要求改了,在加载的时候仍然出错,
    <?xml version="1.0" encoding="GB2312"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict"> <xsl:template match="/">
    <HTML><HEAD><TITLE>1999年生产统计</TITLE></HEAD> 
    <BODY><xsl:apply-templates select="document"/></BODY>
    </HTML>
    </xsl:template><xsl:template match="document">
    <H3>1999年生产统计</H3> 
    <TABLE border="1" cellspacing="0"> 
    <TH>班组</TH>
    <TH>一季度</TH>
    <TH>二季度</TH>
    <TH>三季度</TH>
    <TH>四季度</TH>
    <xsl:apply-templates select="report"/>
    </TABLE>
    </xsl:template><xsl:template match="report">
    <TR>
    <TD><xsl:value-of select="class"/></TD>
    <TD><xsl:apply-templates select="q1"/></TD>
    <TD><xsl:apply-templates select="q2"/></TD>
    <TD><xsl:apply-templates select="q3"/></TD>
    <TD><xsl:apply-templates select="q4"/></TD>
    </TR>
    </xsl:template><xsl:template match="q1|q2|q3|q4">
    <!--此处测试产量,如小于等于20则添加一STYLE属性color,其值为red(红色)--> 
    <xsl:if test=".[value()$le$20]"> 
    <xsl:attribute name="style">color:red</xsl:attribute>
    </xsl:if>
    <xsl:value-of/>
    </xsl:template></xsl:stylesheet>
    C#里的转换函数:
    public System.Xml.XmlDocument TransForm(string xmlsource,string xslsource)
    {
    System.Xml.XmlDocument xd = new XmlDocument();
    XslTransform xslt = new XslTransform(); try
    {
        xslt.Load(xslsource); 
    }
    catch(Exception ex)
    {
    string errmsg = ex.Message; //这里仍然出现错误
    }

    XPathDocument xpathdocument = new
    XPathDocument(xmlsource);
    System.IO.Stream MemStream = new System.IO.MemoryStream(); 
    XmlTextWriter writer = new XmlTextWriter(MemStream, Encoding.GetEncoding("GB2312"));
    writer.Formatting=Formatting.Indented;
    xslt.Transform(xpathdocument, null, writer, null);    
    MemStream.Position = 0;
    xd.Load(MemStream);
    MemStream.Close();
    return xd; }
      

  5.   

    现在按孟大哥的方法改了过后,在浏览器中也不正常了,提示:无法显示 XML 页。 
    使用 XSL 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 
    --------------------------------------------------------------------------------预期的句柄 'eof' 找到 '['。 .-->[<--value()$le$20] 
      

  6.   

    我按MSDN上的例子改了一下,输出到文件中,现在测试已经可以了,但是又遇到一个新问题,那个例子转换后没有根元素,所以不是一个格式良好的XML文档,我想在原来的基础上,头尾加root标签要如何修改XSL文件呢? 
    MSDN参考链接:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconapplyingxsltransformtodataset.asp原来的:XSL文档<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="CustomerOrders">
      <HTML>
      <STYLE>
      BODY {font-family:verdana;font-size:9pt}
      TD   {font-size:8pt}
      </STYLE>
        <BODY>
        <TABLE BORDER="1">
          <xsl:apply-templates select="Customers"/>
        </TABLE>
        </BODY>
      </HTML>
    </xsl:template><xsl:template match="Customers">
        <TR><TD>
          <xsl:value-of select="ContactName"/>, <xsl:value-of select="Phone"/><BR/>
        </TD></TR>
          <xsl:apply-templates select="Orders"/>
    </xsl:template><xsl:template match="Orders">
      <TABLE BORDER="1">
        <TR><TD valign="top"><B>Order:</B></TD><TD valign="top"><xsl:value-of select="OrderID"/></TD></TR>
        <TR><TD valign="top"><B>Date:</B></TD><TD valign="top"><xsl:value-of select="OrderDate"/></TD></TR>
        <TR><TD valign="top"><B>Ship To:</B></TD>
            <TD valign="top"><xsl:value-of select="ShipName"/><BR/>
            <xsl:value-of select="ShipAddress"/><BR/>
            <xsl:value-of select="ShipCity"/>, <xsl:value-of select="ShipRegion"/>  <xsl:value-of select="ShipPostalCode"/><BR/>
            <xsl:value-of select="ShipCountry"/></TD></TR>
      </TABLE>
    </xsl:template>
    </xsl:stylesheet>