是否可以使用<xsl:value-of>
?设置默认值?我试图使用XSLT样式表生成JSON输出,并且在处理阶段可能无法使用某些字段.这会留下一个空值,这会破坏JSON文档的有效性.理想情况下,如果没有默认值,我可以设置默认值.所以在以下情况下:
"foo_count": <xsl:value-of select="count(foo)" />
Run Code Online (Sandbox Code Playgroud)
如果<foo>
文档中没有,我可以将其设置为0吗?
G. *_*man 15
使用序列表达式:
<xsl:value-of select="(foo,0)[1]"/>
Run Code Online (Sandbox Code Playgroud)
构造序列的一种方法是使用逗号运算符,该运算符 计算每个操作数并将结果序列按顺序连接成单个结果序列.
ren*_*ene 13
它要么选择
<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>
Run Code Online (Sandbox Code Playgroud)
或使用,如果测试
<xsl:if test="foo">
<xsl:value-of select="count(foo)" />
</xsl:if>
<xsl:if test="not(foo)">
<xsl:text>0</xsl:text>
</xsl:if>
Run Code Online (Sandbox Code Playgroud)
或使用命名模板进行呼叫
<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>
Run Code Online (Sandbox Code Playgroud)
您可以在表达式if…then…else
上使用条件表达式()@select
:
<xsl:value-of select="if (foo) then foo else 0" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21689 次 |
最近记录: |