zoukankan      html  css  js  c++  java
  • 使用XSLT转换XML数据并传递参数

    1.首先要做xsd文档中定义一个全局变量 使用<xsl:param />标签进行声明

    2.C#代码中使用XslCompiledTransform中的AddParam方法添加参数,

       XslCompiledTransform 类的Transform方法中传递XslCompiledTransform对象

    XML:

          

    View Code
    <?xml version='1.0'?>
    <bookstore>
    <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
    <first-name>Benjamin</first-name>
    <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
    <first-name>Herman</first-name>
    <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
    <name>Plato</name>
    </author>
    <price>9.99</price>
    </book>
    </bookstore>

    XSL:

    View Code
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <!--设置参数-->
    <xsl:param name="discount" select=".10"/>
    <xsl:template match="bookstore">
    <HTML>
    <BODY>
    <TABLE BORDER="2">
    <TR>
    <TD>ISBN</TD>
    <TD>Title</TD>
    <TD>Price</TD>
    <TD>Calculated Discount</TD>
    </TR>
    <xsl:apply-templates select="book"/>
    </TABLE>
    </BODY>
    </HTML>
    </xsl:template>
    <xsl:template match="book">
    <TR>
    <TD>
    <xsl:value-of select="@ISBN"/>
    </TD>
    <TD>
    <xsl:value-of select="title"/>
    </TD>
    <TD>
    <xsl:value-of select="price"/>
    </TD>
    <TD>
    <xsl:value-of select="price * ($discount)"/>
    </TD>
    </TR>
    </xsl:template>
    </xsl:stylesheet>

    C# 代码:

      

    View Code
            string xmlpath = Request.PhysicalApplicationPath +
    @"\App_Data\Books.xml";
    string xslpath = Request.PhysicalApplicationPath +
    @"\App_Data\Books.xsl";
    XPathDocument xpathDoc = new XPathDocument(xmlpath);
    XslCompiledTransform transform = new XslCompiledTransform();
    XsltArgumentList argsList = new XsltArgumentList();
    argsList.AddParam("discount","",".15");
    transform.Load(xslpath);
    transform.Transform(xpathDoc,argsList, Response.Output);

    源代码

  • 相关阅读:
    NEERC 15 (10/12)
    uoj259 & 独立集问题的一些做法
    Berlekamp-Massey算法简单介绍
    树链剖分的一种用法
    长链剖分
    [黑科技]常数优化的一些技巧
    tarjan解决路径询问问题
    Comet OJ
    洛谷P3835 【模板】可持久化平衡树(FHQ Treap)
    LOJ#107. 维护全序集(FHQ Treap)
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/2378543.html
Copyright © 2011-2022 走看看