有没有办法将XSLT的一部分限制为单个节点,以便每次都不需要节点的整个路径?
例如...
Name: <xsl:value-of select="/root/item[@attrib=1]/name"/>
Age: <xsl:value-of select="/root/item[@attrib=1]/age"/>
Run Code Online (Sandbox Code Playgroud)
这可以通过for-each命令来完成,但我认为如果可能的话应该避免这些......
<xsl:for-each select="/root/item[@attrib=1]"/>
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
我想我在问是否有一个类似于VB.NET With命令的XSLT?
我宁愿避免使用xsl:template来提高可读性,因为有问题的XSLT文件很大,但很高兴接受,如果这是唯一的方法.如果是这样,基于特定节点调用特定模板的语法是什么?
更新
在@javram的答案的后续中,可以基于特定属性/节点匹配单独的模板.
<xsl:apply-templates select="/root/item[@attrib=1]"/>
<xsl:apply-templates select="/root/item[@attrib=2]"/>
<xsl:template match="/root/item[@attrib=1]">
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:template>
<xsl:template match="/root/item[@attrib=2]">
Foo: <xsl:value-of select="foo"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
正确的方法是使用模板:
<xsl:apply-templates select="/root/item[@attrib=1]"/>
.
.
.
<xsl:template match="/root/item">
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4349 次 |
| 最近记录: |