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的更多相关文章

  1. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  2. Swift2.0 中的String(一):常用属性

    字符串算是平常用的比较多.花样也比较多的一个类型,昨天有空把相关的一些常用操作都写了一遍,总结出来.其实iOS里面的字符串更复杂,还有NSString系列等等,那些API太多将来需要用的时候再慢慢学. ...

  3. 详解Javascript中的Array对象

    基础介绍 创建数组 和Object对象一样,创建Array也有2种方式:构造函数.字面量法. 构造函数创建 使用构造函数的方式可以通过new关键字来声明,如下所示: 12 var arr = new ...

  4. Jdk5.0中出现的新特性

    掌握jdk5.0中出现的新特性1.泛型(Generics)2.增强的"for"循环(Enhanced For loop)3.自动装箱/自动拆箱(Autoboxing/unboxin ...

  5. 结合 category 工作原理分析 OC2.0 中的 runtime

    绝大多数 iOS 开发者在学习 runtime 时都阅读过 runtime.h 文件中的这段代码: struct objc_class { Class isa  OBJC_ISA_AVAILABILI ...

  6. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  7. TC2.0中怎样调用汇编程序

    转载于: TC2.0中怎样调用汇编程序 一.概述 TC是美国BORLAND 公司在IBM PC机上开发的一个高效.优化的C编译程序,它自带高效的全屏幕编辑程序,在集成开发环境下可支持编辑.编译.连接调 ...

  8. 用 for/in 在 Java 5.0 中增强循环

    这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...

  9. ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展

    关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...

随机推荐

  1. C++程序设计(关于函数中数组传递的一点心得)

    题目: 10个学生考完期末考试评卷完成后,老师需要划出及格线,要求如下: (1) 及格线是10的倍数: (2) 保证至少有60%的学生及格: (3) 如果所有的学生都高于60分,则及格线为60分:   ...

  2. wampserver修改mysql默认字符集

    [client] default-character-set=utf8 [mysqld]character_set_server = utf8 重启服务

  3. IIS7下使用4.0框架集成模式URLRewriter重写中文URL乱码问题

    解决方法: 1 url中中文先通过Server.UrlEncode编码 2 修改网站编码 3.更改URLRewriter.net中源码 找到RewriterEngine类中 private strin ...

  4. 解决在VS2015下用C++开发的DLL在WIN7上无法加载运行

    首先用Dependency Walker检查该DLL依赖的库,如下图所示: 依赖的动态库除了KERNEL32.DLL.USER32.DLL外,还包括了MSVCP120D.DLL以及MSVCR120D. ...

  5. 初版storm项目全流程自动化测试代码实现

    由于项目需要,写了版针对业务的自动化测试代码,主要应用场景在于由于业务日趋复杂,一些公共代码的改动,担心会影响已有业务.还没进行重写,但知识点还是不少的与大家分享实践下.首先,介绍下整个流处理的业务流 ...

  6. 关于 Direct2D

    http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd370987(v=vs.85).aspx 本主题介绍 Direct2D,这是 Win ...

  7. Spring整合JUnit框架进行单元测试代码使用详解

    一.Spring提供的JUnit框架扩展:   1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元 ...

  8. 如何自定义Flask中的响应类

    http://codingpy.com/article/customizing-the-flask-response-class/

  9. sqlalchemy

    #!/usr/bin/env python #coding:utf8 from sqlalchemy import create_engine,and_,or_,func,Table from sql ...

  10. flume整合kafka

    # Please paste flume.conf here. Example: # Sources, channels, and sinks are defined per # agent name ...