有没有一种方法可以将XSLT的一部分限制为单个节点,从而不必每次都需要节点的整个路径?
例如...
Name: <xsl:value-of select="/root/item[@attrib=1]/name"/>
Age: <xsl:value-of select="/root/item[@attrib=1]/age"/>
这可以通过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>
我想我想问的是,是否存在与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>
最佳答案
正确的方法是使用模板:
<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>
关于xslt - XSLT的限制部分为单节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9929174/