2

我有这个代码:

<a>
   <xsl:attribute name="href">
        <xsl:value-of select="$foo"/>
   </xsl:attribute>
   bar
</a>

问题是转换后我得到:

<a href="&#xA;                PooValue        &#xA;"                 >bar</a>

我的 xsl:output 带有 indent="no"。

Visual Studio 缩进所有文件。因此,将代码放在一行中,但是

<a><xsl:attribute name="href"><xsl:value-of select="$foo"/></xsl:attribute>bar</a>

first 不是很可读,VS 会改变我的缩进,所以我想要另一个解决方案。有点儿 :

<xsl:attribute name="href" select="concat(mystuff)" />

但它不存在,也不再具有可读性。

其他解决方案可能是:

<a href="{$foo}" >bar</a>

但是,我如何使用下面的 xsl 处理:

<a>
       <xsl:attribute name="href">
             <xsl:choose >
                 <xsl:when test="$atest">
                    <xsl:value-of select="$foo"/>
                 </xsl:when>
                 <xsl:otherwise>
                    <xsl:value-of select="$foo2"/>
                 </xsl:otherwise>
             </xsl:choose >
       </xsl:attribute>
       bar
    </a>

使用: <xsl:value-of select="normalize-space($foo)"/>将没有效果原因:在和
&#xA;之间创建
<xsl:attribute name="href"><xsl:value-of select="normalize-space($foo)"/>

xslt 1.0 C# .net 4一起工作XslCompiledTransform

更多细节:我把我的 XslCompiledTransform 的结果放在一个

4

3 回答 3

2

使用内联评估语法。

<a href="{$foo}" />

但是,您似乎遇到了不同的问题。
您看到的空格和换行符来自数据源,而不是来自 XSL 模板。

在这种情况下,您可以使用:

<a>
  <xsl:attribute name="href">
    <xsl:value-of select="normalize-space($foo)"/>
  </xsl:attribute>
  bar
</a>

编辑:

如果我明确地说,我只能重现这种行为:

<a>
  <xsl:attribute name="href" xml:space="preserve">
    <xsl:value-of select="$foo"/>
  </xsl:attribute>
  bar
</a>

在这种情况下,请尝试

<a>
  <xsl:attribute name="href" xml:space="default">
    <xsl:value-of select="$foo"/>
  </xsl:attribute>
  bar
</a>
于 2011-08-05T08:03:32.253 回答
1

检查样式表中是否有 xml:space 属性。这将导致您的 xsl:attribute 指令中的空白被视为重要。

于 2011-08-05T14:10:50.290 回答
0

内部的空格(空格和换行符)<a>被视为重要。

如果要确保 XSLT 处理器忽略该空格,请将文本“bar”放在xsl:text元素内:

<a>
   <xsl:attribute name="href">
        <xsl:value-of select="'foo'"/>
   </xsl:attribute>
   <xsl:text>bar</xsl:text>
</a>

通过这种方式,很明显,您希望在输出中包含的唯一文本就是xsl:text.

我从上面的示例中得到以下输出:

<a href="foo">bar</a>

虽然它有点冗长,但将您想要输出的文本放入其中xsl:text有助于确保只有您想要的文本包含在输出中,而不是有时包含随机空格和回车,并且您可以随意格式化您的XSLT 无需担心可能包含哪些空格和换行符。

于 2011-08-06T23:39:42.547 回答