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

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

Probability and Computing: Randomization and Probabilistic Techn

Probability and Computing: Randomization and Probabilistic Techn

Michael Mitzenmacher、Eli Upfal / Cambridge University Press / 2017-7-3 / USD 62.23

Greatly expanded, this new edition requires only an elementary background in discrete mathematics and offers a comprehensive introduction to the role of randomization and probabilistic techniques in m......一起来看看 《Probability and Computing: Randomization and Probabilistic Techn》 这本书的介绍吧!

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

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具