Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XSLT how do you test to see if a variable exists?

Tags:

xml

xslt

When using XSLT how do you test to see if a locally scoped variable exists, or is this even possible?

like image 978
rjzii Avatar asked Aug 19 '09 13:08

rjzii


People also ask

How to check if a variable does not exist in XSLT?

Note that this is entirely dependent on the structure of the XSLT, not the structure of the XML it's processing. The XSLT processor can and should produce an error if an XPath expression uses a variable that doesn't exist. There's no way to check for this condition inside XSLT because this condition isn't legal within XSLT.

What is an IF statement in XSLT?

XSLT if statement is defined as a conditional pattern used to process a true statement and considered a simple Statement. The statement provides a simple test to use concerning the XML file. If a certain condition is met, the respective code is executed. It takes a test attribute to return a Boolean value through the XPath expression.

What is XPath variable in XSLT?

XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it.

What is the purpose of the XSLT variable checker?

This allows XSLT processors to statically analyze any XPath containing a variable reference to see if the variable exists; if the variable declaration exists on the preceding-sibling or ancestor axis, the variable reference is legal, otherwise it's not.


2 Answers

Considering the XSLT stylesheet as an XML DOM, a variable declaration element makes the variable visible to all following siblings and their descendants. This allows XSLT processors to statically analyze any XPath containing a variable reference to see if the variable exists; if the variable declaration exists on the preceding-sibling or ancestor axis, the variable reference is legal, otherwise it's not.

Note that this is entirely dependent on the structure of the XSLT, not the structure of the XML it's processing. The XSLT processor can and should produce an error if an XPath expression uses a variable that doesn't exist.

There's no way to check for this condition inside XSLT because this condition isn't legal within XSLT. The sitauation you described in your comment - "The idea is to set a flag variable if something is output and later on display a different message if nothing was output." - actually should result in a syntax error. For instance, if you do something like this:

<xsl:if test="some_condition">
   <!-- produce output here -->
   <xsl:variable name="flag">true</xsl:variable>
</xsl:if>
<!-- time passes -->
<xsl:if test="$flag='true'>
   <!-- wouldn't it be nice? -->
</xsl:if>

you'll get a syntax error: the second xsl:if element is neither a following sibling of the variable declaration nor one of their descendants.

Here's a technique I use a fair amount - this produces variable output based on a variety of different conditions that you don't want to re-check later:

<xsl:variable name="output">
   <xsl:if test="$condition1='true'">
      <p>condition1 is true</p>
   </xsl:if>
   <xsl:if test="$condition2='true'">
      <p>condition2 is true</p>
   </xsl:if>
   <xsl:if test="$condition3='true'">
      <p>condition3 is true</p>
   </xsl:if>
</xsl:variable>
<!-- we've produced the output, now let's actually *output* the output -->
<xsl:copy-of select="$output"/>
<!-- time passes -->
<xsl:if test="normalize-space($output) != ''">
   <p>This only gets emitted if $output got set to some non-empty value.</p>
</xsl:if>
like image 87
Robert Rossney Avatar answered Oct 25 '22 08:10

Robert Rossney


Asking this question indicates that you did not fully grasp the key point of XSLT. :-)

It's declarative: nothing can exist unless you declare it. You declare a variable, then it's there, you don't, then it's not.

Not once will there be the point where you have to wonder, while coding, if a certain variable exists.

XSLT has strict scoping rules, variables exist only within the scope of their parent element, (and not all elements can contain variables to begin with). Once you leave the parent element, the variable is gone.

So unless you specify your question/intent some more, the only valid answer is that the question is wrong. You cannot and do not need to check if a variable exists at run-time.

like image 25
Tomalak Avatar answered Oct 25 '22 09:10

Tomalak