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 因为 ...
随机推荐
- kali 初始化
关于kali使用前的一些配置,网上有很多版本,但是几乎都很雷同,或者是不全,或者是根本就没有测试过,或者是有的方法是错的(换句话说是版本变化的差异),因此让很多人接触kali时百度无数,效果一般,浪费 ...
- DSP中.gel文件的作用
GEL是CCS提供的一种解释语言,使用该语言写出的GEL,函数具有两在功能,一是配置CCS工作环境,二是直接访问目标处理器DSP(包括DSP软/硬仿真器).用户可以使用GEL函数完成类似于宏操作的自动 ...
- win 安装mysql
windows上安装sql最容易出现 1067错误,网上查了很多,大部分都是误导.现在将验证过的步骤总结如下: 1.下载mysql,我用的是mysql-5.6.24-win32 下载后解压,进入到bi ...
- python---set集合
集合(set)是一个无序不重复元素的序列. #!/usr/bin/python3 student = ({'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}) p ...
- 图片Exif 信息中Orientation的理解和对此的处理
这个问题是在用七牛上传图片后获取宽高时发现的,一张图片,用图片浏览器打开始终是竖图,但是查看属性或者用七牛获取宽高,却发现宽大于高,也就是在属性中这是个横图.这样导致客户端用该宽高来展示图片会出现问题 ...
- 【学】AngularJS日记(1) - 常用工具
angular.isArray(a) 判断a是否为数组 angular.isDate 是否为时间对象 angular.isDefined 判断元素是否存在 angular.isUndefined an ...
- c#Ice开发之环境配置(一)
第一步,基于Windows下的安装,所以下载windows版的Ice,官网最新版本是Ice3.5.1: http://www.zeroc.com/download/ 安装完成可以在vs-工具的最下面看 ...
- [AS3.0] HTMLLoader与js交互
HtmlLoader 的属性window是加载到 HTML 控件中的内容的全局 JavaScript 对象,通过这个对象能够方便的和页面js通讯. AS代码: import flash.html.HT ...
- 【HOW】如何手工编辑InfoPath文件
因为直接打开InfoPath的配置文件进行编辑会有些无从下手,所以下面以修改“节”的Margin为例来说明手工编辑InfoPath文件的过程. 1. 新建一个“节”,并在此节中创建一个数字类型字段“n ...
- 用直接路径(direct-path)insert提升性能的两种方法
1.传统串行insert方式 常见的insert方式有两种: (1) insert into table_name values(....) (2) insert into target_table ...