DSS
DSS

Reputation: 211

How to know variable has value or not in XSLT

I am creating XSLT file. I have one variable which take value from XML file.But it may happen that there is no reference in xml for the value and at that time XSL variable will return False/None(don't know).I want keep condition like,If there is no value for the variable use the default one. How to do that ?

Upvotes: 21

Views: 70510

Answers (5)

ceving
ceving

Reputation: 23856

You can use string-length to check, if a variable called $reference for example contains anything.

<xsl:choose>

  <xsl:when test="string-length($reference) > 0">
    <xsl:value-of select="$reference" />
  </xsl:when>

  <xsl:otherwise>
    <xsl:text>some default value</xsl:text>
  </xsl:otherwise>

</xsl:choose>

If necessary use normalize-space, too.

Upvotes: 6

Sudip7
Sudip7

Reputation: 2384

I tried a lot of solution from SO, my last solution was taken from @dimitre-novatchev, but that one also not working every time. Recently I found one more solution from random google search, thought to share with the community.

In order to check empty variable value, we can declare an empty variable and compare its value against test condition. Here is code snippet:

<xsl:variable name="empty_string"/>
<xsl:if test="testVariableValue != $empty_string">
...
</xsl:if>

Here testVariableValue hold the value of new variable to be tested for empty scenario. Hope it would help to test empty variable state.

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

You haven't explained what you mean by "has no value". Here is a generic solution:

not($v) and not(string($v))

This expression evaluates to true() iff $v "has no value".

Both conditions need to be met, because a string $v defined as '0' has a value, but not($v) is true().

In XSLT 1.0 using a default can be achieved in different ways if the "value" is a node-set or if the value is a scalar (such as a string, a number or a boolean).

@Alejandro provided one way to get a default value if a variable that is supposed to contain a node-set is empty.

If the variable is supposed to contain a scalar, then the following expression returns its value (if it has a value) or (otherwise) the desired default:

concat($v, substring($default, 1 div (not($v) and not(string($v)))))

Upvotes: 16

Dirk Vollmar
Dirk Vollmar

Reputation: 176179

With the few details given in the question, the simplest test you can do is:

<xsl:if test="$var">
    ...
</xsl:if>

Or you might use xsl:choose if you want to provide output for the else-case:

<xsl:choose>
    <xsl:when test="not($var)"> <!-- parameter has not been supplied -->
</xsl:when>
    <xsl:otherwise> <!--parameter has been supplied --> </xsl:otherwise>
</xsl:choose>

The second example will also handle the case correctly that the variable or parameter has not been supplied with an actual value, i.e. it equals the empty string. This works because not('') returns true.

Upvotes: 34

user357812
user357812

Reputation:

First, all variables has values, because XSLT belongs to declarative paradigm: there is no asignation instruction, but when you declare the variable you are also declaring the expression for its value relationship.

If this value it's a node set data type (that looks from your question), then you should test for an empty node set in case nothing was selected. The efective boolean value for an empty node set is false. So, as @0xA3 has answered: test="$node-set".

You wrote:

If there is no value for the variable use the default one. How to do that ?

Well, that depends on what kind of data type you are looking for.

Suppose the node set data type: if you want $node-set-1 value or $node-set-2 if $node-set-1 is empty, then use:

$node-set-1|$node-set-2[not($node-set-1)]

Upvotes: 0

Related Questions