在xsl中使用导入另外一个xsl,使用的方式是<xsl:import> ,import 一个style sheet 和include一个style sheet一致,
没有太大的区别
<xsl:import> 允许添加其他的顶级标签
它的子标签允许加说有在<xsl:stylesheet>标签允许下的标签
使用原来通过<xsl:import href="arith.xsl"/> 方式引入的文件中template 来处理当前节点。
如下<xsl:apply-imports/>的当前节点就是op。
在它所在文件(ops.xsl)中引入了arith.xsl文件找到它对应的template.来处理。
| Xml |
|
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ops.xsl"?>
<ops>
<desc>Some binary operations</desc>
<op name="add" symbol="+">
<operand>1</operand>
<operand>2</operand>
</op>
<op name="sub" symbol="-">
<operand>1</operand>
<operand>2</operand>
</op>
<op name="mul" symbol="*">
<operand>1</operand>
<operand>2</operand>
</op>
</ops>
|
Main XSLT File (ops.xsl)
| Xml |
|
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="arith.xsl"/>
<xsl:template match="op">
<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>
|
Imported XSLT File (arith.xsl)
|
|
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="op[@symbol='+']">
<xsl:value-of select="sum(operand)"/> (from arith.xsl)
</xsl:template>
<xsl:template match="op[@symbol='-']">
<xsl:value-of select="number(operand[1])-number(operand[2])"/>
(from arith.xsl)
</xsl:template>
<xsl:template match="op[@symbol='*']">
<xsl:value-of select="number(operand[1])*number(operand[2])"/>
(from arith.xsl)
</xsl:template>
</xsl:stylesheet>
|
Imported XSLT File (str.xsl)
| Xml |
|
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="desc">
<DIV><xsl:value-of select="."/></DIV>
</xsl:template>
<xsl:template match="op[@name='add']">
<xsl:value-of select="operand[1]"/>
<xsl:value-of select="operand[2]"/> (from str.xsl)
</xsl:template>
<xsl:template match="op[@name='mul']">
<xsl:value-of select="operand[2]"/>
<xsl:value-of select="operand[1]"/> (from str.xsl)
</xsl:template>
</xsl:stylesheet>
|
output:
Some binary operations
1+2 = 12 (from arith.xsl)
1-2 = -1 (from arith.xsl)
1*2 = 21 (from arith.xsl)
注意
ops.xsl文件加红色背景的部分如果在
<xsl:import href="arith.xsl"/>
后加上
<xsl:import href="str.xsl"/>
那么显示就变成了
Some binary operations
1+2 = 12 (from str.xsl)
1-2 = -1 (from arith.xsl)
1*2 = 21 (from str.xsl)
原因
后面覆盖前面。
ok
今天就说这个节点了。
后续加上
微软的