是否有将XSLT的一部分限制为单个节点的方法,以便每次都不需要整个节点路径?将XSLT的部分限制为单个节点
例如...
Name: <xsl:value-of select="/root/item[@attrib=1]/name"/>
Age: <xsl:value-of select="/root/item[@attrib=1]/age"/>
这可以通过换每个命令,但我认为,导致这些应该是可以避免的,如果可能的话做...
<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:for-each'本身没有任何问题,就像你在这里做的一样。在这种情况下,它以“有”的方式正常工作。当XSLT提供更好的方法来做等价的事情时,例如让模板应用和匹配,人们建议不要使用'xsl:for-each'来显式地循环。 – 2012-03-30 03:03:04
Downvoter ...我可以问为什么,过了这么久? – freefaller 2012-07-03 13:28:15
Downvoter ...不,我不这么认为......你只是隐藏在SO的匿名背后,而不是建设性的。谢谢,非常感谢! – freefaller 2012-07-03 13:35:43