【问题标题】:XML XSLT %0A%09%09 ? fix?XML XSLT %0A%09%09 ?使固定?
【发布时间】:2012-05-11 11:15:47
【问题描述】:

我的哈希 (#) "%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>

【问题讨论】:

  • 你试过去掉空格,特别是换行符和两个制表符吗?
  • 您有两个很好的答案,请务必对两个都投赞成票,然后“接受”您认为最能回答问题的一个。

标签: xml eclipse xslt


【解决方案1】:

样式表中的文本节点为whitespace stripped,除非满足以下条件之一:

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

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

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

使用Attribute Value Template 代替“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>

【讨论】:

  • 感谢 lachlan 很多选择 :)
猜你喜欢
  • 1970-01-01
  • 2018-03-02
  • 2019-03-06
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2017-11-12
  • 1970-01-01
相关资源
最近更新 更多