使用xslt 1显示X个节点数

时间:2014-03-26 15:35:20

标签: xslt xslt-1.0

我有一些看起来有点像这样的xml:

<myGroup>
   <something>Stuff</something>
   <anotherThing>More Stuff</anotherThing>
   <thisThing></thisThing>
   <andAnother>Good stuff</andAnother>
   <howAboutThis></howAboutThis>
   <andOneMore>Tell me the good things</andOneMore>
   <lastOne>That come into your mind about your mother</lastOne
<myGroup>

myGroup实际上包含更多节点,但我只对特定节点感兴趣。我试图做的是检查它们是否为空,如果没有则显示它们。像这样:

<xsl:if test="something != ''">
    <xsl:value-of select="something" />
</xsl:if>
<xsl:if test="anotherThing != ''">
    <xsl:value-of select="anotherThing" />
</xsl:if>

一旦我有3个非空节点,我做的就是停止显示了。 感谢。

1 个答案:

答案 0 :(得分:1)

将条件放在谓词中:

<xsl:template match="myGroup">
  <xsl:apply-templates select="(something | anotherthing | howAboutThis | lastOne)[normalize-space()][position() &lt; 4]"/>
</xsl:template>

<xsl:template match="myGroup/*">
  <xsl:value-of select="."/>
</xsl:template>
相关问题