XSLT教程

XML文档树

1) XML可以转化文档树

2) XSLT对XML的转化过程

内建模板规则

调用<xsl:apply-templates>处理根节点的儿子。处理时,使用调用时相同的模式

元素

调用<xsl:apply-templates>处理该节点的儿子。处理时,使用调用时相同的模式

属性

拷贝属性值到结果树,结果作为文本而不是属性节点

文本

拷贝文本到结果树

注释

不做任何事

处理指令

不做任何事

命名空间

不做任何事

XSLT 1.0支持的五种数据类型

number、boolean、string、node-set、tree

相互之间的转化

boolean

number

string

node-set

tree

boolean

/

false -> 0

true -> 1

false -> ‘false’

true -> ‘true’

不允许

不允许

number

0 -> false

非0-> true

/

转化为十进制数

不允许

不允许

string

null-> false

其他 -> true

解析为十进制

/

不允许

不允许

node-set

empty -> false

其他 -> true

通过string转化

文档顺序中第一个节点的字符串值

/

不允许

tree

通过string转化

通过string转化

连接所有的文本节点

不允许

/

说明:

Tree很少见,只有在xsl:variable中才出现。xslt2.0已经取消该类型。另外,转化为node-set,标准xslt1.0不提供,但是扩展接口通常都是提供的。比如微软提供了msxsl:node-set。

XPath

基本概念

绝对路径的写法

上下文节点(context node)

即XPath中直接包含的,和当前节点等同。上下文节点可以通过“.”得到。

position()可以得到上下文节点的当前值。

last()可以得到上下文节点的大小

可以修改上下文节点的xslt标签

xsl:apply-templates xsl:for-each

xsl:for-each一个常用的特色就是修改context node

Axes

参见P282

Axes

Description

ancestor::

The ancestors of the context node.

The ancestors of the context node consist of the parent of the context node and the parent's parent and so on; thus the ancestor:: axis always includes the root node unless the context node is the root node.

ancestor-or-self::

The context node and its ancestors.

The ancestor-or-self:: axis always includes the root node.

attribute::

The attributes of the context node.

This axis will be empty unless the context node is an element.

child::

The children of the context node.

A child is any node immediately below the context node in the tree. However, neither attribute or namespace nodes are considered children of the context node.

descendant::

The descendants of the context node.

A descendant is a child or a child of a child and so on; thus the descendant:: axis never contains attribute or namespace nodes.

descendant-or-self::

The context node and its descendants.

following::

All nodes that are after the context node in the tree, excluding any descendants, attribute nodes, and namespace nodes.

following-sibling::

All the following siblings of the context node.

The following-sibling:: axis identifies just those children of a parent node who appear in the tree after the context node. This axis excludes all other children that appear before the context node.

If the context node is an attribute node or namespace node, the following-sibling:: axis is empty.

namespace::

The namespace nodes of the context node.

There is a namespace node for every namespace which is in scope for the context node.

This axis will be empty unless the context node is an element.

parent::

The parent of the context node, if there is one.

The parent is the node immediately above the context node in the tree.

preceding::

All nodes that are before the context node in the tree, excluding any ancestors, attribute nodes, and namespace nodes.

One way to think of the preceding axis is all nodes whose content occurs in their entirety before the start of the context node.

preceding-sibling::

All the preceding siblings of the context node.

The preceding-sibling:: axis identifies just those children of a parent node who appear in the tree before the context node. This axis excludes all other children that appear after the context node.

If the context node is an attribute node or namespace node, the preceding-sibling:: axis is empty.

self::

Just the context node itself.

几种简写方式

self::

.

attribute::

@

parent::

..

child::

 

/descendant-or-self::node()

//

Filter

[] 里面的值是boolean类型,如果不是(数值除外)就会发生强制类型转化成boolean。

数值的特性

其他

NameTest *

Union |

XPath部分函数

Node-Set函数

count()、local-name()、name()、position()、last()

String函数

concat()、contains()、starts-with()、string()、string-length()、substring ()、substring-after()、substring-before()、translate()

<xsl:variable name="UpCaseHttp" select="translate($ItemValue, 'htp', 'HTP')"/>

Boolean函数

boolean()、false()、not()、true()

Number函数

ceiling()、floor()、number()、round()、sum()

XSLT标签

xsl:template xsl:call-template xsl:apply-templates xsl:param xsl:with-param

<xsl:template name="JSButton">

<xsl:param name="Name"/>

<xsl:param name="JS"/>

<img src="../images/empty.gif" width="5" height="1"/>

<span class="clsButtonFace">

<a onclick="javascript:{$JS};">

<xsl:value-of select="$Name"/>

</a>

</span>

</xsl:template>

<xsl:template name="ea:SaveNoNewButton">

<xsl:call-template name="JSButton">

<xsl:with-param name="Name" select="' 保存'"/>

<xsl:with-param name="JS" select="'verify(&quot;Save&quot;)'"/>

</xsl:call-template>

</xsl:template>

<xsl:apply-templates select="Ebanswers/PAGE">

<xsl:template match="PAGE">

</xsl:template>

xsl:foreach

<xsl:for-each select="$CodeTable">

<option value="{CODE_VALUE}">

<xsl:if test="CODE_VALUE = $AValue">

<xsl:attribute name="selected"/>

</xsl:if>

<xsl:value-of select="CODE_NAME"/>

</option>

</xsl:for-each>

xsl:sort

<xsl:sort

select = string-Expression

lang = { nmtoken }

data-type = { "text" | "number" | QName }

order = { "ascending" | "descending" }

case-order = { "upper-first" | "lower-first" }

/>

紧随xsl:apply-templates或xsl:for-each

<xsl:apply-templates select="/Toolkit/tk:Table/tk:Field[tk:List]">

<xsl:sort select="tk:List/@Order" data-type="number"/>

</xsl:apply-templates>

xsl:variable

注意字符串变量

<xsl:variable name="Test" select="'title'"/>

xsl:value-of xsl:text

<xsl:value-of

select = Expression

disable-output-escaping = "yes" | "no"

</xsl:value-of>

<xsl:text

disable-output-escaping = "yes" | "no">

</xsl:text>

<xsl:template name="ea:WhiteSpace">

<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

</xsl:template>

xsl:element xsl:attribute xsl:attribute-set

XML File (item.xml)

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="element.xsl" ?>

<root>

<item>My Item</item>

</root>

XSLT File (element.xsl)

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="item">

<xsl:element name="xsl:template">

<xsl:attribute name="match">cost</xsl:attribute>

<xsl:attribute name="xml:space">preserve</xsl:attribute>

<xsl:apply-templates/>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

My Item

The following is the processor output, with line breaks added for clarity.

<?xml version="1.0"?>

<xsl:template match="cost"

xml:space="preserve"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My Item</xsl:template>

<xsl:attribute-set name="ea:WrapAttrs">

<xsl:attribute name="nowrap"/>

</xsl:attribute-set>

<!--不能为空的字段的显示名称的td的attribute-set-->

<xsl:attribute-set name="ea:NonEmptyAttrs" use-attribute-sets="ea:WrapAttrs">

<xsl:attribute name="align">right</xsl:attribute>

<xsl:attribute name="bgcolor">#E2EBF6</xsl:attribute>

</xsl:attribute-set>

xsl:if xsl:choose xsl:when xsl:otherwise

<a href="{$HttpRef}">

<xsl:if test="$Target != ''">

<xsl:attribute name="target"><xsl:value-of select="$Target"/></xsl:attribute>

</xsl:if>

<xsl:value-of select="$ItemValue"/>

</a>

<xsl:choose>

<xsl:when test="$BigHrefType='HREF'">

<a href="{$HrefAddress}">

<xsl:value-of select="$Value"/>

</a>

</xsl:when>

<xsl:when test="$BigHrefType='EMAIL'">

<a href="mailto:{$Value}">

<xsl:value-of select="$Value"/>

</a>

</xsl:when>

<xsl:otherwise>

<xsl:call-template name="ea:ProcessText">

<xsl:with-param name="Text" select="$Value"/>

</xsl:call-template>

</xsl:otherwise>

</xsl:choose>

xsl:import xsl:include

<xsl:import href="PublicUtil.xslt"/>

xsl:copy-of xsl:output

<xsl:output method="html" indent="no"/>

<xsl:variable name="ea:NoRecord">

<tr>

<td colspan="20" class="pad5">没有内容</td>

</tr>

</xsl:variable>

<xsl:copy-of select="$ea:NoRecord"/>

XSLT函数

current()

通常情况下,和“.”返回的值相同。只有在filter情况下,才不同。

<xsl:apply-templates select="//glossary/item[@name=current()/@ref]"/

document()

<xsl:variable name="ea:FieldList" select="document('')/xsl:stylesheet/ea:FieldList"/>

format-number()

format-number(53.51, "#.0000") // "53.5100"

其他

递归:

<xsl:template name="ea:ProcessText">

<xsl:param name="Text"/>

<xsl:choose>

<xsl:when test="contains($Text,' ')">

<xsl:value-of select="substring-before($Text,' ')"/>

<br/>

<xsl:call-template name="ea:ProcessText">

<xsl:with-param name="Text" select="substring-after($Text,' ')"/>

</xsl:call-template>

</xsl:when>

<xsl:otherwise>

<p style="word-break:break-all">

<xsl:value-of select="$Text"/>

</p>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

<xsl:template name="ShowRow">

<xsl:param name="CurIndex" select="0"/>

<xsl:param name="Data"/>

<xsl:param name="CurRow"/>

<xsl:if test="$CurIndex=0">

<td bgcolor="#6D8CAB">

<a href="{concat($WeekLink, 'Date=', $Data/Date)}">

<img src="{concat('../images/link_', $CurRow + 1, '.gif')}" width="18" height="40" border="0" alt="周历"/>

</a>

</td>

</xsl:if>

<xsl:if test="$CurIndex &lt; 7">

<xsl:call-template name="ShowData">

<xsl:with-param name="Data" select="$Data"/>

</xsl:call-template>

<xsl:for-each select="$Data">

<xsl:call-template name="ShowRow">

<xsl:with-param name="CurIndex" select="$CurIndex + 1"/>

<xsl:with-param name="Data" select="following-sibling::DateDescription[1]"/>

</xsl:call-template>

</xsl:for-each>

</xsl:if>

</xsl:template>

优先级问题

<xsl:import>存在优先级问题

模糊匹配的,例如*,优先级比较低

属性值模板

<a href="{concat($WeekLink, 'Date=', $Data/Date)}">

XSLT教程 比较全的 -摘自网络的更多相关文章

  1. 最全面的Android Studio使用教程【申明:来源于网络】

    最全面的Android Studio使用教程[申明:来源于网络] http://www.admin10000.com/document/5496.html

  2. Ubuntu的常用快捷键(摘自网络)

    篇一 : Ubuntu的复制粘贴操作及常用快捷键(摘自网络) Ubuntu的复制粘贴操作 1.最为简单,最为常用的应该是鼠标右键操作了,可以选中文件,字符等,右键鼠标,复制,到目的地右键鼠标,粘贴就结 ...

  3. NODE-WEBKIT教程(12)全屏

    node-webkit教程(12)全屏 文/玄魂 目录 node-webkit教程(12)全屏 前言 12.1  和全屏有关的三个api Window.enterFullscreen() Window ...

  4. Android:控件WebView显示网页 -摘自网络

    WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...

  5. C# 中的内存管理,摘自网络

    C#编程的一个优点是程序员不需要关心具体的内存管理,尤其是垃圾收集器会处理所有的内存清理工作.虽然不必手工管理内存,但如果要编写高质量的代码,还是要理解后台发生的事情,理解C#的内存管理.本文主要介绍 ...

  6. Android新手系列教程(申明:来源于网络)

    Android新手系列教程(申明:来源于网络) 地址:http://blog.csdn.net/column/details/androidcoder666.html

  7. PyTorch全连接ReLU网络

    PyTorch全连接ReLU网络 1.PyTorch的核心是两个主要特征: 一个n维张量,类似于numpy,但可以在GPU上运行 搭建和训练神经网络时的自动微分/求导机制 本文将使用全连接的ReLU网 ...

  8. 利用XSD配合XSLT產出特定格式Word檔案 -摘自网络

    利用類別產生XSD檔 產出XSD檔的目的在於提供Word樣板設計之資料框架 在此使用微軟提供之XML Schema Definition Tool (Xsd.exe)工具產生XSD檔 1. 定義類別 ...

  9. scala笔记,主要摘自网络教程

    1.scala是一种纯面向对象的语言,每个值都是对象.对象的数据类型以及行为由类和特质描述 2.类的扩展有2种机制:继承和混入机制 3.scala是一种函数式语言,其函数也能当成值来使用 ==4.sc ...

随机推荐

  1. jquery 中的 $("#id") 与 document.getElementById("id") 的区别

    以前没注意过,认为jquery 中的 $("#air") 与 document.getElementById("air") 是一回事,指的是同一个东西.在今天写 ...

  2. ubuntu 在XP下硬盘安装

    以下选择在XP下用 grub4dos 安装 ubuntu 12.04版本 需要下载两个文件:一个是grub4dos,另一个是 ubutuntu 镜像文件 grub4dos下载地址:http://dow ...

  3. CSS禁止Chrome谷歌浏览器激活输入框后自动添加橘黄色边框

    Chrome默认会为所有的输入框加上橘黄色的边框,虽然有时候可以使我们的网站看起来更友好,但对自定义的样式是有影响的.当鼠标点击输入框时,在谷歌chrome浏览器中,光标移到输入框时激活输入框会被加上 ...

  4. 采用python获得并修改文件编码(原创)

    windows和linux采用了不同的编码,这让很多人伤透了脑经,这里我采用了Python的chardet库获得代码的编码,然后修改编码. 1.首先需要安装chardet库,有很多方式,我才用的是比较 ...

  5. 【javascript 变量和作用域】

    今天学习了javascript 的变量和作用域的基本知识,对于以前在开发中遇到的一些不懂的小问题也有了系统的认识,收获还是比较多的. [基本类型和引用类型] ECMAScript 变量可能包含两种不同 ...

  6. Python的简介以及安装和第一个程序以及用法

    Python的简介: 1.Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程.Pytho ...

  7. 编程书籍分享--pdf

    作为程序员,我觉得我们应该多学习.多思考.多分享. 今天就花费了一点时间把这几年搜集的编程资料上传到了网上做个分享, 其中涵盖.net .java.js.html5.css3.mysql.sqlser ...

  8. POJ 2635 The Embarrassed Cryptographer 大数模

    题目: http://poj.org/problem?id=2635 利用同余模定理大数拆分取模,但是耗时,需要转化为高进制,这样位数少,循环少,这里转化为1000进制的,如果转化为10000进制,需 ...

  9. bzoj3677: [Apio2014]连珠线

    Description 在列奥纳多·达·芬奇时期,有一个流行的童年游戏,叫做“连珠线”.不出所料,玩这个游戏只需要珠子和线,珠子从1到礼编号,线分为红色和蓝色.游戏 开始时,只有1个珠子,而接下来新的 ...

  10. Array 原型扩展(快速排序,搅乱顺序)

    /// 快速快速排序算法Array.prototype.quickSort = function (left, right) { // left = left || 0; // right = rig ...