XSLT中<xsl:apply-templates select="@*"/>语句含义解析
关于
<xsl:apply-templates select="@*"/>的解释 你的猜测完全正确。@*在XSLT里指代当前上下文节点的所有属性,<xsl:apply-templates select="@*"/>的作用就是遍历这些属性,为每个属性找到对应的<xsl:template match="...">模板并应用。
举个实际例子更容易理解:
假设你有这段XML:
<book title="XSLT入门" author="张三"/>
对应的XSLT代码如下:
<xsl:template match="book"> <div> <xsl:apply-templates select="@*"/> </div> </xsl:template> <xsl:template match="@title"> <p>书名: <xsl:value-of select="."/></p> </xsl:template> <xsl:template match="@author"> <p>作者: <xsl:value-of select="."/></p> </xsl:template>
运行这段XSLT后会生成:
<div> <p>书名: XSLT入门</p> <p>作者: 张三</p> </div>
这里<xsl:apply-templates select="@*"/>就触发了@title和@author各自的匹配模板,分别处理这两个属性。
如果没有为属性单独编写匹配模板,XSLT会调用默认模板——默认情况下,属性的默认模板会输出它的名称和值(不同XSLT版本细节略有差异,但核心是会输出属性内容)。
内容的提问来源于stack exchange,提问作者L.Li
相关产品推荐
相关产品推荐

