转自:http://blog.csdn.net/crystalbruce/article/details/7407631

2007年1月,W3C发布了XSLT2.0规范,2009年发布了XSLT2.1,XSLT3.0预计今年发布

分组

函数:node-set current-group():该函数返回分组所包含的节点集。

函数:node-set current-grouping-key():该函数返回当前分组的条件(控制分分组的关键节点)。

<!-- Category: instruction -->
<xsl:for-each-group
  select
= expression
  group-by? = expression
  group-adjacent? = expression
  group-starting-with? = pattern
  group-ending-with? = pattern
  collation? = { uri }>
  <!-- Content: (xsl:sort*, sequence-constructor) -->
</xsl:for-each-group>

属性详解:

1:select:用于指定需要分组的节点;

2:group-by:用于控制分组的关键节点;

3:group-adjacent:用于控制分组的关键节点,并且只将相邻的元素分为一组;

4:group-starting-with:指定分组的开始节点;

5:group-ending-with:指定分组的结束节点;

Example: Grouping Nodes based on Common Values

The following example groups a list of nodes based on common values. The resulting groups are numbered but unsorted, and a total is calculated for each group.

Source XML document:

  1. <cities>
  2. <city name="Milano"  country="Italia"      pop="5"/>
  3. <city name="Paris"   country="France"      pop="7"/>
  4. <city name="München" country="Deutschland" pop="4"/>
  5. <city name="Lyon"    country="France"      pop="2"/>
  6. <city name="Venezia" country="Italia"      pop="1"/>
  7. </cities>
<cities>
<city name="Milano" country="Italia" pop="5"/>
<city name="Paris" country="France" pop="7"/>
<city name="München" country="Deutschland" pop="4"/>
<city name="Lyon" country="France" pop="2"/>
<city name="Venezia" country="Italia" pop="1"/>
</cities>

Desired output:

  1. <table>
  2. <tr>
  3. <th>Position</th>
  4. <th>Country</th>
  5. <th>List of Cities</th>
  6. <th>Population</th>
  7. </tr>
  8. <tr>
  9. <td>1</td>
  10. <td>Italia</td>
  11. <td>Milano, Venezia</td>
  12. <td>6</td>
  13. </tr>
  14. <tr>
  15. <td>2</td>
  16. <td>France</td>
  17. <td>Lyon, Paris</td>
  18. <td>9</td>
  19. </tr>
  20. <tr>
  21. <td>3</td>
  22. <td>Deutschland</td>
  23. <td>München</td>
  24. <td>4</td>
  25. </tr>
  26. </table>
<table>
<tr>
<th>Position</th>
<th>Country</th>
<th>List of Cities</th>
<th>Population</th>
</tr>
<tr>
<td>1</td>
<td>Italia</td>
<td>Milano, Venezia</td>
<td>6</td>
</tr>
<tr>
<td>2</td>
<td>France</td>
<td>Lyon, Paris</td>
<td>9</td>
</tr>
<tr>
<td>3</td>
<td>Deutschland</td>
<td>München</td>
<td>4</td>
</tr>
</table>

Solution:

  1. <table xsl:version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <tr>
  3. <th>Position</th>
  4. <th>Country</th>
  5. <th>City List</th>
  6. <th>Population</th>
  7. </tr>
  8. <xsl:for-each-group select="cities/city" group-by="@country">
  9. <tr>
  10. <td><xsl:value-of select="position()"/></td>
  11. <td><xsl:value-of select="@country"/></td>
  12. <td>
  13. <xsl:value-of select="current-group()/@name" separator=", "/>
  14. </td>
  15. <td><xsl:value-of select="sum(current-group()/@pop)"/></td>
  16. </tr>
  17. </xsl:for-each-group>
  18. </table>
<table xsl:version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<tr>
<th>Position</th>
<th>Country</th>
<th>City List</th>
<th>Population</th>
</tr>
<xsl:for-each-group select="cities/city" group-by="@country">
<tr>
<td><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="@country"/></td>
<td>
<xsl:value-of select="current-group()/@name" separator=", "/>
</td>
<td><xsl:value-of select="sum(current-group()/@pop)"/></td>
</tr>
</xsl:for-each-group>
</table>

自定义函数

<!-- Category: declaration -->
<xsl:function
  name = qname
  as? = sequence-type
  override? = "yes" | "no">
  <!-- Content: (xsl:param*, sequence-constructor) -->
</xsl:function>

属性详解:

1:name:函数名;

2:as:函数的返回值;

3:override:当存在同名函数时,是否覆盖。

<funciton.../>中可包含N个<param.../>子元素,用于为该函数定义形参。

<!-- Category: declaration -->
<xsl:param
  name = qname
  select? = expression
  as? = sequence-type
  required? = "yes" | "no"
  tunnel? = "yes" | "no">
  <!-- Content: sequence-constructor -->
</xsl:param>

属性详解:

as:指定形参的数据类型;

tunnel:默认是no,用于指定该参数是一个tunnel参数。

Example: A Stylesheet Function

The following example creates a recursive stylesheet function named str:reverse that reverses the words in a supplied sentence, and then invokes this function from within a template rule.

  1. <xsl:transform
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:str="http://example.com/namespace"
  5. version="2.0"
  6. exclude-result-prefixes="str">
  7. <xsl:function name="str:reverse" as="xs:string">
  8. <xsl:param name="sentence" as="xs:string"/>
  9. <xsl:sequence
  10. select="if (contains($sentence, ' '))
  11. then concat(str:reverse(substring-after($sentence, ' ')),
  12. ' ',
  13. substring-before($sentence, ' '))
  14. else $sentence"/>
  15. </xsl:function>
  16. <xsl:template match="/">
  17. <output>
  18. <xsl:value-of select="str:reverse('DOG BITES MAN')"/>
  19. </output>
  20. </xsl:template>
  21. </xsl:transform>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:str="http://example.com/namespace"
version="2.0"
exclude-result-prefixes="str"> <xsl:function name="str:reverse" as="xs:string">
<xsl:param name="sentence" as="xs:string"/>
<xsl:sequence
select="if (contains($sentence, ' '))
then concat(str:reverse(substring-after($sentence, ' ')),
' ',
substring-before($sentence, ' '))
else $sentence"/>
</xsl:function> <xsl:template match="/">
<output>
<xsl:value-of select="str:reverse('DOG BITES MAN')"/>
</output>
</xsl:template> </xsl:transform>

An alternative way of writing the same function is to implement the conditional logic at the XSLT level, thus:

  1. <xsl:function name="str:reverse" as="xs:string">
  2. <xsl:param name="sentence" as="xs:string"/>
  3. <xsl:choose>
  4. <xsl:when test="contains($sentence, ' ')">
  5. <xsl:sequence select="concat(str:reverse(substring-after($sentence, ' ')),
  6. ' ',
  7. substring-before($sentence, ' '))"/>
  8. </xsl:when>
  9. <xsl:otherwise>
  10. <xsl:sequence select="$sentence"/>
  11. </xsl:otherwise>
  12. </xsl:choose>
  13. </xsl:function>
<xsl:function name="str:reverse" as="xs:string">
<xsl:param name="sentence" as="xs:string"/>
<xsl:choose>
<xsl:when test="contains($sentence, ' ')">
<xsl:sequence select="concat(str:reverse(substring-after($sentence, ' ')),
' ',
substring-before($sentence, ' '))"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="$sentence"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function> 

Final Result Trees

<!-- Category: instruction -->
<xsl:result-document
  format? = { qname }
  href? = { uri-reference }
  validation? = "strict" | "lax" | "preserve" | "strip"
  type? = qname
  method? = { "xml" | "html" | "xhtml" | "text" | qname-but-not-ncname }
  byte-order-mark? = { "yes" | "no" }
  cdata-section-elements? = { qnames }
  doctype-public? = { string }
  doctype-system? = { string }
  encoding? = { string }
  escape-uri-attributes? = { "yes" | "no" }
  include-content-type? = { "yes" | "no" }
  indent? = { "yes" | "no" }
  media-type? = { string }
  normalization-form? = { "NFC" | "NFD" | "NFKC" | "NFKD" | "fully-normalized" | "none" |nmtoken }
  omit-xml-declaration? = { "yes" | "no" }
  standalone? = { "yes" | "no" | "omit" }
  undeclare-prefixes? = { "yes" | "no" }
  use-character-maps? = qnames
  output-version? = { nmtoken }>
  <!-- Content: sequence-constructor -->
</xsl:result-document>

属性解释:

1:format:指定输出结果文档的格式,属性值是一个<output.../>元素的name属性值。

2:href:指定输出结果文档的文件路径。

Example: Multiple Result Documents

The following example takes an XHTML document as input, and breaks it up so that the text following each <h1> element is included in a separate document. A new documenttoc.html is constructed to act as an index:

  1. <xsl:stylesheet
  2. version="2.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:xhtml="http://www.w3.org/1999/xhtml">
  5. <xsl:output name="toc-format" method="xhtml" indent="yes"
  6. doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  7. doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
  8. <xsl:output name="section-format" method="xhtml" indent="no"
  9. doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  10. doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
  11. <xsl:template match="/">
  12. <xsl:result-document href="toc.html" format="toc-format" validation="strict">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head><title>Table of Contents</title></head>
  15. <body>
  16. <h1>Table of Contents</h1>
  17. <xsl:for-each select="/*/xhtml:body/(*[1] | xhtml:h1)">
  18. <p><a href="section{position()}.html"><xsl:value-of select="."/></a></p>
  19. </xsl:for-each>
  20. </body>
  21. </html>
  22. </xsl:result-document>
  23. <xsl:for-each-group select="/*/xhtml:body/*" group-starting-with="xhtml:h1">
  24. <xsl:result-document href="section{position()}.html"
  25. format="section-format" validation="strip">
  26. <html xmlns="http://www.w3.org/1999/xhtml">
  27. <head><title><xsl:value-of select="."/></title></head>
  28. <body>
  29. <xsl:copy-of select="current-group()"/>
  30. </body>
  31. </html>
  32. </xsl:result-document>
  33. </xsl:for-each-group>
  34. </xsl:template>
  35. </xsl:stylesheet>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xsl:output name="toc-format" method="xhtml" indent="yes"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/> <xsl:output name="section-format" method="xhtml" indent="no"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/> <xsl:template match="/">
<xsl:result-document href="toc.html" format="toc-format" validation="strict">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Table of Contents</title></head>
<body>
<h1>Table of Contents</h1>
<xsl:for-each select="/*/xhtml:body/(*[1] | xhtml:h1)">
<p><a href="section{position()}.html"><xsl:value-of select="."/></a></p>
</xsl:for-each>
</body>
</html>
</xsl:result-document>
<xsl:for-each-group select="/*/xhtml:body/*" group-starting-with="xhtml:h1">
<xsl:result-document href="section{position()}.html"
format="section-format" validation="strip">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title><xsl:value-of select="."/></title></head>
<body>
<xsl:copy-of select="current-group()"/>
</body>
</html>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template> </xsl:stylesheet> 

字符映射

<!-- Category: declaration -->
<xsl:character-map
  name = qname
  use-character-maps? = qnames>
  <!-- Content: (xsl:output-character*) -->
</xsl:character-map>

属性解释:

1:name:标识符;

2:use-character-maps:用于包含另外一个字符映射。

<xsl:output-character
  character = char
  string = string />

属性解释:

用character的属性值替代string的属性值。

Example: Using Character Maps to Generate Non-XML Output

Character maps can be useful when producing serialized output in a format that resembles, but is not strictly conformant to, HTML or XML. For example, when the output is a JSP page, there might be a need to generate the output:

  1. <jsp:setProperty name="user" property="id" value='<%= "id" + idValue %>'/>
<jsp:setProperty name="user" property="id" value='<%= "id" + idValue %>'/>

Although this output is not well-formed XML or HTML, it is valid in Java Server Pages. This can be achieved by allocating three Unicode characters (which are not needed for any other purpose) to represent the strings<%,%>, and ", for example:

  1. <xsl:character-map name="jsp">
  2. <xsl:output-character character="«" string="<%"/>
  3. <xsl:output-character character="»" string="%>"/>
  4. <xsl:output-character character="§" string='"'/>
  5. </xsl:character-map>
<xsl:character-map name="jsp">
<xsl:output-character character="«" string="<%"/>
<xsl:output-character character="»" string="%>"/>
<xsl:output-character character="§" string='"'/>
</xsl:character-map>

When this character map is referenced in the xsl:output declaration, the required output can be produced by writing the following in the stylesheet:

  1. <jsp:setProperty name="user" property="id" value='«= §id§ + idValue »'/>
<jsp:setProperty name="user" property="id" value='«= §id§ + idValue »'/>

This works on the assumption that when an apostrophe or quotation mark is generated as part of an attribute value by the use of character maps, the serializer will (where possible) use the other choice of delimiter around the attribute value.

数据类型绑定

在<variable.../>、<param.../>和<with-param.../>元素中使用as属性,指定其数据类型,前面示例中用过。

正则表达式

<!-- Category: instruction -->
<xsl:analyze-string
  select = expression
  regex = { string }
  flags? = { string }>
  <!-- Content: (xsl:matching-substring?, xsl:non-matching-substring?, xsl:fallback*) -->
</xsl:analyze-string>

<xsl:matching-substring>
  <!-- Content: sequence-constructor -->
</xsl:matching-substring>

<xsl:non-matching-substring>
  <!-- Content: sequence-constructor -->
</xsl:non-matching-substring>

呃!直接理解字面意思就可

Example: Parsing a Date

Problem: the input string contains a date such as 23 March 2002. Convert it to the form2002-03-23.

Solution (with no error handling if the input format is incorrect):

  1. <xsl:variable name="months" select="'January', 'February', 'March', ..."/>
  2. <xsl:analyze-string select="normalize-space($input)"
  3. regex="([0-9]{{1,2}})\s([A-Z][a-z]+)\s([0-9]{{4}})">
  4. <xsl:matching-substring>
  5. <xsl:number value="regex-group(3)" format="0001"/>
  6. <xsl:text>-</xsl:text>
  7. <xsl:number value="index-of($months, regex-group(2))" format="01"/>
  8. <xsl:text>-</xsl:text>
  9. <xsl:number value="regex-group(1)" format="01"/>
  10. </xsl:matching-substring>
  11. </xsl:analyze-string>
<xsl:variable name="months" select="'January', 'February', 'March', ..."/>

<xsl:analyze-string select="normalize-space($input)"
regex="([0-9]{{1,2}})\s([A-Z][a-z]+)\s([0-9]{{4}})">
<xsl:matching-substring>
<xsl:number value="regex-group(3)" format="0001"/>
<xsl:text>-</xsl:text>
<xsl:number value="index-of($months, regex-group(2))" format="01"/>
<xsl:text>-</xsl:text>
<xsl:number value="regex-group(1)" format="01"/>
</xsl:matching-substring>
</xsl:analyze-string>

Note the use of normalize-space to simplify the work done by the regular expression, and the use of doubled curly brackets because theregex attribute is an attribute value template.

XSLT2.0实用的新功能 .(转)的更多相关文章

  1. MySQL 8.0有什么新功能

    https://mysqlserverteam.com/whats-new-in-mysql-8-0-generally-available/ 我们自豪地宣布MySQL 8.0的一般可用性. 现在下载 ...

  2. Bash 5.0 发布及其新功能

    导读 邮件列表证实最近发布了 Bash-5.0.而且,令人兴奋的是它还有新的功能和变量.如果你一直在使用 Bash 4.4.XX,那么你一定会喜欢 Bash 的第五个主要版本. 第五个版本侧重于新的 ...

  3. Kafka 0.11版本新功能介绍 —— 空消费组延时rebalance

    在0.11之前的版本中,多个consumer实例加入到一个空消费组将导致多次的rebalance,这是由于每个consumer instance启动的时间不可控,很有可能超出coordinator确定 ...

  4. Android Studio 3.0 下载 使用新功能介绍

    谷歌2017发布会更新了挺多内容的,而且也发布了AndroidStudio3.0预览版,一些功能先睹为快.(英语一般,有些翻译不太好) 下载地址 https://developer.android.g ...

  5. 干货来袭:Redis5.0支持的新功能说明

    Redis5.0支持的新特性说明 本文内容来自华为云帮助中心 华为云DCS的Redis5.x版本继承了4.x版本的所有功能增强以及新的命令,同时还兼容开源Redis5.x版本的新增特性. Stream ...

  6. Redis4.0支持的新功能说明

    本文以华为云DCS for Redis版本为例,介绍Redis4.0的新功能.文章转载自华为云帮助中心. 与Redis3.x版本相比,DCS的Redis4.x以上版本,除了开源Redis增加的特性之外 ...

  7. React Native 0.50版本新功能简介

    React Native在2017年经历了众多版本的迭代,从接触的0.29版本开始,到前不久发布的0.52版本,React Native作为目前最受欢迎的移动跨平台方案.虽然,目前存在着很多的功能和性 ...

  8. Apache Kafka 0.11版本新功能简介

    Apache Kafka近日推出0.11版本.这是一个里程碑式的大版本,特别是Kafka从这个版本开始支持“exactly-once”语义(下称EOS, exactly-once semantics) ...

  9. 161128、Redis 4.0发布及其新功能介绍

    Redis 4.0-rc1 发布了,这是 4.0 的首个 RC 版.Redis 是一个高性能的key-value数据库.Redis 的出现,很大程度补偿了memcached这类keyvalue存储的不 ...

随机推荐

  1. platform_driver_register(),platform_device_register()区别

    设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. 以一个USB设备为例,有两种情形: (1)先插上USB设备并挂到总线中,然后在安装USB驱动程序过程中从总线上遍历各个设备,看驱 ...

  2. linux 防火墙iptables简明教程

    前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔 ...

  3. tlProPlayer for windows

    tlProPlayer tlProPlayer简介 tlProPlayer是一款定位高性能产品,支持透传,原生输出,并支持硬解码(硬件加速)的多媒体产品,兼容tlplayer所有特性.支持视频加密播放 ...

  4. string.length()与-1比较为什么会出现匪夷所思的结果

    今天调试程序发现了个匪夷所思的事情,-1与string.length()比较永远是-1大,看下面代码 #include<iostream> #include<string> u ...

  5. Git for windows 中文乱码解决方案

    1.git status时显示乱码,如下: \316\304\261\276\316\304\265\265.txt 解决方案: $ git config --global core.quotepat ...

  6. [58 Argo]58同城开源web框架Argo搭建实践

    无意间听说58开源的消息(Long long ago),我辈欣喜异常. 一方面感谢开源同仁的辛苦劳动,另一方面也为我辈在互联网技术实践圈外的人提供了一条实践的渠道. 我迫不及待的从github上dow ...

  7. Python模块整理(三):子进程模块subprocess

    文章 原始出处 http://ipseek.blog.51cto.com/1041109/807513. 本来收集整理网络上相关资料后整理: 从python2.4版本开始,可以用subprocess这 ...

  8. hdu 4920 Matrix multiplication (矩阵计算)

    题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. #include <iostr ...

  9. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...

  10. bzoj1054: [HAOI2008]移动玩具

    hash+bfs:要注意特殊情况.(似乎连sort.lower_bound都不用数据小直接判重了... #include<cstdio> #include<cstring> # ...