2013-11-20 72 views
0

鑑於以下XML代碼:通過XML內容與循環格式

<section> 
<p>Text...</p> 
<p>More text...</p> 
<special>Text with formatting</special> 
<p>More text..</p> 
</section> 

我想用XSLT輸出這一點 - 但<special>標籤必須與HTML標記<pre></pre>周圍。

無法弄清楚如何使它以正確的順序出現在節中的所有元素p元素中。幫助讚賞。

回答

1

這是你如何能做到這一點:

<?xml version="1.0" encoding="utf-8"?> 

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

<xsl:output method="xml" indent="yes"/> 

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

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

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

<xsl:template match="special"> 
    <xsl:element name="pre"> 
    <xsl:copy> 
     <xsl:value-of select="."/> 
    </xsl:copy> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

關鍵位是xsl:元素構建新元素「預」你需要的。

在將來的問題中,請花點時間讓您看看已嘗試到目前爲止。而不是僅僅說你無法實現它的工作。

+0

謝謝。我正在使用一種效果不佳的結構,並且我會針對未來的問題保留你所說的內容。 – BTB

1

我將開始與身份轉換模板

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

,然後添加模板元素需要特殊處理如

<xsl:template match="special"> 
    <pre> 
    <xsl:call-template name="identity"/> 
    </pre> 
</xsl:template>