声明 XSL 变量时如何删除空格? [英] How can I remove whitespace when declaring an XSL variable?

查看:33
本文介绍了声明 XSL 变量时如何删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个带有选择的 XSL 变量.像下面这样:

I have to create an XSL variable with a choose in it. Like the following:

<xsl:variable name="grid_position">
  <xsl:choose>
    <xsl:when test="count(/Element) &gt;= 1">
      inside
    </xsl:when>
    <xsl:otherwise>
      outside
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

然后在我的代码中,如果:

And later in my code, I do an xsl if:

<xsl:if test="$grid_position = 'inside'">
   {...code...}
</xsl:if>

问题是由于换行和缩进,我的变量永远不会 = 'inside'.如何从变量中删除空格?我知道当我在 xsl:copy-of 中使用它时,我可以使用 disable-output-escaping="yes" 删除它,但它不适用于 xsl:variable 标记.那么如何删除那些空格和换行符?

Problem is that my variable is never = 'inside' because of the line breaks and indent. How can I remove whitespaces from my variable? I know I can remove it using disable-output-escaping="yes" when I use it in a xsl:copy-of, but it's not working on the xsl:variable tag. So how can I remove those whitespace and line breaks?

推荐答案

这就是 的用途:

<xsl:variable name="grid_position">
  <xsl:choose>
    <xsl:when test="count(/Element) &gt;= 1">
      <xsl:text>inside</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>outside</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

它允许您同时构建代码控制空格.

It allows you to structure your code and control whitespace at the same time.

事实上,您应该清除 XSL 中未包含在 中的文本节点,以避免将来出现此类错误(即,当代码重新-稍后格式化或重构).

In fact, you should stay clear of text nodes in XSL that are not wrapped in <xsl:text> to avoid these kinds of bugs in the future, too (i.e. when code gets re-formatted or re-factored later).

对于简单的情况,例如在您的示例中,执行 Jim Garrison 建议的内容 也是一种选择.

For simple cases, like in your sample, doing what Jim Garrison suggests is also an option.

顺便说一句,使用 count() 测试元素是否存在是多余的.选择它就足够了,因为空节点集的计算结果为 false.

As an aside, testing for the existence of an element with count() is superfluous. Selecting it is enough, since the empty node-set evaluates to false.

<xsl:when test="/Element">

这篇关于声明 XSL 变量时如何删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆