【发布时间】:2016-12-14 06:53:59
【问题描述】:
我需要指定从 html 文件到文本文件的输出顺序。因此我使用 xsl:apply-templates 选择方法。 它工作正常,但为了微调不同节点的输出,我需要一个相应的模板,而不仅仅是一个通用模板。这也可以,但我需要在模板的匹配模式中重复选择模式。
我喜欢定义一个保存模式的变量,所以它只需要定义一次。 下面是我的简化样式表和简化的 html,它们不起作用,但给出了我想要完成的想法。 是否可以使用这样的变量?如果需要,我可以同时使用 xslt 1.0 和 2.0。
<xsl:stylesheet ...>
...
<xsl:variable name="first">div[@class='one']</xsl:variable>
<xsl:variable name="second">div[@class='two']</xsl:variable>
<xsl:template match="/*">
<xsl:apply-templates select="//$first"/>
<xsl:apply-templates select="//$second"/>
...
</xsl:template>
<xsl:template match="//$first">
<xsl:text>Custom text for class one:</xsl:text><xsl:value-of select="text()"/>
</xsl:template>
<xsl:template match="//$second">
<xsl:text>Custom text for class two:</xsl:text><xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
html:
...
<div class="two">text from two</div>
<div class="one">text from one </div>
...
期望的输出:
Custom text for class one: text from one
Custom text for class two: text from two
【问题讨论】: