开发者

Xsl 
 for new line in transformation

I've this code :

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

the probleme is after transformation I get :

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

My xsl:output is with indent="no".

Visual studio indent all the files. So put the code in one line work but

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

first is not very readable and VS will change me the indent so I want another solution. kind of :

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

but it doesn't exist and it is no very readable again.

Other solution may be :

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

but and How can I use xsl treatment like below with this :

<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>

Use : <xsl:value-of select="normalize-space($foo)"/> will have no effect cause :

&#xA开发者_开发问答; is created between

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

I work in xslt 1.0 C# .net 4 with XslCompiledTransform

More details : I put the result of my XslCompiledTransform in a


Use the inline evaluation syntax.

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

However, you appear to have a different problem.
The spaces and new lines you see come from the data source, not from the XSL template.

In which case you can use:

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

EDIT:

I can only reproduce this behaviour if I explicitly say:

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

In which case, try

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


Check to see if there's an xml:space attribute anywhere in the stylesheet. This would cause the whitespace in your xsl:attribute instruction to be treated as significant.


The whitespace(spaces and line breaks) inside the <a> are being seen as significant.

If you want to ensure that the XSLT processor ignores that whitespace, put the text "bar" inside of an xsl:text element:

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

In this way, it is clear that the only text that you want included in your output is what is inside the xsl:text.

I get the following output from the above sample:

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

Although it is a bit more verbose, putting the text that you want output inside of xsl:text helps ensure that only the text you want is included in the output, rather than sometimes random spaces and carriage returns being included, and you can feel free to format your XSLT without worrying about what spaces and line-breaks might get included.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜