XSLT详解笔记

首先介绍XSL:拓展样式表语言

        CSS = HTML 的样式表语言

        通过CSS可以向HTML元素添加样式,告知浏览器用特定字体或颜色显示一个元素。

        XSL = XML的样式表语言

        可用于描述如何显示XML文档。

XSL不仅是样式表语言,他包含三个部分:

  • XSLT—— 一种用于转换XML文档的语言
  • Xpath—— 一种用于在XML文档中导航的语言
  • XSL-FO—— 一种用于格式化XML文档的语言

 XSLT简介:

  • 是指XSL转换(XSL Transformation)
  • 可完成XML,XHTML之间的转换,把XML源树转换为结果树

样式表声明:

将文档声明为XSL样式表的根元素为:< xsl:stylesheet >或< xsl:transform >。两者同义

根据 W3C 的 XSLT 标准,声明 XSL 样式表的正确方法是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

其中

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

指向了官方的W3C XSLT命名空间,必须指定命名空间才可以访问XSLT的元素,属性及特性。

XSLT元素

<xsl:template>元素

XSLT模板有两种:Rule Template 和 Named Template

Rule Template

定义语法如下:

<xsl:template match="xxx" mode="xx">
......template content
</xsl:template>

是定义的模板处理对象必须是指定的XML节点,即必须有match属性,通过match属性来指定该模板可以处理哪些节点。当match="/"代表处理对象是整个XML文档。

mode属性:可选属性,当需要对同一个节点定义不同的规则模板时,指定该模板在该节点中的mode值,apply-templates时也需要指定相应的mode属性值。

Named Template:具名模板

定义语法如下:

<xsl:template name="xxx">
.....template content
</xsl:template>

用name属性对template定义,然后可以反复被call-template调用,可以理解为定义了一个函数

<xsl:apply-templates>元素

  • 总是包含在<xsl:template>中
  • 当有用select属性进行指定时,定义了template匹配的元素及其子元素中,哪些元素将被输出。
  • 当没有select属性进行指定时,则输出该template匹配到的元素及其所有子元素。
  • 可理解为在该模板(函数)中运用了子函数。与上下文节点有关系

用例1:用<h1>元素包围文档中的每个<title>元素

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="message">
  <h1><xsl:apply-templates select="title"/></h1>
</xsl:template>

</xsl:stylesheet>

用例2:用<h1>元素包围文档中mode属性设置为big的message的所有子节点:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="message">
  <h1><xsl:apply-templates select="*" mode="big"/></h1>
</xsl:template>

</xsl:stylesheet>

<xsl:call-template name="xxx">元素

  • 调用一个已经定义好的模板(函数)
  • 如果定义具名模板时设置了参数(param),则需要用一下语法给模板传递参数的值:
  • <xsl:with-param> 标记的语法:
    <xsl:with-param name="name" select="expression">
      <!-- Content -->
    </xsl:with-param>
    

    其中name是必须的,用于指定哪个参数;select是可选属性,用于指定该参数的默认值。

<xsl:value-of>元素

< xsl:value-of > 元素用于提取某个选定节点的值,并把值添加到转换的输出流中

使用语法:

<xsl:value-of
select="expression"
disable-output-escaping="yes|no"/>

select属性是必选,用于指定提取哪个节点的值。

disable-output-escaping可选,决定内容是否转义,“yes”则不进行转义,“No”则需要转义,默认为“No”

<xsl:attribute>元素

用于向元素添加属性。当元素内有同名属性时会被替换掉。

语法:

<xsl:attribute name="attributename" namespace="uri">
  <!-- value of the attribute -->
</xsl:attribute>

name:必须属性,指定要添加属性的名称

namespace:可选,为要添加的属性定义命名空间的URI。

**关于URI的补充:

URL (Uniform Resource Locator)表示资源的位置,期望提供查找资源的方法。

URN (Uniform Resource Name)为资源提供持久的,位置无关的标记方式,例如磁力链接。

URI(Uniform Reource Identifier),主要用于区分资源,包含了URL和URN的概念。换句话说,URI可以是URL或URN,但URL/URN不一定是URI。URI 注重唯一标识(Identifier),URL注重的是位置(Location)。一个资源可以有多个对应的URI,但一个URI只能映射到唯一的关联资源。

用例1:为picture元素添加了一个名为source的属性,值为空

<picture>
  <xsl:attribute name="source"/>
</picture>

用例2:向picture元素添加source属性,并且使用"images/name"中的值为其赋值:

<picture>
  <xsl:attribute name="source">
    <xsl:value-of select="images/name" />
  </xsl:attribute>
</picture>

<xsl:attribute-set>元素

 用于创建属性集,该属性集可以作为整体应用到输出文档。

* *必须是<xsl:stylesheet>或<xsl:transform>的子节点。

语法:

<xsl:attribute-set
name="name" use-attribute-sets="name-list">
  <!-- Content:xsl:attribute* -->
</xsl:attribute-set>

name:必须属性,用来指定该属性集的名称。

use-attribute-sets:可选,如需在这属性集中使用其他属性集,则在这里指定被引属性集。

用例1:创建font属性集中的属性,以及fname,size,color这三个属性。

<xsl:attribute-set name="font" use-attribute-sets="font">
  <xsl:attribute name="fname">Arial</xsl:attribute>
  <xsl:attribute name="size">14px</xsl:attribute>
  <xsl:attribute name="color">red</xsl:attribute>
</xsl:attribute-set>

<xsl:element>元素

用于在输出文档中创建元素节点

定义语法:

<xsl:element
name="name"
namespace="URI"
use-attribute-sets="namelist">
  <!-- Content:template -->
</xsl:element>

name:必需属性,规定要创建的元素的名称。

namespace:可选,规定元素的命名空间的URI。

use-attribute-sets:可选,可用于对元素运用指定的属性集今天添加属性。

用例1:在每个"catalog/cd"节点下,添加一个"singer"元素,值与该cd节点下"artist"元素的值相同

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:for-each select="catalog/cd">
    <xsl:element name="singer">
      <xsl:value-of select="artist" />       “取artist元素的值,为singer元素赋值”
    </xsl:element>
    <br />
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

<xsl:copy> 元素

When xsl:copy is applied to an element node, the start and end element tags are copied; the attributes, character content and child elements are copied only if xsl:apply-templates is used within xsl:copy.

定义语法:

<xsl:copy use-attribute-sets="name-list">
  <!-- Content:template -->
</xsl:copy>

 use-attribute-sets:可选属性,如果对象节点是元素,则该属性集列表就将被运用到输出节点中

用例1:Copy the input element to the output, together with all its child elements,character content and attributes. 

<xsl:template match="*|text()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

用例2:把message节点都拷贝到输出文档

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="message">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

<xsl:copy-of>元素

This function is used exclusively if the current node is to be copied excluding all the child nodes and the attributes defined in the document. While displaying an expression it doesn’t convert them to string in all the situations

The properties of copy-of depend on the type of data type the path expression returns.

  1. If the final result is of the data type Boolean, string, or numeric the XSL:copy-of outputs them in a text node. Therefore, it behaves the same as for xsl:value-of.

  2. Next, if the expression statement evaluates to a node-set then xsl: copy-of copies all nodes in the order of the tree along with their descendant in the final document.

  3. If it is a fragment tree then it leads to an unchanged output document.

定义语法

<xsl:copy-of select="expression"/>

 select:必需属性,选定要拷贝的对象(内容)

用例1:

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="copy.xsl"?>

<Types>
    <binary>zero one</binary>
    <string>data</string>
    <number>5.14</number>
    <list-set>
        <id>11</id>
        <id>12</id>
        <id>13</id>
    </list-set>
    <basetype>
        content
        <first>
            text
        <second>
            text
        <sub-child />
        <sub-child />
        </second>
        <sub-child />
        </first>
    </basetype>
</Types>

 XSL sheet:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <xsl:variable                           **定义一个binary变量并判断是否等于zero
        name="binary"
        select="Types/binary='zero'"/>

    <xsl:variable                        
        name="string"
        select="string(values/string)"/>

    <xsl:variable
        name="number"
        select="number(Types/number)"/>

    <xsl:variable
        name="list-set"               **定义一个list-set变量,并将"list-set"片段赋值给它
        select="Types/list-set/*"/>

    <xsl:variable name="basetype">
        <xsl:copy-of select="values/basetype/*" />
    </xsl:variable>

<xsl:text>Value-of binary:</xsl:text>、
<xsl:value-of select="$binary" />        **取变量"Binary"的值
<xsl:text>Copy-of binary:</xsl:text>
<xsl:copy-of select="$binary" />

<xsl:text>Value-of string:</xsl:text>
<xsl:value-of select="$string" />
<xsl:text>Copy-of string:</xsl:text>
<xsl:copy-of select="$string" />

<xsl:text>Value-of number:</xsl:text>
<xsl:value-of select="$number" />
<xsl:text>Copy-of number:</xsl:text>
<xsl:copy-of select="$number" />

<xsl:text>Value-of list-set:</xsl:text>
<xsl:value-of select="$list-set" />
<xsl:text>Copy-of list-set:</xsl:text>
<xsl:copy-of select="$list-set" />

<xsl:text>Value-of basetype:</xsl:text>
<xsl:value-of select="$basetype" />
<xsl:text>Copy-of basetype:</xsl:text>
<xsl:copy-of select="$basetype" />

</xsl:template>
</xsl:stylesheet>

处理结果:

 区别xsl:copy 与xsl:copy-of :

--当只想复制上下文节点(context item,通常与当前节点是相同的),且不考虑其子项时,用xsl:copy

--当要以递归方式复制@select属性通过xpath选定的节点下的完整节点树

**补充:区分上下文节点与当前节点:

当前节点是模板当前正在操作的任何节点,通常情况下这恰好也是上下文节点。但是在嵌套XPath表达式(方括号中的内容)中,上下文节点指的是正在测试是否匹配的任何节点,因此在XPath表达式中上下文节点会更改,但当前节点不会更改。上下文节点可以缩写为(.)

在XPath中由于(.)代表了上下文节点,因此要想在XPath中引用回当前节点,可以用current()函数。

<xsl:apply-templates select="//cd[@title=current()/@ref]"/>

上面这行,将会处理title属性的值等于当前节点的ref属性的值的所有cd元素。 

<xsl:apply-templates select="//cd[@title=./@ref]"/>

这一行,则会处理title属性和ref属性具有相同值的所有cd元素

<xsl:message>元素

可输出一条指定内容的消息,该元素主要用于报错

定义语法:

<xsl:message terminate="yes|no">

  <!-- Message content -->

</xsl:message>

 terminate:可选属性,用于决定抛出消息后时候终止处理。

用例1:检测artist是否是空字符串,如果是,则显示一条消息并退出XSL处理器。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <xsl:for-each select="catalog/cd">
    <p>Title: <xsl:value-of select="title"/><br />
    Artist:
    <xsl:if test="artist=''">
      <xsl:message terminate="yes">
        Error: Artist is an empty string!
      </xsl:message>
    </xsl:if>
    <xsl:value-of select="artist"/>
    </p>
  </xsl:for-each>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

<xsl:param>元素

用于声明局部或全局参数,当在具名模板内声明参数,则是局部参数;如果在顶层元素来声明(stylesheet的子节点),就是全局参数

As a template parameter, xsl:param must be used as an immediate child of the xsl:templateelement. As a stylesheet parameter, it must be used as an immediate child of the xsl:stylesheetelement.

定义语法:

<xsl:param
name="name"
select="default value of the parameter">

<!-- Content:template -->

</xsl:param>

name:必选参数,用于指定参数名称。

select:可选,用Xpath表达式设置参数默认值。

<xsl:variable>元素

用于声明局部或全局变量,如果在模板内声明,则是局部变量;如果是被声明为顶层元素(immediate child of stylesheet)则是全局变量。

*可以通过<xsl:variable>元素的内容或者通过select属性来向变量指定值。

*variable不同于param,variable一旦设置了值,就无法修改

用例1:分别用<xsl:variable>元素的内容以及select属性为变量赋值

<xsl:variable name="color" select="'red'" />        **用select属性为变量color赋值


**用variable元素的内容为变量赋值:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="header">
  <tr>
  <th>Element</th>
  <th>Description</th>
  </tr>
</xsl:variable>

<xsl:template match="/">
  <html>
  <body>
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="reference/record">
    <tr>
    <xsl:if category="XML">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  <br />
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="table/record">
    <tr>
    <xsl:if category="XSL">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

<xsl:import>元素

该元素是顶层元素,用于把一个样式表中的内容导入到另一个样式表中(只是单纯导入但未应用)相当于import了个包。

*该元素必须是<xsl:stylesheet>的第一个子节点(immediate child)

定义语法:

<xsl:import href="URI"/>

href:必需属性,用于指定要导入的样式表的URI。

用例1:向名为"cdacatalog_import.xsl"的样式表中导入名为"cdcatalog_ex3.xsl"的样式表

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="cdcatalog_ex3.xsl"/>

<xsl:template match="/">
  <xsl:apply-imports/>
</xsl:template>

</xsl:stylesheet>

 <xsl:apply-imports>元素

将样式表导入到当前主样式表后(<xsl:import>),可通过<xsl:apply-imports元素>应用来自导入样式表中的模板规则。

由于导入样式表中的模板规则优先级要低于主样式表中的模板规则,所以希望应用导入样式表中的模板规则,而不是主样式表中的某条等价规则,就需要用到该apply-import元素。

定义语法:

<xsl:apply-imports/>

无其他属性。

用例1:假设有一个standard.xsl的样式表,其中包含match对象是message元素的模板规则。在另一个样式表中导入"standard.xsl",并且修改message:

standard.xsl样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="message">
  <h2><xsl:apply-templates/></h2>
</xsl:template>

</xsl:stylesheet>

导入standard样式表并修改message:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="standard.xsl"/>

<xsl:template match="message">
  <div style="border:solid blue">
  <xsl:apply-imports/>
  </div>
</xsl:template>

</xsl:stylesheet>

结果:

<div style="border:solid blue"><h2>...</h2></div>

<xsl:include>元素

顶层元素,必须是<xsl:stylesheet>或<xsl:transform>的子节点。

区别与import,被包含的样式表与包含的样式表的优先级相同(可以理解为把内容贴进去包含样式表里了)

定义语法:

<xsl:include href="URI"/>

href:必须属性,用于指定被包含样式表的URI

用例1:在下述样式表中包含了名为xslincludefile.xsl的样式表:

<?xml version=1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:template match="/">
   <xsl:for-each select="COLLECTION/BOOK">
      <xsl:apply-templates select="TITLE"/>
      <xsl:apply-templates select="AUTHOR"/>
      <xsl:apply-templates select="PUBLISHER"/>
      <BR/>  <!-- add this -->
   </xsl:for-each>
</xsl:template>

<xsl:template match="TITLE">
  <DIV STYLE="color:blue">
    Title: <xsl:value-of select="."/>
  </DIV>
</xsl:template>

<xsl:include href="/xsl/xslincludefile.xsl" />

</xsl:stylesheet>

XSLT函数

参考:(68条消息) XSLT详解_yumo丶的博客-CSDN博客_xslt

更多信息,请参考:xsl:accept - XSLT 3.1 with examples (xsltdev.com)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值