Xslt 1.0中使用Array
XSLT Variable Arrays
I recently answered a question on a popular programmers forum about how to store and access an array of user-defined variables in a stylesheet and then loop though those variables. I realized that many developers are not familar with the available techniques for doing this and decided to add an entry in my blog about this topic.
User-defined variable arrays within stylesheets are not part of the XSLT specification. The usual way to handle this problem in XSLT 1.0 stylesheets is to define a user-defined top-level element which belongs to a non-null namespace which is different from the XSLT namspace. These user-defined top-level elements are typically used to store error messages, lookup data, etc. You can then access these user-defined elements from within your stylesheet by treating the stylesheet as an additional source document and loading it using the document() function with an empty string as the first argument. An empty string is interpreted to mean the current stylesheet.
The following stylesheet demonstrates this method.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:foo="http://foo.com" exclude-result-prefixes="foo">
<xsl:output method="text" encoding="utf-8"/>
<foo:vars>
<foo:var name="z1">A</foo:var>
<foo:var name="z2">B</foo:var>
<foo:var name="z3">C</foo:var>
<foo:var name="z4">D</foo:var>
</foo:vars>
<xsl:template match="/">
<xsl:for-each select="document('')/xsl:stylesheet/foo:vars/foo:var" >
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
If you are using XSLT 2.0, this method is no longer needed as simpler and more elegant methods are available to us. For example, you can store and directly access the variables using the <xsl:variable> element as shown in the following stylesheet.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:variable name="z1" select="'A'" />
<xsl:variable name="z2" select="'B'" />
<xsl:variable name="z3" select="'C'" />
<xsl:variable name="z4" select="'D'" />
<xsl:variable name="vars" select="$z1, $z2, $z3, $z4" />
<xsl:template match="/">
<xsl:for-each select="$vars" >
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Another way to store and access this data in a XSLT 2.0 stylesheet is to use a global variable definition as shown below.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:variable name="vars">
<var name="z1">A</var>
<var name="z2">B</var>
<var name="z3">C</var>
<var name="z4">D</var>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="$vars/var" >
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
All three stylesheets output the same data.
Read more: http://blog.fpmurphy.com/2008/12/xslt-variable-arrays.html#ixzz4MkwhVn00
Xslt 1.0中使用Array的更多相关文章
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- Swift2.0 中的String(一):常用属性
字符串算是平常用的比较多.花样也比较多的一个类型,昨天有空把相关的一些常用操作都写了一遍,总结出来.其实iOS里面的字符串更复杂,还有NSString系列等等,那些API太多将来需要用的时候再慢慢学. ...
- 详解Javascript中的Array对象
基础介绍 创建数组 和Object对象一样,创建Array也有2种方式:构造函数.字面量法. 构造函数创建 使用构造函数的方式可以通过new关键字来声明,如下所示: 12 var arr = new ...
- Jdk5.0中出现的新特性
掌握jdk5.0中出现的新特性1.泛型(Generics)2.增强的"for"循环(Enhanced For loop)3.自动装箱/自动拆箱(Autoboxing/unboxin ...
- 结合 category 工作原理分析 OC2.0 中的 runtime
绝大多数 iOS 开发者在学习 runtime 时都阅读过 runtime.h 文件中的这段代码: struct objc_class { Class isa OBJC_ISA_AVAILABILI ...
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下: C++ Code 1 2 template < class _Ty, cl ...
- TC2.0中怎样调用汇编程序
转载于: TC2.0中怎样调用汇编程序 一.概述 TC是美国BORLAND 公司在IBM PC机上开发的一个高效.优化的C编译程序,它自带高效的全屏幕编辑程序,在集成开发环境下可支持编辑.编译.连接调 ...
- 用 for/in 在 Java 5.0 中增强循环
这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...
随机推荐
- java内存的那些事
在Java中,内存的管理分为以下几个部分: Heap:堆区域,存放对象实例,凡是New出来的东西都存放在此. Stack:栈区域,存放基本数据类型.常量.局部变量.对象的引用地址 Data Segme ...
- SQL 导出表结构到Excel
SQL 导出表结构到Excel SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a ...
- 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控
参考上篇文章: <[SFTP]使用Jsch实现Sftp文件下载-支持断点续传和进程监控>:http://www.cnblogs.com/ssslinppp/p/6248763.html ...
- storm基础系列之一----storm并发度概念剖析
前言: 学了几天storm的基础,发现如果有hadoop基础,再理解起概念来,容易的多.不过,涉及到一些独有的东西,如调度,如并发度,还是很麻烦.那么,从这一篇开始,力争清晰的梳理这些知识. 在正式学 ...
- [JavaScript]JS由来
JavaScript最早由Netscape公司开发 JavaScript的发展历程 我们知道Windows桌面程序是可以交互的,用户可以点击菜单.按钮.下拉列表等控件,并通过消息机制来响应用户操作. ...
- JAVA源码走读(二)二分查找与Arrays类
给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找 ...
- SVN has atopping svn已停止工作 or windows资源管理器无限重启
准备在空间时间用用linux,就在自己的win7系统上安装了属性系统,用easyBCD安装的,谁知安装好之后win7系统下的svn客户端不能使用了,点击报错“SVN已停止工作”,随后怀疑是linux引 ...
- Swagger .Net配置
1.NuGet 下载安装 Swagger Install-Package Swashbuckle -Version 5.5.3 2.http://localhost:xxx/swagger 3. 4. ...
- 国内常用DNS地址介绍
1.谷歌全球通用DNS地址: 首选DNS地址:8.8.8.8 备选DNS地址:8.8.4.4 2.国内知名的114 DNS地址 首选DNS地址:114.114.114.114 备选DNS地址:114. ...
- mac 10.9 安装 gevent
安装步骤: Gevent依赖libevent和greenlet,需要分别安装. 1,安装 macport (如已安装,可以跳过) 2,通过终端 键入: sudo port install libeve ...