2010-04-26 25 views
0

這個問題不保留其他標籤頗爲相似this one。只是我想只保留<br />和一些<img>標籤(帶class =「可見」)。即從:如何使用XSLT維護<br />轉換XML,然後在文本

<example> 
    <text> 
     Some text with <br /> and <img src="source" /> then text .... <img class="visible" src="source" /> 
    </text> 
</example> 

擁有:

<div class="example"> 
    <p>Some text with <br /> and then text .... <img class="visible" src="source" /></p> 
</div> 
+0

很好的問題,+1。見我的回答對一個完整的解決方案。 :) – 2010-04-26 17:09:28

回答

1

這種轉變

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

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

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

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

<xsl:template match= 
    "example/text/* 
      [not(self::br) 
     and 
      not(self::img[@class='visible']) 
      ]"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用時產生想要的,正確的結果

<div class="example"><p> 
     Some text with <br/> and then text .... <img class="visible" src="source"/></p></div> 
-1

我沒有測試,但我認爲這應該爲你做。

<xsl:template match="/example/text"> 
    <div class="example"> 
    <p> 
     <xsl:copy-of select="@* | node()[name()='br' or (name()='img' and @class='visible')]"/> 
    </p> 
    </div> 
</xsl:template> 
相關問題