基于XSLT调试的相关问题

所属分类: 软件编程 / C#教程 阅读数: 46
收藏 0 赞 0 分享

新建控制台程序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 &lt; $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; lt less than 的意思,同理> xml里面是&gt; gt 就是great than的意思。

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

image 

调试xslt 有两种方式:

第一种:使用VS

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

image

同样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 属性修改如下:

image

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

参数传递为true,代表enableDebug.

就可以看到如下界面了:

image 

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

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

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多