XSL最全基础教程之xsl:apply-imports元素
1、<xsl:apply-imports>元素 <xsl:apply-imports>元素导入其他样式表的模板规则,用该元素导入的模板规则可以用来重写主样式表中的模板规则。 通过该元素导入的模板规则的优先级根据导入的顺序由低到高,但都低于主样式表的模板规则。
2、语法 <xsl:apply-imports/>
3、示例创建一个名为operation.xml文件,该文件有三组操作数分别用来加、减、乘运算。<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="operation.xsl"?><operations> <operation name="add" symbol="+"> <operand>1</operand> <operand>2</operand> </operation> <operation name="sub" symbol="-"> <operand>1</operand> <operand>2</operand> </operation> <operation name="mul" symbol="*"> <operand>1</operand> <operand>2</operand> </operation></operations>
4、创建xsl文件创建名为operation.xsl文件,测试apply-imports元素。<?x罪焐芡拂ml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="arith.xsl"/> <xsl:import href="result.xsl"/> <xsl:template match="operations"> <html> <head> <title>测试apply-imports元素</title> </head> <body> <xsl:apply-templates select="operation"/> </body> </html> </xsl:template> <xsl:template match="operation"> <xsl:value-of select="operand[1]"/> <xsl:value-of select="@symbol"/> <xsl:value-of select="operand[2]"/> =<xsl:apply-imports/><br/> </xsl:template></xsl:stylesheet>
5、创建名为arith.xsl文件导入arith.xsl文件,用于操作数运算<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="operation[@symbol='+']"> <xsl:value-of select="sum(operand)"/>结果来自arith.xsl文 </xsl:template> <xsl:template match="operation[@symbol='-']"> <xsl:value-of select="number(operand[1])-number(operand[2])"/> 结果来自arith.xsl文件 </xsl:template> <xsl:template match="operation[@symblo='*']"> <xsl:value-of select="number(operand[1])*number(operand[2])"/> 结果来自arith.xsl文件 </xsl:template></xsl:stylesheet>
6、创建名为result.xsl文件导入result.xsl文件,用于操作数拼接<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="operation[@name='add']"> <xsl:value-of select="operand[1]"/> <xsl:value-of select="operand[2]"/> 运算结果来自result.xsl文件 </xsl:template> <xsl:template match="operation[@name='mul']"> <xsl:value-of select="operand[1]"/> <xsl:value-of select="operand[2]"/> 运算结果来自result.xsl文件 </xsl:template></xsl:stylesheet>
7、运算结果用Firefox打开operation.xml文件,查看转换结果。分析:从运行结果可以看出,后导入的result.xsl文件优先级别较arith.xsl文件,result.xsl的结果覆盖了arith.xsl的结果。 由于result.xsl文件中没有加法运算,所以使用了arith.xsl的结果