Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know variable has value or not in XSLT

Tags:

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 ?

like image 995
DSS Avatar asked Nov 18 '10 19:11

DSS


People also ask

How do I assign a value to a variable in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

What is difference between Param and variable in XSLT?

The difference is that the value of an xsl:param could be set outside the context in which it is declared.


3 Answers

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)))))
like image 21
Dimitre Novatchev Avatar answered Sep 29 '22 06:09

Dimitre Novatchev


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.

like image 109
Dirk Vollmar Avatar answered Sep 29 '22 06:09

Dirk Vollmar


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.

like image 39
ceving Avatar answered Sep 29 '22 06:09

ceving