Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT set default value when selected one is not available

Tags:

xml

xslt

Is it possible to set a default value using <xsl:value-of>? I am attempting to produce JSON output with an XSLT stylesheet and certain fields might not be available during the processing stage. This leaves a null value which breaks the validity of the JSON document. Ideally I'd be able to set a default value if one is not available. So in the case of:

    "foo_count": <xsl:value-of select="count(foo)" />

If <foo> is not available in the document, can I just set this to 0 somehow?

like image 428
randombits Avatar asked Sep 06 '13 19:09

randombits


People also ask

How do you set a blank value in XSLT?

Simply use CSS in your HTML with <style>table { emtpy-cells: show; }</style> , see developer.mozilla.org/en-US/docs/Web/CSS/empty-cells. If you really want to put a text node with a space into a cell then use <td><xsl:value-of select="'&#160;'"/></td> .

How do you assign a value to XSLT variable?

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!

What does xsl value of select /> mean?

Definition and Usage The <xsl:value-of> element extracts the value of a selected node. The <xsl:value-of> element can be used to select the value of an XML element and add it to the output.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What is an XSLT variable?

Definition of XSLT Variable. 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.

How to extract the value of a selected node in XSL?

The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:

How to change the book title in XSLT file?

We can do with the XSL variable and the book title is changed provided the changes done in one preferred location in the XSLT file. For example, we may need to use the value of current-time twenty times. Instead of passing a call to current-time (), we can call once and the corresponding value is stored in a variable.

What are the different modes of output methods in XSLT?

The above code uses a global variable and could be accessed throughout the documents.XSLT supports three modes of Output methods XML, HTML and Text. Here I have used HTML to show.


3 Answers

XSLT/XPath 2

Using Sequence Expressions:

<xsl:value-of select="(foo,0)[1]"/>

Explanation

One way to construct a sequence is by using the comma operator, which evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence.

like image 131
G. Ken Holman Avatar answered Oct 23 '22 17:10

G. Ken Holman


XSLT/XPath 2.0

You can use a Conditional Expressions (if…then…else) on your @select expression:

<xsl:value-of select="if (foo) then foo else 0" />
like image 27
Édouard Lopez Avatar answered Oct 23 '22 17:10

Édouard Lopez


It is either choose

<xsl:choose>
   <xsl:when test="foo">
     <xsl:value-of select="count(foo)" />
   </xsl:when>
   <xsl:otherwise>
     <xsl:text>0</xsl:text>
   </xsl:otherwise>
 </xsl:choose> 

or use if test

<xsl:if test="foo">
  <xsl:value-of select="count(foo)" />
</xsl:if>
<xsl:if test="not(foo)">
  <xsl:text>0</xsl:text>
</xsl:if>

or use a named template for calling

<xsl:template name="default">
  <xsl:param name="node"/>
  <xsl:if test="$node">
      <xsl:value-of select="count($node)" />
    </xsl:if>
    <xsl:if test="not($node)">
      <xsl:text>0</xsl:text>
  </xsl:if>
</xsl:template>

 <!-- use this in your actual translate -->
 <xsl:call-template name="default">
         <xsl:with-param name="node" select="."/>
 </xsl:call-template>
like image 19
rene Avatar answered Oct 23 '22 18:10

rene