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. sp_who_lock

    USE MyDataBase GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 4/10/2015 ******/ ...

  2. Debian MySQL 卸载和安装 PHP安装

    如果是apt-get安装的 sudo apt-get --purge remove mysql-serversudo apt-get --purge remove mysql-clientsudo a ...

  3. SQL 常用的命令 (转)

    地址:http://www.cnblogs.com/longly/p/6030609.html 设置SQL语句所用的字符编码:set names UTF8; 判断指定的数据库是否存在:DROP DAT ...

  4. php 对url 操作类:url拼接、get获取页面、post获取页面(带传参)

    /* * @brief url封装类,将常用的url请求操作封装在一起 * */ class URL{ private $error; public function __construct(){ $ ...

  5. HBase内部操作日志说明

    版本:0.94-cdh4.2.1 1. Split Region [regionserver60020-splits-1397585864985] INFO org.apache.hadoop.hba ...

  6. iOS5.0以上使用新浪微博开放平台OAuth 续(及解决登录无效问题)

    新浪微博开放平台为第三方应用提供了简便的合作模式,满足了手机用户和平板电脑用户随时随地分享信息的需求.通过调用平台的api即可实现很多微博上的功能. 本篇主要目的是记录新浪微博移动SDK iOS版本的 ...

  7. android-studio设置代理

    /home/{user}/.gradle/gradle.properties org.gradle.jvmargs=-Xmx1536m -DsocksProxyHost=127.0.0.1 -Dsoc ...

  8. Arduino 翻译系列 - LED 灯闪烁

    原文地址 - https://www.arduino.cc/en/Tutorial/Blink 闪烁 这个例子展示了你能拿 Arduino / Genuino 板子来干的最简单的事:使开发板上的 LE ...

  9. ActiveMQ与WebSocket的结合应用

    前段时间无意之中看到了WebSocket这样东西,发现自己真的是火星了.早在11年就有的东西,一直都不知道. 研究了一番之后感觉还是比较好用的. 我很少做Socket开发,但是曾经由于项目的原因,用过 ...

  10. XE6 & IOS开发之开发者账号、苹果证书(1):关于开发者账号

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 关于苹果开发者账号, 注 ...