Chapter 2. Creating and Using Stylesheets中关于模版的引用
来源:互联网 发布:骑在银龙的背上 知乎 编辑:程序博客网 时间:2024/06/02 08:51
1。关于xslt <xsl:include>和<xsl:import>标签的差异
在一般的应用中可以说两个标签都是等同的,你怎么用都无所谓,但是<xsl:import>标签有一个优先级的概念
例如你在一个xsl模版中引入了多个外部模版,而正好这些外部模版都对同一个节点产生作用,这个使用<xsl:import>标签就能更具模版被引入的先后顺路来选择使用与那个模版。最先引入的模版优先级最低。
模版 rule1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="PLANET">
<TD><xsl:value-of select="NAME"/></TD>
<TD><xsl:value-of select="MASS"/></TD>
<TD><xsl:value-of select="RADIUS"/></TD>
<TD><xsl:value-of select="DAY"/></TD>
</xsl:template>
</xsl:stylesheet>
模版 rule2.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="PLANET">
<TD><xsl:value-of select="NAME"/></TD>
<TD><xsl:value-of select="MASS"/></TD>
<TD>abc</TD>
<TD>cba</TD>
</xsl:template>
</xsl:stylesheet>
模版 temp.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="rule1.xsl"/>
<xsl:import href="rule2.xsl"/>
<xsl:template match="/PLANETS">
<HTML>
<HEAD>
<TITLE>
The Planets Table
</TITLE>
</HEAD>
<BODY>
<H1>
The Planets Table
</H1>
<TABLE BORDER="2">
<TD>Name</TD>
<TD>Mass</TD>
<TD>Radius</TD>
<TD>Day</TD>
<xsl:apply-templates/>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
我们可以看到rule1.xsl 和rule2.xsl都同样对<PLANET>这个节点产生作用,这个时候由于rule2.xsl晚于rule1.xsl导入,所以他的优先级比rule1.xsl高,所有模版temp.xsl将会使用rule2.xsl而不是rule1.xsl。这个特性很有用,当我们有很多模版的时候,就能避免对于不同模版中相同的<xsl:template>产生冲突。也不用担心模版到底会调用哪个的实现的问题,你可以把它理解为java的继承感念,子模版复写了父模版的方法(感觉这个说法有点变扭,不过我就是这么理解的,呵呵)。
2。关于模版中已经存在的的<xsl:template>和所到入的外部模版重复时怎么样确定具体使用哪个模版
在xslt中<xsl:apply-imports/>可以是我们引用外部模版而不是自身的模版的<xsl:template>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="rule1.xsl"/>
<xsl:template match="/PLANETS">
<HTML>
<HEAD>
<TITLE>
The Planets Table
</TITLE>
</HEAD>
<BODY>
<H1>
The Planets Table
</H1>
<TABLE BORDER="2">
<TR>
<TD>Date</TD>
<TD>Name</TD>
<TD>Mass</TD>
<TD>Radius</TD>
<TD>Day</TD>
<xsl:apply-templates/>
</TR>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="PLANET">
<TR>
<TD>4/1/2002</TD>
<xsl:apply-imports/> <!--在这里引用的是rule1.xsl的<xsl:template match="PLANET"/>模版 -->
</TR>
</xsl:template>
</xsl:stylesheet>
NewVersion of rule1.xsl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="PLANET"> <TD><xsl:value-of select="NAME"/></TD> <TD><xsl:value-of select="MASS"/></TD> <TD><xsl:value-of select="RADIUS"/></TD> <TD><xsl:value-of select="DAY"/></TD> </xsl:template> </xsl:stylesheet>
个人感觉这个标签简直太好用了,可以让我们复用很多模版,这样我们可以为不同的内容设计不同的模版,最后要做的只是把他们整合起来就好了。
- Chapter 2. Creating and Using Stylesheets中关于模版的引用
- Chapter 22: Using Item Renderers and Item Editors--Creating an item renderer and item editor
- Chapter 22: Using Item Renderers and Item Editors--Creating drop-in item renderers and item editors
- Chapter 22: Using Item Renderers and Item Editors--Creating inline item renderers and editors
- Chapter 22: Using Item Renderers and Item Editors--Creating item renderers and item editor components
- Using and Creating Builds
- 【Chapter-1】Creating and destorying objects
- >>>是否缺少 using 指令或程序集引用?-关于Asp.Net中引用类的问题。
- Creating and Using Strong-Named Assemblies
- Creating and Using Temporary Tables in Oracle
- Creating and using squashed file systems
- Creating and Using custom controlsin VC++
- 2.2 Creating and Using Switches with UISwitch
- Redis(7)Creating and Using Cluster Mode
- Creating and Using a Temporary File
- Creating Custom Annotations and Using Them
- Creating and using Clang plugin with Xcode
- custom control的使用方法(Creating and Using custom controls in VC++)
- 为何要引入虚函数
- 把健康留给自己(转载)
- 『英语杂谈』 [资源共享]20个经典学英语网站
- HR,你不得不听的营销课
- 策略游戏 图
- Chapter 2. Creating and Using Stylesheets中关于模版的引用
- Ten Rules for Web Startups
- C#中的API32
- 战略策划
- 项目经理是这样当的
- 如何在运行时确定对象类型(RTTI)
- 李开复:21世纪7种人才最抢手
- 闲来无事
- static_cast和reinterpret_cast揭密