2013-07-22 60 views
0

考慮下面的XML文檔:XSL:包裝所有的後代文本和元素?

<dialog> 
    <speech speaker="Robert"> 
    <line>"Once more into the breach", he said.</line> 
    I can't believe him. I just <emphasis>can't</emphasis> believe him! 
    </speech> 
</dialog> 

我想嘗試和內speech捕捉eveything不在一個line已經和在line包裹它,但是我需要沿捕捉任何其他元素與它(例如上面的示例中的emphasis)。

我想達到的效果是:

<dialog> 
    <speech speaker="Robert"> 
    <line>"Once more into the breach", he said.</line> 
    <line>I can't believe him. I just <emphasis>can't</emphasis> believe him!</line> 
    </speech> 
</dialog> 

我使用libxsltlibxml,所以我堅持使用XSLT 1.0。接近,在XSLT 1.0

回答

2

一種方式是通過同級遞歸,如下面的例子所示:

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

<xsl:output method="xml"/> 

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

<xsl:template match="speech"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*"/> 
    <xsl:apply-templates select="node()[1]"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="speech/line"> 
    <xsl:call-template name="identity"/> 
    <xsl:apply-templates select="following-sibling::node()[1]"/> 
</xsl:template> 

<xsl:template match="speech/node()[not(self::line) 
            and (not(preceding-sibling::node()) 
             or 
             preceding-sibling::node()[1][self::line])]"> 
    <xsl:if test="normalize-space() or following-sibling::node()[1][not(self::line)]"> 
    <line> 
     <xsl:call-template name="identity"/> 
     <xsl:apply-templates select="following-sibling::node()[1][not(self::line)]"/> 
    </line> 
    </xsl:if> 
    <xsl:apply-templates select="following-sibling::line[1]"/> 
</xsl:template> 

<xsl:template match="speech/node()[not(self::line) 
            and preceding-sibling::node()[1][not(self::line)]]"> 
    <xsl:call-template name="identity"/> 
    <xsl:apply-templates select="following-sibling::node()[1][not(self::line)]"/> 
</xsl:template> 

</xsl:stylesheet> 

隨着該樣式表等

<dialog> 
    <speech speaker="Robert"> 
    <line>"Once more into the breach", he said.</line> 
    I can't believe him. I just <emphasis>can't</emphasis> believe him! 
    </speech> 
    <speech speaker="Foo">This is a test. 
    <line>This line is wrapped and should be copied unchanged.</line> 
    <em>This</em> needs to be <it>wrapped</it>. 
    </speech> 
    <speech speaker="Bar"> <em>This</em> should be wrapped. 
    <line>This line is wrapped and should be copied unchanged.</line> 
    <it>Test</it> 
    </speech> 
</dialog> 

的輸入採樣被變換成

<dialog> 
    <speech speaker="Robert"><line>"Once more into the breach", he said.</line><line> 
    I can't believe him. I just <emphasis>can't</emphasis> believe him! 
    </line></speech> 
    <speech speaker="Foo"><line>This is a test. 
    </line><line>This line is wrapped and should be copied unchanged.</line><line> 
    <em>This</em> needs to be <it>wrapped</it>. 
    </line></speech> 
    <speech speaker="Bar"><line> <em>This</em> should be wrapped. 
    </line><line>This line is wrapped and should be copied unchanged.</line><line> 
    <it>Test</it> 
    </line></speech> 
</dialog> 
相關問題