根據參數運用XSL顯示XML數據(包含一些格式化效果)

本次實踐所要實現的效果為
可以根據輸入的參數 動態調用XML數據並顯示.
顯示的數據  實現 自動序號, 超聯接,樣式設定等效果
效果圖如下:

* * * * * * * * *
查詢按鈕代碼
* * * * * * * * *
   protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("link.xml"));

        XslTransform xsltrans = new XslTransform();
        xsltrans.Load(Server.MapPath("linktable.xsl"));

        System.Xml.Xsl.XsltArgumentList xslpara = new XsltArgumentList();
        xslpara.AddParam("para", "", this.TextBox1.Text);
        //xslpara.AddParam("para", "", "zhangsan");

        Xml1.Transform = xsltrans;
        Xml1.TransformArgumentList = xslpara;
        Xml1.Document = xmldoc;
    }
= = = = = = = = = = = = = = = =
* * * * * *
link.xml
* * * * * *
<?xml version="1.0" encoding="utf-8" ?>
<links>
  <link>
    <name>zhangsan</name>
    <age>24</age>
    <email etype="a">zhangsan@email.com</email>
    <hreflink>http://freeliver54.cnblogs.com</hreflink>
  </link>
  <link>
    <name>lisi</name>
    <age>23</age>
    <email etype="b">lisi@email.com</email>
    <hreflink>http://freeliver54.cnblogs.com</hreflink>
  </link>
</links>
= = = = = = = = = = = = = = = = = = =
* * * * * * *
linktable.xsl
* * * * * * *
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- 定義參數-->
  <xsl:param name="para"></xsl:param>
    <xsl:template match="/links">
    <table>
      <xsl:variable name="begin"  select="0"></xsl:variable>
      <xsl:variable name="end" select="1"></xsl:variable>      
        <!-- 根據參數顯示-->
        <xsl:for-each select="link[contains(name,$para)]">
          <!-- for-each 下的 xsl:sort -->
          <xsl:sort order="descending" select="age"/>
        <tr>
          <!-- 序號-->
          <td>
            <xsl:number  value="position()" format="1."/>
          </td>
          <!-- 超級連接-->
          <td>
            <a href="{hreflink}" target="_blank">
              <xsl:value-of select="name"/>
            </a>
          </td>
          <!-- 添加樣式-->
          <td>
            <xsl:attribute name="style">color:red</xsl:attribute>
            <xsl:value-of select="age"/>
          </td>       
          <!-- 判斷節點包含-->
          <td>
            <xsl:choose>
              <!--
                 <xsl:when test="count(/name[age])=0">不包含</xsl:when>
                 -->
              <xsl:when test="count(/link[age])=0">不包含</xsl:when>
              <xsl:otherwise>包含</xsl:otherwise>
            </xsl:choose>
          </td>
          <!-- 獲得屬性值 用@-->
          <td>
            <xsl:value-of select="email/@etype"/>
            <!-- xsl文件里设置连续空格-->
            <xsl:text disable-output-escaping="yes">        </xsl:text>
            mail:
            <xsl:value-of select="email"/>
          </td>
          <!-- xsl:choose-->
          <td>
            <xsl:choose>
              <xsl:when test="name[.='zhangsan']">張三</xsl:when>
              <xsl:otherwise>不認識</xsl:otherwise>
            </xsl:choose>
          </td>
          <!-- xsl:if-->
          <td>
            <xsl:if test="age[.='23']">不到24年輕啊</xsl:if>
          </td>
          <!-- 自定義的其他頁面內容-->
          <td>其他頁面內容</td>
        </tr>
        </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>
= = = = = = = = = = = = = =
如果要實現分頁顯示等效果 可以把XML數據讀入DataSet後 加上其他如GridView控件 再做進一步處理
更多更強XSLT內容 請參閱
http://www.w3.org/TR/xslt

posted on 2006-02-15 16:43  freeliver54  阅读(562)  评论(1编辑  收藏

导航