Storing DRY XSLT Code with "if" Tests and "value-of" Selects

In XSLT, what is the preferred way to save DRY code when it comes to 'if's?

I am currently doing this:

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

I would prefer to write "select / some / long / path" only once.

+3
source share
2 answers

I see your thought. When the path is 200 characters long, the code can become messy.

You can just add it to a variable

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

<xsl:if test="$path">    
   <xsl:value-of select="$path" />
</xsl:if>
+6
source

Where is the difference between:

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

and

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

? If it does not exist, the value displays an empty string (i.e. Nothing). So why the test?

0
source

Source: https://habr.com/ru/post/1716082/


All Articles