3

在 XSLT 中,当涉及到 'if' 时,保持代码 DRY 的首选方法是什么?

目前我正在这样做:

<xsl:if test="select/some/long/path">
    <element>
        <xsl:value-of select="select/some/long/path" />
    </element>
</xsl:if>

我宁愿只写一次“select/some/long/path”。

4

2 回答 2

6

我明白你的意思了。当路径长度为 200 个字符时,代码可能会变得混乱。

您可以将其添加到变量中

<xsl:variable name="path" select="select/some/long/path"/>

<xsl:if test="$path">    
   <xsl:value-of select="$path" />
</xsl:if>
于 2009-08-26T09:50:33.347 回答
0

两者的区别在哪里:

<xsl:if test="select/some/long/path">
  <xsl:value-of select="select/some/long/path" />
</xsl:if>

<xsl:value-of select="select/some/long/path" />

? 如果不存在,value-of 将输出一个空字符串(即:无)。那么为什么要考试呢?

于 2009-08-26T10:12:30.603 回答