详解 xls xlst xml 二

我们紧接着上篇,继续xls xlst xml

-----------------------------------我是华丽的分割线-----------------------------------------

11.先看简单的代码

XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="hello.xslt"?>
<message>Hello,world!</message>

Xlst文件:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <html>
        <body>
          <h1>
            <xsl:value-of select="message"/>
          </h1>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

效果如图:

 

12.先准备xml文件

xml文件如下:

View Code
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Products.xslt"?>
<Products>
  <Category Name="笔记本">
    <Product ProductID="100">
      <ProductName>联想</ProductName>
      <ProductNumber>SB-M1911</ProductNumber>
      <Cost>2000</Cost>
      <Price>4000</Price>
    </Product>
    <Product ProductID="200">
      <ProductName>神舟</ProductName>
      <ProductNumber>SZ-M1911</ProductNumber>
      <Cost>1000</Cost>
      <Price>2000</Price>
    </Product>
    <Product ProductID="300">
      <ProductName>惠普</ProductName>
      <ProductNumber>HP-M1911</ProductNumber>
      <Cost>3000</Cost>
      <Price>5000</Price>
    </Product>
  </Category>
  <Category Name="数码相机">
    <Product ProductID="600">
      <ProductName>尼康</ProductName>
      <ProductNumber>NK-M000</ProductNumber>
      <Cost>2000</Cost>
      <Price>4000</Price>
    </Product>
    <Product ProductID="700">
      <ProductName>索尼</ProductName>
      <ProductNumber>SN-M1911</ProductNumber>
      <Cost>1000</Cost>
      <Price>2000</Price>
    </Product>
    <Product ProductID="800">
      <ProductName>松下</ProductName>
      <ProductNumber>SX-M1911</ProductNumber>
      <Cost>3000</Cost>
      <Price>5000</Price>
    </Product>
  </Category>
</Products>

13.使用 xsl:for-each 的xlst

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <table style="background-color:lightgreen">
        <xsl:for-each select="./Products/Category">
          <tr>
            <td style="border:solid 1px black;">
              <xsl:value-of select="./@Name"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:template>
</xsl:stylesheet>

效果如下:

14.使用 xsl:for-each 的xlst 显示更多信息

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <table>
      <tr style="background-color:yellow">
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Product Number</th>
        <th>Cost</th>
        <th>Price</th>
        <th>Category</th>
      </tr>
      <xsl:for-each select="./Products/Category/Product">
        <tr style="background-color:lightgreen">
          <td style="border:solid 1px black;">
            <xsl:value-of select="@ProductID"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductName"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductNumber"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Cost"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Price"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="../@Name"/>
          </td>
        </tr>

      </xsl:for-each>

    </table>
  </xsl:template>
</xsl:stylesheet>

效果如下:

15.使用 xsl:for-each ,xsl:attribute,xsl:if 的xlst

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
<!--使用for-each-->
  <xsl:template match="/">
    <table>
      <tr style="background-color:yellow">
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Product Number</th>
        <th>Cost</th>
        <th>Price</th>
        <th>Category</th>
      </tr>
      <xsl:for-each select="./Products/Category/Product">
        <tr>
          <xsl:attribute name="style">
            <xsl:if test="Cost &gt; 2001">
              background-color:red
            </xsl:if>
            <xsl:if test="Cost &lt; 2001">
              background-color:green
            </xsl:if>
          </xsl:attribute>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@ProductID"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductName"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductNumber"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Cost"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Price"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="../@Name"/>
          </td>
        </tr>

      </xsl:for-each>

    </table>
  </xsl:template>
</xsl:stylesheet>

效果如下:

16.使用 xsl:for-each ,xsl:attribute,xsl:choose 的xlst

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <!--使用choose when-->
  <xsl:template match="/">
    <table>
      <tr style="background-color:yellow">
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Product Number</th>
        <th>Cost</th>
        <th>Price</th>
        <th>Category</th>
      </tr>
      <xsl:for-each select="./Products/Category/Product">
        <tr>
          <!--此处的style是tr的style属性-->
          <xsl:attribute name="style">
            <xsl:choose>
              <xsl:when test="Cost &gt; 2001">
                background-color:red
              </xsl:when>
              <xsl:otherwise>
                background-color:green
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@ProductID"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductName"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductNumber"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Cost"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="Price"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="../@Name"/>
          </td>
        </tr>

      </xsl:for-each>

    </table>
  </xsl:template>
</xsl:stylesheet>

效果15图

17.使用 xsl:for-each ,xsl:attribute,xsl:choose ,format-number ,xsl:sort的xlst

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <!--使用format-number xsl:sort-->
  <xsl:template match="/">
    <table>
      <tr style="background-color:yellow">
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Product Number</th>
        <th>Cost</th>
        <th>Price</th>
        <th>Category</th>
      </tr>
      <xsl:for-each select="./Products/Category/Product">
        <xsl:sort select="Price" order="ascending"/>
        <tr>
          <!--此处的style是tr的style属性-->
          <xsl:attribute name="style">
            <xsl:choose>
              <xsl:when test="Cost &gt; 2001">
                background-color:red
              </xsl:when>
              <xsl:otherwise>
                background-color:green
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@ProductID"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductName"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="ProductNumber"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="format-number(Cost,'#.00')"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="format-number(Price,'#.00')"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:value-of select="../@Name"/>
          </td>
        </tr>

      </xsl:for-each>

    </table>
  </xsl:template>
</xsl:stylesheet>

效果如下:

18.要实现如下图所示的功能,有三种xslt写法

方法一:使用嵌套的xsl:for-each

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <table>
      <xsl:for-each select="./Products/Category">
        <tr>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@Name"/>
          </td>
          <td style="border:solid 1px black;">
            <ul>
              <xsl:for-each select="./Product">
                <li>
                  <xsl:value-of select="@ProductID"/>
                  <xsl:value-of select="ProductName"/>
                </li>
              </xsl:for-each>
            </ul>
          </td>
        </tr>
      </xsl:for-each>

    </table>
  </xsl:template>
</xsl:stylesheet>

方法二:使用xsl:apply-templates 路径

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <table>
      <xsl:for-each select="./Products/Category">
        <tr>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@Name"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:apply-templates select="."></xsl:apply-templates>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template match="Category">
    <ul>
      <xsl:for-each select="./Product">
        <li>
          <xsl:value-of select="@ProductID"/>
          <xsl:value-of select="ProductName"/>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>

方法三:使用xsl:apply-templates name

View Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <table>
      <xsl:for-each select="./Products/Category">
        <tr>
          <td style="border:solid 1px black;">
            <xsl:value-of select="@Name"/>
          </td>
          <td style="border:solid 1px black;">
            <xsl:call-template name="C"></xsl:call-template>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template match="Category" name="C">
    <ul>
      <xsl:for-each select="./Product">
        <li>
          <xsl:value-of select="@ProductID"/>
          <xsl:value-of select="ProductName"/>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>

19.使用.Net中的xlst功能

   a)从工具箱中,拖一个xml控件,如图:

    设置xml控件的属性:

    这样效果,和18图一样

  b)通过代码的方式:

View Code
    public static string GetString(string path)
        {
            /*
             * select top 5 
                SalesLT.ProductCategory.Name "Category/@Name",
                ProductID "Category/Product/@ProductID",
                SalesLT.Product.Name "Category/Product/ProductName",
                ProductNumber "Category/Product/ProductNumber",
                StandardCost "Category/Product/Cost",
                ListPrice "Category/Product/Price" 
                from SalesLT.Product join SalesLT.ProductCategory 
                on SalesLT.Product.ProductCategoryID=SalesLT.ProductCategory.ProductCategoryID
                for xml path(''),root('Products')
             */

            string sql = @"select top 5 
                                    SalesLT.ProductCategory.Name ""Category/@Name"",
                                    ProductID ""Category/Product/@ProductID"",
                                    SalesLT.Product.Name ""Category/Product/ProductName"",
                                    ProductNumber ""Category/Product/ProductNumber"",
                                    StandardCost ""Category/Product/Cost"",
                                    ListPrice ""Category/Product/Price"" 
                                    from SalesLT.Product join SalesLT.ProductCategory 
                                    on SalesLT.Product.ProductCategoryID=SalesLT.ProductCategory.ProductCategoryID
                                    for xml path(''),root('Products')";
            SqlConnection conn = new SqlConnection("Data Source=Refactor;Initial Catalog=AdventureWorksLT;user id=sa;pwd=a123456");
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            XmlReader reader = cmd.ExecuteXmlReader();
            reader.Read();
            string s = reader.ReadOuterXml();
            reader.Close();
            conn.Close();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            XslCompiledTransform ct = new XslCompiledTransform();
            ct.Load(path);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            ct.Transform(doc, null, ms);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

 

posted on 2012-06-04 18:31  refactor  阅读(3401)  评论(0编辑  收藏  举报

导航