• 使用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);

    源代码

  • 相关阅读:
    Golang的math包常用方法
    部署tomcat部署实战案例
    CentOS 7.6操作系统部署JDK实战案例
    Linux防火墙iptables命令管理入门
    Docker镜像-基于DockerFile制作编译版nginx镜像
    使用Docker快速部署Mysql服务器
    Docker镜像-基于DockerFile制作yum版nginx镜像
    Docker镜像-手动制作yum版nginx镜像
    Docker容器操作基础命令
    Docker镜像管理篇
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/2378543.html
Copyright © 2020-2023  润新知