使用'if'测试和'value-of'选择保持XSLT代码DRY

Rya*_*all 3 xslt dry

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

目前我这样做:

<xsl:if test="select/some/long/path">
    <element>
        <xsl:value-of select="select/some/long/path" />
    </element>
</xsl:if>
Run Code Online (Sandbox Code Playgroud)

我宁愿只写一次"select/some/long/path".

Tom*_*mmy 6

我明白你的意思了.当路径长200个字符时代码会变得混乱.

您可以将其添加到变量中

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

<xsl:if test="$path">    
   <xsl:value-of select="$path" />
</xsl:if>
Run Code Online (Sandbox Code Playgroud)