【问题标题】:Why does String get trimmed in XSLT?为什么 String 在 XSLT 中会被修剪?
【发布时间】:2014-01-24 19:28:28
【问题描述】:

我希望我的字符串以我使用 XSLT XPATH 从 XML 生成的 pdf 格式显示。我的 XML 包含带有一些空格的字符串,但问题是 xslt 会自动修剪这些字符串。我已经在这个 W3SCHOOL 链接上检查过了 Try it yourself。每当我添加一些额外的空间时,它就会自动被修剪,这在我的情况下我真的不需要。任何帮助将不胜感激。

--------更新------------

我尝试用  替换字符串中的所有空格并尝试显示它,但xslt 没有将  作为空格。它就像字符串的一部分一样考虑它。 我还尝试使用递归将字符串中的特殊字符替换为空格(我已将空格替换为 xml 中的一个字符,现在我的 xml 包含我需要空格的那个字符)但它在递归调用时显示 named template is not available

<xsl:template
 name="add-spaces">
<!-- COMMENTS is string containing ~ where I need spaces -->
    <xsl:param
         name="text"
     select="COMMENTS" />
      <xsl:if
        test="$text != ''">
         <xsl:variable
           name="letter"
             select="substring($text, 1, 1)" />
         <xsl:when
        test="$letter = '~'">
       <fo:inline>
       <xsl:text>&#160;</xsl:text>
       </fo:inline>
     </xsl:when>
     <xsl:otherwise>
        <fo:inline>
         <xsl:value-of
          select="$letter" />
        </fo:inline>
      </xsl:otherwise>
      <xsl:call-template
       name="add-spaces">
               <xsl:with-param
        name="text"
        select="substring-after($text, $letter)" />
       </xsl:call-template>
</xsl:if>
</xsl:template>

【问题讨论】:

  • 请提供一些您的字符串被修剪的示例代码,并说明您正在使用哪些工具(FOP?)。
  • XSLT 和 XPath 不生成 PDF 文件。那你用什么工具?在任何情况下,您都需要在此处添加您的 XSLT 代码。

标签: xml string xslt xpath trim


【解决方案1】:

您正在生成 HTML。修剪正常空格是HTML的问题。对于剩余的空格,您可以使用 &amp;nbsp;。但是,您不能在 XML 中使用 &amp;nbsp; 而不将其定义为实体,但您可以使用 &amp;#160; 代替:

<artist>Bob Dylan&#160;</artist>

或者您可以在根标记之前在 XML 中定义实体:

<!DOCTYPE cds[
<!ENTITY nbsp "&#160;">
]>

然后使用

<artist>Bob Dylan&nbsp;</artist>

但是,对我来说,如果您在开头和结尾没有空格,表格看起来很丑。但是,解决方案不是添加空格,解决方案是在转换中指定填充和边距,例如

<td style="padding:0 10 0 10"><xsl:value-of select="catalog/cd/title"/></td>

【讨论】:

  • 我用&amp;#160; 替换了字符串中的` `(空格),我的xslt 没有把它当作空格而是字符串的一部分。例如字符串Bob Dylan&amp;#160; 显示为Bob Dylan&amp;#160; 而不是Bob Dylan
  • 有什么办法,我可以读取每个字符并将其更改为 xslt 中的空格。我说的是迭代字符串中的每个字符,然后决定是否出现特殊字符?我试过递归,但它说named template is not available on recursive call.
【解决方案2】:

为了解决这个问题,我只是用特殊字符替换了所有空格,比如 XML 字符串中的 ~,然后在 xslt 中我使用了上面的模板,只做了一点修改(使用 if 而不是 when,我不这样做'不知道为什么when 不起作用),我完成了。现在在 pdf 中,我所有的字符串都包含我需要的空格。可能下面的代码是一个业余代码,但暂时它对我有用。

 <xsl:template name="add-spaces">
    <xsl:param name="text" select="COMMENTS" />
    <xsl:if test="$text != ''">
        <xsl:variable name="letter" select="substring($text, 1, 1)" />
        <xsl:if test="$letter = '~'">
            <xsl:text>&#160;</xsl:text>
        </xsl:if>
        <xsl:if test="$letter != '~'">
            <fo:inline>
                <xsl:value-of select="$letter" />
            </fo:inline>
        </xsl:if>
        <xsl:call-template name="add-spaces">
            <xsl:with-param name="text"
                select="substring-after($text, $letter)" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>
    <!-- Above is template and its call where I displayed strings is-->
<xsl:call-template
name="add-spaces">
<xsl:with-param
  name="text"
  select="COMMENTS" />
</xsl:call-template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多