xslt元素的使用
注意:对于本技巧,您可以使用任何XSLT处理器(例如Xalan或Saxon),或基于浏览器的解决方案(例如Microsoft Internet Explorer或Mozilla)。
基本文件
本技巧文章描述了一个样式表,该样式表将XML声明转换为HTML。 总体目标是提供默认模板,但如果需要,允许用户有足够的自由来更改样式表的某些方面。 其他方面不变。 样式表可与以下源文档一起使用:
清单1.源文档
<?xml version="1.0"?>
<?xml-stylesheet href="style.xsl" version="1.0"
type="text/xsl"?>
<announcement>
<headline>Contest Announcement</headline>
<description>
Do you love
<product>Pop's Homemade Mashed Potato Mix</product>?
Are you artistic? Well, here's your big big chance!
Loopy Foods, the company that brings you
<product>Pop's Homemade Mashed Potato Mix</product>
and <product>Aunt Susie's Squash in a Box</product>
is hosting a Mashed Potato Sculpting Contest. Send your
entry today. Contest rules are on the back of every box
of <product>Pop's Homemade Mashed Potato Mix</product>.
</description>
<copyright/>
</announcement>
目标是创建一个具有如下样式表HTML页面:
清单2.基本样式表
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title><xsl:value-of select="announcement/headline"/></title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="headline">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="description">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="product">
<b><xsl:apply-templates/></b>
</xsl:template>
</xsl:stylesheet>
结果是一个为每个公告创建一个段落的文档,并以粗体显示产品名称:
清单3.基本转换结果
<html>
<head><title>Contest Announcement</title></head>
<body>
<h1>Contest Announcement</h1>
<p>
Do you love <b>Pop's Homade Mashed Potato Mix</b>?
Are you artistic? Well, here's your big big chance! Loopy Foods, the
company that brings you <b>Pop's Homemade Mashed Potato Mix</b>
and <b>Aunt Susie's Squash in a Box</b> is hosting a Mashed Potato
Sculpting Contest. Send your entry today. Contest rules are on the
back of every box of <b>Pop's Homemade Mashed Potato Mix</b>.
</p>
</body>
</html>
创建模板样式表
使用这些模板,管理员可以构建基本的样式表,该样式表使用户可以使用默认样式,但在必要时允许他们使用自定义样式。 为此,您需要在第二个样式表中包括基本模板,在本例中为import.xsl:
清单4.导入的样式表(import.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="headline">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="description">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="product">
<b><xsl:apply-templates/></b>
</xsl:template>
</xsl:stylesheet>
从那里开始,创建一个导入主要样式的样式表即可。 进行此导入后,仅包含主模板的样式表仍会按预期运行,但是如果添加了其他模板,它将覆盖导入中的相应模板:
清单5.覆盖导入的模板
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="import.xsl"/>
<xsl:template match="/">
<html>
<head><title><xsl:value-of select="announcement/headline"/></title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<i><xsl:apply-templates/></i>
</xsl:template>
</xsl:stylesheet>
导入的样式表的优先级低于主样式表,因此将使用主样式表中的产品模板代替导入的产品模板:
清单6.覆盖的模板结果
<html>
<head><title>Contest Announcement</title></head>
<body>
<h1>Contest Announcement</h1>
<p>
Do you love <i>Pop's Homade Mashed Potato Mix</i>?
Are you artistic? Well, here's your big big chance! Loopy Foods, the
company that brings you <i>Pop's Homemade Mashed Potato Mix</i>
and <i>Aunt Susie's Squash in a Box</i> is hosting a Mashed Potato
Sculpting Contest. Send your entry today. Contest rules are on the
back of every box of <i>Pop's Homemade Mashed Potato Mix</i>.
</p>
</body>
</html>
即使覆盖了模板,仍然可以使用apply-imports
选项使用导入的模板:
清单7.应用导入
...
</html>
</xsl:template>
<xsl:template match="product">
<i><xsl:apply-imports/></i>
</xsl:template>
</xsl:stylesheet>
结果,原始模板和导入的模板均被执行:
清单8.使用覆盖的模板
<html>
<head><title>Contest Announcement</title></head>
<body>
<h1>Contest Announcement</h1>
<p>
Do you love <i><b>Pop's Homade Mashed Potato Mix</b></i>?
Are you artistic? Well, here's your big big chance! Loopy Foods, the
company that brings you <i><b>Pop's Homemade Mashed Potato Mix</b></i>
and <i><b>Aunt Susie's Squash in a Box</b></i> is hosting a Mashed Potato
Sculpting Contest. Send your entry today. Contest rules are on the
back of every box of <i><b>Pop's Homemade Mashed Potato Mix</b></i>.
</p>
</body>
</html>
导入和包含之间的区别
import元素必须始终是顶级元素,并且必须始终位于其他任何元素之前。 就优先级而言,此要求具有特定的结果。 在XSLT样式表中,最后处理的模板优先于之前的所有模板,因此始终会覆盖导入的模板。 导入的模板也成为导入树的一部分,因此可用于apply-imports
元素。
另一方面,还可以使用include元素来包含样式表,该样式表只是在将样式表包含的位置将其添加到主样式表中。 而且,尽管必须在顶层包含样式表,但可以随时添加样式表,因此可以轻松地将其添加到页面底部,从而覆盖用户可能添加的任何模板。
例如,您可以创建一个包含样式表来设置版权信息,称为include.xsl:
清单9.包含的样式表(include.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="copyright">
<p>Copyright 2003, Loopy Foods</p>
</xsl:template>
</xsl:stylesheet>
当此样式表包含在主样式表的末尾时,它将覆盖其他模板:
清单10.包括样式表
...
<xsl:template match="product">
<i><xsl:apply-imports/></i>
</xsl:template>
<xsl:template match="copyright">
<p><big>Copyright now, by ME</big></p>
</xsl:template>
<xsl:include href="include.xsl"/>
</xsl:stylesheet>
因此,即使主样式表包含copyright
模板,仍将使用包含的模板:
清单11.包含结果:
...
Sculpting Contest. Send your entry today. Contest rules are on the
back of every box of <i><b>Pop's Homemade Mashed Potato Mix</b></i>.
</p>
<p>Copyright 2003, Loopy Foods</p>
</body>
</html>
请记住,优先级是位置问题; 如果样式表( include.xsl
)已包含在主样式表的开头而不是底部,则将使用主样式表( style.xsl
)中的copyright
模板。
摘要
除了关于导入和包含的基本规则外,XSLT还允许使用priority属性对应用哪个模板进行细粒度控制。 通过明智地使用所有这些技术,可以使用户自定义样式表的特定部分,同时仍保持对其余部分的控制权。
翻译自: https://www.ibm.com/developerworks/xml/library/x-tipimpinc/index.html
xslt元素的使用