2010-08-10 100 views
5

嗨我正在使用xslt來顯示我的web部件上的鏈接,並且在鏈接旁邊添加了加號圖像,但我想在它們之間添加一些空格。我已經添加了,但這不是真的有效。我在這裏錯過了什麼?請在下面找到我的代碼。在xslt中插入空格

謝謝。

<xsl:choose> 
     <!-- do _self --> 
     <xsl:when test="contains(Link,'xxx')"> 
      <a target="_self"> 
      <xsl:attribute name="href"> 
       <xsl:value-of select="URL"/> 
      </xsl:attribute> 
       <xsl:value-of select="Title"/> 
      </a> 
     </xsl:when> 
     <!-- use _blank (new browser window) --> 
    <xsl:otherwise> 
     <a target="_blank"> 
     <xsl:attribute name="href"> 
      <xsl:value-of select="URL"/> 
     </xsl:attribute> 
      <xsl:value-of select="Title"/> 
     </a> 
    </xsl:otherwise> 
    </xsl:choose> 
    <xsl:text> </xsl:text> 
    <xsl:choose> 
     <xsl:when test="Description !=' ' "> 
      <img class="imageclass" src="/images/plus.gif"></img> 
     </xsl:when> 
    </xsl:choose> 

回答

13

我的理解是要生成HTML,將在瀏覽器中顯示空白。

如果是這樣的話,不要使用空格 - brouser只爲連續的空格序列顯示一個空格。

使用不間斷空格&#xA0;

,而不是所以:

<xsl:text> </xsl:text> 

使用

<xsl:text>&#xA0;&#xA0;&#xA0;</xsl:text> 
+0

太好了!它爲我工作。非常感謝你的幫助。 – user346514 2010-08-11 13:05:45

5

xsl:text指令是您的請求的正確工具。作爲例子,這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="a"> 
     <xsl:call-template name="identity"/> 
     <xsl:text> </xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個輸入:

<div> 
    <a href="#">link1</a> 
    <a href="#">link1</a> 
    <a href="#">link1</a> 
</div> 

輸出:

<div><a href="#">link1</a> <a href="#">link1</a> <a href="#">link1</a> </div>