How to know a variable matters or not in XSLT

I am creating an XSLT file. I have one variable that takes a value from an XML file. But it may happen that in xml there is no value for the value, and at this time the XSL variable will return False / None (I don’t know). I want to save a condition like: If the variable does not use the default value. How to do it?

+12
xslt
source share
6 answers

Given the few details asked in the question, the easiest test you can do is:

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

Or you can use xsl:choose if you want to provide output for 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 also correctly handles the case when a variable or parameter was not provided with the actual value, that is, it is equal to an empty string. This works because not('') returns true .

+25
source share

You have not explained what you mean by "does not matter." Here is a general solution :

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

This expression evaluates to true() if $v "does not matter."

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

In XSLT 1.0, the default usage can be achieved differently if the β€œvalue” is a collection of nodes or if the value is a scalar (for example, a string, number, or boolean).

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

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

 concat($v, substring($default, 1 div (not($v) and not(string($v))))) 
+14
source share

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

 <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> 

Use normalize-space if necessary.

+4
source share

First, all variables have values ​​because XSLT belongs to a declarative paradigm: there is no asignation statement, but when you declare a variable, you also declare an expression for its value relationship.

If this value is a node, set the data type (which looks from your question), then you should check for an empty node set if nothing has been selected. The effective boolean for an empty node set is false. So, since @ 0xA3 answered: test="$node-set" .

You wrote:

If there is no value for the variable, use the default value. How to do it?

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

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

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

I tried many solutions from SO, my last solution was taken from @ dimitre-novatchev, but this also does not work every time. I recently found another solution from a random Google search that is believed to be sharing with the community.

To check the empty value of a variable, we can declare an empty variable and compare its value with the test condition. Here is the code snippet:

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

Here testVariableValue contains the value of the new variable to be tested for empty scripts. Hope this helps to check the empty state of the variable.

0
source share

Having tried several times, I was able to solve the problem.

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://example.com" version="1.0" exclude-result-prefixes="ns" xmlns:func="http://exslt.org/functions"> <xsl:output method="text" omit-xml-declaration="yes" indent="no" encoding="UTF-8" media-type="application/json"/> <xsl:template match="/"> <xsl:text>{</xsl:text> <xsl:text>"abc": </xsl:text> <xsl:variable name="givenDate" select="substring-before(//ns:RootElement/ns:givenDate, 'T')"/> <xsl:value-of select="ns:set_value($givenDate)"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:function name="ns:set_value"> <xsl:param name="givenDate"/> <xsl:choose> <xsl:when test="$givenDate !=''"> <func:result> <xsl:text>"</xsl:text> <xsl:value-of select="$givenDate"/> <xsl:text>"</xsl:text> </func:result> </xsl:when> <xsl:otherwise> <xsl:text>null</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:function> </xsl:stylesheet> 
0
source share

All Articles