2

我的哈希 (#) "%0A%09%09#2" 前面附加了奇怪的字符,我使用的是 Eclipse 和 java 处理器 Xalan 2.7.1

    <xsl:for-each select="fps-photo-atlas/portion">
<a>
<xsl:attribute name="href" >
    #<xsl:value-of select="normalize-space(food-number)" />
</xsl:attribute>
<xsl:value-of select="food-description" />
</a>
</xsl:for-each>

首选输出是...

 <body><a href="#1">Rice </a></body>

实际输出是..

 <body><a href="%0A%09%09#1">Rice </a></body>

已修复解决方案(感谢 Ign)

<xsl:attribute name="href" >#<xsl:value-of select="normalize-space(food-number)" /></xsl:attribute>
4

1 回答 1

2

样式表中的文本节点会被去除空格,除非以下情况之一适用:

  • 它直接位于“xsl:text”元素内
  • 它包含一个非空白字符
  • 它通过使用 'xml:space' 属性来保留

不需要的空格与您的 # 字符位于同一文本节点中,因此会被保留。

有几种方法可以排除不需要的空格。

使用属性值模板而不是“xsl:attribute”。

<a href="#{normalize-space(food-number)}">
    <xsl:value-of select="food-description" />
</a>

将 # 字符移动到“xsl:text”元素中。

<a>
    <xsl:attribute name="href" >
        <xsl:text>#</xsl:text>
        <xsl:value-of select="normalize-space(food-number)" />
    </xsl:attribute>
    <xsl:value-of select="food-description" />
</a>

将 # 字符移动到“value-of”表达式中。

<a>
    <xsl:attribute name="href" >
        <xsl:value-of select="concat('#',normalize-space(food-number))" />
    </xsl:attribute>
    <xsl:value-of select="food-description" />
</a>

从样式表中删除空格。

<a>
    <xsl:attribute name="href" >#<xsl:value-of select="normalize-space(food-number)></xsl:attribute>
    <xsl:value-of select="food-description" />
</a>
于 2012-05-02T01:36:19.003 回答