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?
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="' '"/></td> .
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!
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.
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.
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.
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:
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.
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.
Using Sequence Expressions:
<xsl:value-of select="(foo,0)[1]"/>
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.
You can use a Conditional Expressions (if…then…else
) on your @select
expression:
<xsl:value-of select="if (foo) then foo else 0" />
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With