2012-08-29 70 views
0

當元素包含散佈有文本的其他元素時,如何維護文本元素的順序?在本(簡化的)例子:XSLT如何處理穿插其他XML元素的多個XML文本節點

<block>1st text<bsub>2nd text</bsub>3rd text</block> 

期望的輸出是:

"1st text 2nd text 3rd text" 

我曾嘗試:

<xsl:template match="block"> 
    <xsl:value-of select="."> 
    <xsl:apply-templates select="bsub"/> 
    <xsl:value-of select="."> 
    </xsl:template> 

    <xsl:template match="bsub"> 
    <xsl:value-of select="."> 
    </xsl:template> 

並且輸出:

"1st text 2nd text 3rd text 2nd text 1st text 2nd text 3rd text" 

如何選擇個別文本元素(<block>)使用<xsl:value-of>

回答

0

不要使用value-of來處理像這樣的混合內容 - 而是使用apply-templates來代替它,它將全部爲您工作。

0

這XSLT 1.0樣式表...

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

<xsl:template match="/"> 
    <t> 
    <xsl:apply-templates /> 
    </t> 
</xsl:template> 

<xsl:template match="block|bsub"> 
    <xsl:apply-templates /> 
</xsl:template> 

<xsl:template match="text()"> 
    <xsl:value-of select="concat(.,' ')" /> 
</xsl:template> 

</xsl:stylesheet> 

...當適用於您的輸入文檔...

<block>1st text<bsub>2nd text</bsub>3rd text</block> 

... ...產量

<t>1st text 2nd text 3rd text </t>