XSLT合并模板简述 日期:10/24/2004 9:22:23 AM l XSLT复用使用模板有两种方式 1. 包含复用 2. 导入复用 l 在这之前,我们必须熟悉以下的概念 1. 模板的优先级 由于XSLT有复用模板的功能,,当有模板产生冲突,就需要根据模板的优先级来选择应用哪一个模板.XSLT处理器总是优先选择模板优先级较高的模板.. 2. 那什么是包含复用呢?即一个XSLT文件包含来自另一个XSLT文件中的模板., 在XSLT文件中,使用<xsl:include href=”被包含文件”/>来使用包含复用 在包含复用里,当被包含文件与包含文件产生模板冲突时,XSLT将发出错误信号并停止或选择XSLT文档中最后定义的那个模板. 3. 什么又是导入复用?即一个XSLT文件导入另一个XSLT文件中的模板, 在XSLT文件中,使用<xsl:import href=”被导入的文件”/>来使用导入复用. 在导入复用里,当被导入文件与导入文件中产生模板冲突时,被导入文件中的模板的优先级低于导入文件中的模板的优先级,所以会优先选用导入文件中的模板. l 下面,我举出几个例子. 1 包含复用例子(无模板冲突) (在压缩包中的文件includeImport.xml, includeNo1.xslt, includeNo2.xslt) includeImport.xml 源代码 <?xml version="1.0" encoding="gb2312"?> <!-- //包含复用(无模板冲突时) --> <?xml-stylesheet type="text/xsl" href="includeNo1.xslt"?> <includeImport> <include name="include">INCLUDE</include> <import name="import">IMPORT</import> </includeImport> ncludeNo1.xslt 源代码 <?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output version="1.0" method="html" encoding="gb2312"/> <xsl:include href="includeNo2.xslt"/> <!-- --> <xsl:template match="/"> <xsl:apply-templates select="includeImport/include "/> <xsl:apply-templates select="includeImport/import "/> </xsl:template> <xsl:template match="include"> Element Value = <xsl:value-of select="."/> Attribute Value = <xsl:value-of select="./@name"/> </xsl:template> <xsl:import href="importNo2.xslt"/> </xsl:stylesheet> includeNo2.xslt 源代码 <?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output version="1.0" method="html" encoding="gb2312"/> <xsl:template match="import "> Element Value = <xsl:value-of select="."/> Attribute Value = <xsl:value-of select="./@name"/> </xsl:template> </xsl:stylesheet> 2 包含复用例子(有模板冲突)请注意这里的实难结果 (在压缩包中的文件includeImport.xml, includeYes1.xslt, includeYes2.xslt) includeImport.xsl 源代码 <?xml version="1.0" encoding="gb2312"?> <!-- //包含复用(有模板冲突时) --> <?xml-stylesheet type="text/xsl" href="includeYes1.xslt"?> <includeImport> <include name="include">INCLUDE</include> <import name="import">IMPORT</import> </includeImport> includeYes1.xslt 源代码 <?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output version="1.0" method="html" encoding="gb2312"/> <xsl:template match="/"> <xsl:apply-templates select="includeImport/include "/> </xsl:template> <xsl:template match="include"> Use Tempale includeYes1.xslt Element Value = <xsl:value-of select="."/> Attribute Value = <xsl:value-of select="./@name"/> </xsl:template> <xsl:include href="includeYes2.xslt"/> <!-- --> </xsl:stylesheet> includeYes2.xslt 源代码 <?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output version="1.0" method="html" encoding="gb2312"/> <xsl:template match="include"> Use Tempale includeYes2.xslt Element Value = <xsl:value-of select="."/> Attribute Value = <xsl:value-of select="./@name"/> </xsl:template> </xsl:stylesheet> 3 导入复用例子(无模板冲突)(这个结果总是出错,不知道为什么?) 4 导入复用例子(有模板冲突)请注意导入文件与被导入文件的模板的优先级,差看结果优先选择了哪一个文件的模板 (这个结果总是出错,不知道为什么?)
附件有原代码. 附件:1119012920216XSLT.rar(15K) |
|