1

我试图在 XSLT 的 foreach 语句中插入一个 if-else 语句。这个想法是,如果节点存在,它将插入一个换行符。这就是我到目前为止所拥有的

<xsl:for-each select="Books/Publisher/[BookTitle]">
                  <strong>
                    <xsl:value-of select=" BookTitle " />
                  </strong>
<xsl:if test=" Books/Publisher/BookTitle">!= ''">
        <br/>  
      </xsl:if>
</xsl:for-each>

问题是,当我插入 if-else 语句时,换行标记不会出现,但只有当我完全删除 if else 时才会出现。有人可以向我解释为什么会这样,因为我已经检查过节点是否存在并且插入换行符应该可以工作。所以foreach循环内的if-else一定有问题。

4

1 回答 1

4

I think what you are going for is this:

<xsl:for-each select="Books/Publisher[BookTitle]">
  <strong>
    <xsl:value-of select="BookTitle" />
  </strong>
  <xsl:if test="BookTitle != ''">
    <br/>
  </xsl:if>
</xsl:for-each>

But if I understand correctly, it looks like all you're really trying to do is output all the non-blank book titles, followed by line breaks, so perhaps this would be a better approach:

<xsl:for-each select="Books/Publisher/BookTitle[. != '']">
  <strong>
    <xsl:value-of select="." />
  </strong>
  <br/>
</xsl:for-each>

Or better yet:

<xsl:apply-templates select="Books/Publisher/BookTitle[. != '']" />

<!-- .....  (separate template in the XSLT) -->
<xsl:template match="BookTitle">
  <strong>
    <xsl:value-of select="." />
  </strong>
  <br/>
</xsl:template>
于 2013-02-04T04:09:15.597 回答