欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于XSLT调试的相关问题

程序员文章站 2023-12-16 23:07:28
新建控制台程序castudy.在应用程序中,添加books.xml,belowavg.xsl 代码分别如下: books.xml

新建控制台程序castudy.在应用程序中,添加books.xml,belowavg.xsl 代码分别如下:

books.xml

<?xml version='1.0'?>

<!-- this file represents a fragment of a book store inventory database -->

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

books.xml一看就知道是一个bookstore,里面包含了三个book. 每个book都会有一些attribute和property.例如genre,publicationdate,isbn 就是attribute.而诸如title,author,price 就是book的property 了。

belowavg.xsl:

<?xml version='1.0'?>

<xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.w3.org/1999/xsl/transform">

  <xsl:output method="xml" encoding="utf-8"/>

  <xsl:template match="/">

    <xsl:variable name="bookcount" select="count(/bookstore/book)"/>

    <xsl:variable name="booktotal" select="sum(/bookstore/book/price)"/>

    <xsl:variable name="bookaverage" select="$booktotal div $bookcount"/>

    <books>

      <!--books that cost below average-->

      <xsl:for-each select="/bookstore/book">

        <xsl:if test="price < $bookaverage">

          <xsl:copy-of select="."/>

        </xsl:if>

      </xsl:for-each>

    </books>

  </xsl:template>

</xsl:stylesheet>

belowavg.xsl:名字就代表了,小于平均值的xsl.

xslt: 可扩展样式表语言转换extensible stylesheet transformation (xslt)

这个belowavg.xsl 主要就是将book.xml 中小于平均值的那些book找出来,输出成xml

match=”/”:这样就可以匹配三个book节点了。

接着声明3个变量,bookcount,booktotal,在第三个变量中使用$符号来引用前面声明的变量得到平均值。

接着进行for-each的循环,在循环里面进行if 测试,测试的条件是price < $bookaverage. < xml里面是< lt less than 的意思,同理> xml里面是> gt 就是great than的意思。

接着进行copy-of 操作,”.” 代表的就是self::node(),也就是book节点。

基于XSLT调试的相关问题 

调试xslt 有两种方式:

第一种:使用vs

打开xsl,可以发现菜单多了xml,点击xml菜单的调试xslt,然后选择book.xml 就可以进行调试了。

基于XSLT调试的相关问题

同样f9设置断点,

第二种方法:使用代码.

class xmlxsltdemo

{

    private const string sourcefile = @"books.xml";

    private const string stylesheet = @"belowavg.xsl";

    private const string outputfile = @"output.xml";

    public static void main()

    {

        // enable xslt debugging.

        xslcompiledtransform xslt = new xslcompiledtransform(true);

        // compile the style sheet.

        xslt.load(stylesheet);

        // execute the xslt transform.

        filestream outputstream = new filestream(outputfile, filemode.append);

        xslt.transform(sourcefile, null, outputstream);

    }

}

在这里由于使用的是相对路径,所以要将books.xml和belowavg.xsl 属性修改如下:

基于XSLT调试的相关问题

还要将xslcompiledtransform xslt = new xslcompiledtransform(true);

参数传递为true,代表enabledebug.

就可以看到如下界面了:

基于XSLT调试的相关问题 

使用代码调试的话,不需要设置断点,只要enabledebug为true的话,会自动在xsl中中断。

本人猜测估计是调用了debugger.break() 方法。

上一篇:

下一篇: