tags:

views:

142

answers:

2

This question is quite similar to this one. Except that I want to preserve only <br /> and some <img> tags (with class="visible"). I.e from:

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

To have:

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

I didn't test it but I think this should do for you.

<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>
gbogumil
+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <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>

when applied on the provided XML document, produces the wanted, correct result:

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