简介
当听到样式表这个词时,您可能会想到 CSS 样式表。XSLT 样式表通常用于 XML 转换,比如在 Web 服务之间映射数据。因为 XSLT 非常适合此用途,所以创建了顶层元素 <stylesheet> 的 <xsl:transform> 别名,虽然这很少使用。这种 XSLT 转换的输入结构与输出结构有很大的不同。最重要的是,命名空间的不同。
XSLT 样式表的输入结构与输出结构相似,但却更简单些。其中已经扩充了一些标记,但大部分标记只是原样复制到输出。输入和输出的命名空间是相同的 (HTML)。输入文档也可以包含样式表指令(比如创建脚注),这些指令属于另一个命名空间,不会传递到输出中。
常用缩略语
CSS:级联样式表
XHTML:可扩展超文本标记语言
XPath:XML 路径语言
XSLT:可扩展样式表语言转换
在本文中,我们将学习如何使用 XSLT 样式表扩充 XHTML 文档。文中的示例展示了如何使用指令,如何引用其他源文档的部分,以及如何使用链接在主文档中导航。此外,我们还探索了页面的解释和编译之间的区别。
CSS 样式表的局限性
XSLT 样式表不会阻止您使用其他技术,比如 JavaScript 或 CSS。CSS 适用于字体、加粗、颜色、间距等。它不适合将来自不同位置的信息组合在一起,比如脚注、模块或生成一个目录。这正是 XSLT 的用武之地,它补充而不是替代了 CSS。
XSLT 用途示例
实际上,您可以将 XSLT 代码集中在一个文件中。为了简单起见,本文中的每个示例均位于一个独立的 XSLT 文件中,除了一些必要的代码。清单 1 给出了必需的代码。
清单 1. 必需的代码(位于 samples/common.xml 中)
XML/HTML Code复制内容到剪贴板
- <s:stylesheet
- <span style="width: auto; height: auto; float: none;" id="24_nwp"><a style="text-decoration: none;" mpid="24" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="24_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://www.w3.org/1999/xhtml"
- xmlns:a="http://sourceforge.net/projects/arbalo/"
- xmlns:s="http://www.w3.org/1999/XSL/Transform"
- exclude-result-prefixes="a h"
- version="1.0"
- >
- <s:template match="h:<span style="width: auto; height: auto; float: none;" id="25_nwp"><a style="text-decoration: none;" mpid="25" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="25_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>">
- <s:copy>
- <s:apply-templates select="@*|node()"/>
- <meta
- http-equiv="content-type"
- content="text/html;charset=UTF-8" />
- <link
- href="common.<span style="width: auto; height: auto; float: none;" id="26_nwp"><a style="text-decoration: none;" mpid="26" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=css&k0=css&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="26_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">css</span></a></span>"
- rel="stylesheet"
- type="text/css" />
- </s:copy>
- </s:template>
-
- <s:template match="*">
- <s:copy>
- <s:copy-of select="@*"/>
- <s:apply-templates/>
- </s:copy>
- </s:template>
- </s:stylesheet>
XHTML 的命名空间定义了两次:默认定义和 h:。默认命名空间用于编写输出 XHTML 标记,其中应该避免使用命名空间前缀。h: 用在 XPath 表达式中。
本文使用 XSLT 1.0 版本。目前,大部分浏览器都无法解释 XSLT 2.0。但是,如果 XSLT 运行在服务器上,那么它可能是一个实用的选择。XSLT 2.0 还提供了:
XPATH 2.0(if…then…else 和许多内置的函数)
内置和用户编写的 XPATH 函数
分组
在 清单 1 中:
s:template match="head" 扩充了源文档的 head 一节,添加了一个 CSS 样式表的链接。即使 UTF-8 是在 XML 中的默认编码,一些浏览器也需要内容类型才能呈现它。
s:template match="*" 是默认的详细副本。原则上,所有内容都会复制到目标文档中。如果遗漏了此模板,只会将标记的文本内容复制到目标文档。不会复制处理指令节点。
本文中的所有其他示例都是导入 common.xsl 的独立文件。
扩充
通过扩充,添加了一个未在源文档中显式请求的特性。一个示例是 清单 1 中的 CSS 样式表的链接。尝试另一个示例,向每个内部链接添加一个小箭头 (^ v),指明目标在它之前还是之后。清单 2 给出了该样式表。
清单 2. 样式表(在 samples/linkUpDown.xsl 中)
XML/HTML Code复制内容到剪贴板
- <s:stylesheet
- <span style="width: auto; height: auto; float: none;" id="22_nwp"><a style="text-decoration: none;" mpid="22" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="22_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://www.w3.org/1999/xhtml"
- xmlns:s="http://www.w3.org/1999/XSL/Transform"
- version="1.0"
- >
- <s:import href="common.xsl"/>
- <s:template match="h:a[starts-with(@href,'#')]">
- <s:copy>
- <s:copy-of select="@*"/>
- <s:variable name="name" select="substring-after(@href,'#')"/>
- <s:choose>
- <s:when test="preceding::h:a[@name=$name]">
- <s:text>^</s:text>
- </s:when>
- <s:when test="following::h:a[@name=$name]">
- <s:text>v</s:text>
- </s:when>
- </s:choose>
- <s:apply-templates/>
- </s:copy>
- </s:template>
- </s:stylesheet>
首先,导入清单 2 中的通用样式表。模板与内部链接(以 '#' 开头)相匹配。如果链接指向的锚点位于链接之前,那么使用一个向上箭头扩充该链接(如果情况相反,则使用向下箭头)。
s:copy-of 和 s:apply-templates 可确保不会沿途丢下任何内容。
清单 3 给出了一个示例文档(其中包含内部链接),它经过了清单 2 中的样式表进行扩充。
清单 3. 源文档(在 samples/linkUpDown.xml 中)
XML/HTML Code复制内容到剪贴板
- <?<span style="width: auto; height: auto; float: none;" id="20_nwp"><a style="text-decoration: none;" mpid="20" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="20_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>-stylesheet href="linkUpDown.xsl" type="text/xsl"?>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <<span style="width: auto; height: auto; float: none;" id="21_nwp"><a style="text-decoration: none;" mpid="21" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="21_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>/>
- <body>
- <a name="a"/>
- <p>This link goes <a href="vb">downward.</a></p>
- <br/>
- <p>Reduce the size of the window to verify the link really works.</p>
- <br/>
- <a name="b"/>
- <p>This link goes <a href="^a">upward.</a>
- </p>
- </body>
- </html>
目标文档看起来相同,除了清单 4 中的条目。
清单 4. 目标文档(在 samples/linkUpDown.html 中)
XML/HTML Code复制内容到剪贴板
- … <a href="#b">v downwards.</a> …
- … <a href="#a">^ upwards.</a> …
指令
您可以在源文档中添加一些指令,告诉样式表执行何种操作。它们属于另一个命名空间(在本例中为前缀 a:),不会被复制到目标文档。
在清单 5 中,源文档中任何地方的指令标记 a:ref 都会创建一个脚注。
清单 5. 样式表(在 samples/footnote.xsl 中)
XML/HTML Code复制内容到剪贴板
- <s:stylesheet
- <span style="width: auto; height: auto; float: none;" id="19_nwp"><a style="text-decoration: none;" mpid="19" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="19_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"
- xmlns:a="http://sourceforge.net/projects/arbalo/"
- xmlns:h="http://www.w3.org/1999/xhtml"
- xmlns:s="http://www.w3.org/1999/XSL/Transform"
- version="1.0"
- >
- <s:import href="common.xsl"/>
- <s:template match="h:body">
- <s:copy>
- <s:apply-templates select="@*|node()"/
- <!-- put the footnotes at the end
- if there is no a:references directive -->
- <s:if test="not(descendant::a:references)">
- <s:call-template name="references"/>
- </s:if>
- </s:copy>
- </s:template>
-
- <s:template match="a:ref">
- <s:variable
- name="number"
- select="count(preceding::a:ref) + 1"/>
- <a name="ref-{$number}"></a>
- <a class="footnote" href="#reference-{$number}">
- <s:value-of select="concat('v ',$number)"/>
- </a>
- </s:template>
-
- <s:template match="a:references" name="references">
- <hr/>
- <s:for-each select="//a:ref">
- <s:variable name="number" select="count(preceding::a:ref) + 1"/>
- <p>
- <a name="reference-{$number}"></a>
- <a class="footnote" href="#ref-{$number}">
- <s:value-of select="concat(' ^',$number)"/>
- </a>
- <s:apply-templates/>
- </p>
- </s:for-each>
- </s:template>
- </s:stylesheet>
使用源文档中的 a:references 指令,名为 references 的模板会在模板与该指令匹配的地方分配脚注。如果缺少这样一个指令,第一个与 body 匹配的模板会在 body 的末尾分配脚注,方法是调用名为 references 的相同模板。在两种情况下,都会列出脚注的内容,并生成一个由向上箭头表示的向上链接。
第二个模板(匹配 a:ref)使用向下箭头创建脚注的链接。脚注具有编号。这里忽略了它的内容。
class="footnote" 属性在 XSLT 转换之后由一个 CSS 样式表解析,该样式表链接在 XSLT 样式表 common.xsl 中。
清单 6 中的源文档使用 a:ref 指令创建脚注。
清单 6. 源文档(在 samples/footnote.xml 中)
XML/HTML Code复制内容到剪贴板
- <?<span style="width: auto; height: auto; float: none;" id="17_nwp"><a style="text-decoration: none;" mpid="17" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="17_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>-stylesheet href="footnote.xsl" type="text/xsl"?>
- <html
- xmlns="http://www.w3.org/1999/xhtml"
- xmlns:a="http://sourceforge.net/projects/arbalo/"
- >
- <<span style="width: auto; height: auto; float: none;" id="18_nwp"><a style="text-decoration: none;" mpid="18" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="18_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span>/>
- <body>
- <p>
- This example looks a little scientific
- <a:ref>
- From Latin
- <em>scientia</em>
- </a:ref>
- and academic
- <a:ref>From Greek akademia</a:ref>.
- </p>
- <p>
- Do you know why?
- <a:ref>
- It uses
- <em>footnotes</em>.
- </a:ref>
- </p>
- <p>Reduce size of window to verify links are generated.</p>
-
-
- </body>
- </html>
目标文档将脚注列表包含在底部,如清单 7 所示。
清单 7. 目标文档(在 samples/footnote.html 中)
XML/HTML Code复制内容到剪贴板
- <html
- <span style="width: auto; height: auto; float: none;" id="14_nwp"><a style="text-decoration: none;" mpid="14" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=xml&k0=xml&kdi0=0&luki=10&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=70ab19d85545d1&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F1117%2Ehtml&urlid=0" id="14_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>ns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://www.w3.org/1999/xhtml"
- xmlns:a="http://sourceforge.net/projects/arbalo/">
- <<span style="width: auto; height: auto; float: none;" id="15_nwp"><a style="text-decoration: none;" mpid="15" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=d14555d819ab7000&k=head&k0=head&kdi0=0&luki=9&n=1
CSS配合JavaScript做酷的动态页面效果
利用CSS配合JavaScript的可以做很多更酷的动态页面效果,在本教程的最后给大家简单介绍一下CSS配合JS的应用。首先,要搞清楚事件和动作的概念。在客户端脚本中,JavaScript 通过对事件进行响应来获得与用户的交互。例如,当用户单击一个按钮或者在某段文字上移动鼠标
收藏 0赞 0分享
WEB标准,Web前端开发工程师必备技术列表
想要打造并拥有一流的Web产品开发团队,在团队成员基础能力上一定要下功夫。对于Web前端产品开发来说,仅仅掌握Web1.0时代简单的"网页套接"是完全不够的。我结合自己的团队配备,特此罗列了Web前端产品工程师所涉及的技能列表如下:
通过许多实际项目,
收藏 0赞 0分享
用CSS制作Alpha滤镜测试板
alpha滤镜给制作网页特效提供了较大的创作空间,但由于它控制参数较多,在实际应用时,为了确定一组合适的参数值,不得不反复调整修改,在编辑窗口和预览窗口来回倒腾,甚是麻烦,本文介绍了一种简单的方法。制作一个“Alpha滤镜参数测试板”,在测试板上输入参数
收藏 0赞 0分享
非常流行的所谓的气泡窗口
普通的Alt无法自定义风格,而Sweet Titles通过JS脚本与CSS的集合.自定义了这种伪Alt风格.
前一段时间非常流行的,就所谓的气泡窗口(鼠标移到链接处出现的).
我们这里实现的用的是Sweet Titles的插件.显示效果完全由CSS控制..
先下载Sweet Ti
收藏 0赞 0分享
CSS教程:li和ul标签用法举例
LI代码的格式化:
A).运用CSS格式化列表符: ul li{
list-style-type:none;
}
B).如果你想将列表符换成图像,则: ul li{
list-style-type:none;
list-style-image: url(/blog/images/
收藏 0赞 0分享
CSS教程:CSS中的定位(position)
使用CSS来定位页面内层的位置,一直是比较难以掌握的事情,很多时候,往往被绝对定位的元素,总是以浏览器的左上角为坐标原点,此时,如果浏览器的大小改变,被定义的层就会偏离设计想要的位置,让人很挠头。
其实,要想控制好层的绝对定位,只要理解CSS中关于定位
收藏 0赞 0分享
CSS教程:盒模型(BOX Model)
如果想熟练掌握DIV和CSS的布局方法,首先要对盒模型有足够的了解。每个HTML元素都可以看作一个装了东西的盒子,盒子里面的内容到盒子的边框之间的距离即填充(padding),盒子本身有边框(border),而盒子边框外和其他盒子之间,还有边界(margin),如图1所示。
收藏 0赞 0分享
无延迟翻滚的图形与CSS混合风格按钮
在一个具有图形背景的按钮中添加CSS风格的文本,这种建立按钮的方法结合了具有CSS翻滚(CSS rollover)标记的开发速度和效率,从而有效地提高按钮外表图像的三维效果。
相比于常规的图形按钮,这些图形/CSS混合按钮可易于建立和载入,因为你只需要为空白按钮外面
收藏 0赞 0分享
css里expression实现界面对象的批量控制
用过css样式我们就知道, 可以定义一批对象的class属性来指定同一个样式来统一界面. 但如何统一同类型的对象的事件? 比如:界面有无数个 <img src="**.jpg"> 如何实现鼠标经过此图片, 图片的src变成是**_over.jpg?
收藏 0赞 0分享
CSS教程:水平对齐(text-align)
水平对齐(text-align),用以设定元素内文本的水平对齐方式。
1.语法
text-align具体参数如下:
语法:text-align:left|right|center|justify
说明:设定元素内文本的水平对齐方式。
参数:left:左
收藏 0赞 0分享
查看更多