2011-10-13 93 views
1

我有標記,我想刪除/轉換某些標籤,但保留數據。XSLT - 刪除一些標籤,但保留內容的順序

例如這樣的:

<div>This is some <b>bold</b> text inside of a div</div> 
<p>This is <u>another <b>formatted</b></u> string...<br /></p> 

應該成爲這樣的:

<p>This is some <b>bold</b> text inside of a div</p> 
<p>This is another <b>formatted</b> string...</p> 

使用apply-templates以滿足每一種狀況:由於嵌套不起作用。

你會怎麼做呢?

回答

1

聽起來像是一個修改identity transform,這樣的工作:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

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

    <!--This template turns all <div> into <p>--> 
    <xsl:template match="div"> 
     <p> 
      <xsl:apply-templates select="@*|node()"/> 
     </p> 
    </xsl:template> 

    <!--This template removes all <u> and continues processing --> 
    <xsl:template match="u"> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

大,由於多數民衆贊成工作很適合我。 – Kyle