2012-03-07 42 views
1

尾隨換行符我有這樣與結構的XML:帶領導和XSLT

<xxx> 
<yyy> 
some text with spaces inside 
</yyy> 
<zzz> 
another text 
</zzz> 
</xxx> 

我要生成將與空白格式化的html:前期的風格,但我必須刪除換行符來自節點yyy和zzz的字符。

我的輸出應該是這樣的:

<div><div>some text with spaces inside</div><div>another text</div></div> 

我已經試過

<xsl:output method="html" indent="no"/> 
<xsl:strip-space elements="*"/> 

,但沒有奏效。換行符仍然存在。

我不能使用規範化空間,因爲單詞之間的空格很重要。

回答

1

因爲你只是想從字符串中刪除第一個和最後一個字符,這個簡單的轉變正是這樣做的

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="yyy/text() | zzz/text()"> 
    <xsl:value-of select="substring(.,2, string-length() -2)"/> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加:

<xxx> 
<yyy> 
some text with spaces inside 
</yyy> 
<zzz> 
another text 
</zzz> 
</xxx> 

的希望,正確的結果產生

<xxx> 
<yyy>some text with spaces inside</yyy> 
<zzz>another text</zzz> 
</xxx> 

請注意:在更復雜的情況下,你可能要剝離工作的所有前導空格和所有尾隨witespace。

下面是使用trim()的演示:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:import href="trim.xsl"/> 

    <!-- to be applied on trim.xml --> 

    <xsl:output method="text"/> 
    <xsl:template match="/"> 
    '<xsl:call-template name="trim"> 
     <xsl:with-param name="pStr" select="string(/*)"/> 
    </xsl:call-template>' 
    </xsl:template> 
</xsl:stylesheet> 

當這種轉變是在應用

這可以,如果你使用trim()功能/模板從FXSL完成以下XML文檔

<someText> 

    This is some text 

</someText> 

的通緝(修剪)結果產生

'This is some text' 
3

以下XSL將刪除所有元素均換行符yyyzzz

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" /> 
<xsl:template match="/xxx"> 
<div> 
    <xsl:apply-templates /> 
</div> 
</xsl:template> 
<xsl:template match="yyy | zzz"> 
<div> 
    <xsl:value-of select="translate(.,'&#x0d;&#x0a;', '')" /> 
</div> 
</xsl:template> 
</xsl:stylesheet> 
+0

您是右,上面的代碼刪除ALL換行符。這不完全是我所需要的。 – 2012-03-08 13:30:02