xml – XSLT设置默认值,当选择一个不可用时

栏目: 编程语言 · XML · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/18665516/xslt-set-default-value-when-selected-one-is-not-available
是否可以使用<xsl:value-of>?设置默认值?我试图使用XSLT样式表生成 JSON

输出,并且在处理阶段可能无法使用某些字段.这会留下一个空值,这会破坏JSON文档的有效性.理想情况下,如果没有默认值,我可以设置默认值.所以在以下情况下:

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

如果<foo>在文档中不可用,我可以将其设置为0吗?

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

或使用 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>

或使用 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>

翻译自:https://stackoverflow.com/questions/18665516/xslt-set-default-value-when-selected-one-is-not-available


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security Testing Cookbook

Web Security Testing Cookbook

Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99

Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具