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);

    源代码

  • 相关阅读:
    ICPC-Beijing 2006 狼抓兔子
    【模板】多项式求逆
    AHOI2014/JSOI2014 奇怪的计算器
    Hnoi2013 切糕
    Ahoi2014&Jsoi2014 支线剧情
    bzoj3774 最优选择
    WC2019游记
    HNOI2007 分裂游戏
    bzoj1457 棋盘游戏
    poj2484 A Funny Game
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/2378543.html
Copyright © 2011-2022 走看看