所以我的問題是這樣的。我有一個在許多地方使用的變換文檔,通常處理很多小格式變換。在一個特定情況下,我需要從結果中刪除空格。輸出看起來是這樣的:XSLT:刪除多餘的空白字符保留節點
「\ n <我>的東西</I>非常顯着的有上標註< SUP> 1 </SUP> \ n '
我已經試過上的變化:
<xsl:template match="no_whitespace">
<xsl:variable name="result">
<xsl:apply-templates/>
</xsl:variable>
<xsl:copy-of select="normalize-space($result)"/>
</xsl:template>
但子節點從輸出中剝離。我必須非常小心,不要設置像'text()'這樣的通用模板,因爲它會干擾變換的一般處理。似乎我在這裏錯過了一些明顯的東西。
編輯:試圖寫作由Stefan Hegny建議的身份轉換。
<xsl:template match="title_full">
<xsl:apply-templates mode="stripwhitespace"/>
</xsl:template>
<xsl:template match="text()" mode="stripwhitespace">
<xsl:value-of select="normalize-space(translate(., '\n', ''))"/>
</xsl:template>
<xsl:template match="/ | @* | *" mode="stripwhitespace">
<xsl:apply-templates select="."/>
</xsl:template>
這解決了我的問題,即在標記的最高級別刪除空格和換行符,然後允許轉換正常進行。對於這個虛構的問題抱歉,並感謝您的幫助。
編輯第二:'translate'的使用不像我預期的那樣工作,它按字符工作。我使用了一種替換子字符串的轉換。
請給XML輸入的reprsentative例子。 –
我應該補充說,這個例子是我正在使用的標題xml的一個例子,但是轉換也能夠接收各種大段的文本,包含多個段落, – Snow