基于XSLT调试的相关问题
发布时间:2022-11-01 14:22:53
新建控制台程序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 有两种方式:
第一种:使用VS
打开xsl,可以发现菜单多了XML,点击XML菜单的调试XSLT,然后选择book.xml 就可以进行调试了。
同样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 属性修改如下:
还要将XslCompiledTransform xslt = new XslCompiledTransform(true);
参数传递为true,代表enableDebug.
就可以看到如下界面了:
使用代码调试的话,不需要设置断点,只要enableDebug为true的话,会自动在xsl中中断。
本人猜测估计是调用了Debugger.Break() 方法。


猜你喜欢
- 背景在很多场景下面我们需要在集合发生变化的时候能够通过一个事件对外进行通知,默认的List<T>并没有此类功能,所以对于这一类需
- 前言安卓6.0之后,一些敏感权限需要进行动态请求,虽说编写请求授权代码并不难,但是每次一需要权限就需要在视图中添加一段代码,严重影响代码美观
- 完成一个简单的基于MVC的数据查询模块,要求能够按照name进行模糊查询。Index.jsp:<%@ page import=&quo
- 前言在这个 Spring Security 教程中,我很乐意与您分享如何通过在 Java Web 应用程序中为用户添加角色来实现授权&
- 硬件环境:小米4Android版本:6.0咱们先看效果图:我把代码贴出来:AndroidMainfest.xml文件(需要新增camera权
- 前言我们通常使用Spring boot做项目搭建的基础框架,必然少不了它的内置日志框架Logback,在spring-boot-starte
- 在winform程序中给form添加了keyup事件,但是程序却不响应键盘事件,解决办法是重写Form基类的ProcessCmdKey(re
- 一、使用spring initializr创建java工程 1、启动IDEA,新建java工程,使用向导创建一个springboo
- 本文实例讲述了C#记录消息到日志文件的方法。分享给大家供大家参考。具体实现方法如下:public void LogMessageToFile
- 目录1. 定义排序列数组2. 修改表头点击事件3. 修改表格排序方法4. 修改后台传参实现思路也比较简单,只需要用一个数组来存放所有排序的列
- 最近公司要求开发工具要用Idea,作为一个eclipse的老员工,记录一下Idea中遇到的坑刚开始用Idea从Git上导入一个项目时,遇到了
- 编写RedisConfig首先我们要明白RedisConfig中需要包含什么,首先看看我们直接使用RedisTemplate的问题,我们就知
- 1. JAVA 内存模型 (JMM)JMM是用来干嘛的?:《Java虚拟机规范》中曾试图定义一种“Java内存模型&am
- 前言在真实的项目开发中,使用SpringBoot可以说非常普遍了,而在框架整合中,与数据库的交互无外乎使用jpa,mybatis,mybat
- 本文实例讲述了java和c#使用hessian通信的方法,是非常实用的技巧。分享给大家供大家参考。具体分析如下:首先,hessian主页为:
- 本文实例讲述了Android编程实现自动检测版本及自动升级的方法。分享给大家供大家参考,具体如下:步骤:1.检测当前版本的信息Android
- 目录一、所使用的环境配置:二、项目简介三、知识点总结(代码和配置)SpringBoot:1.Mybatis-Plus配置文件,实现分页查询:
- 策略模式所谓策略其实就是做一件事情有很多很多的方法,比如说一个商场要搞促销,促销的方式有可能有很多:打折啊,满100返50啊、积分等等之类的
- 一、安装activeMQLinux环境ActiveMQ部署方法:https://www.aspxhome.com/article/16232
- C#操作Excel的方法有很多种,常见的有微软官方的OLE Automation,Apache的POI等。这里介绍的是POI翻译成C#的NP