Copying node with its replaced children using XSLT
I have to copy the node with a particular attribute and then replace its children with the particular literals. I'm able to change the value of the class attribute from <div class="LR" to class="WZ" but not able to change the value of id in the first div tag just after body.

I want to copy any <div class="iD2"> with its replaced children as per requirement. By Going though the suggestions/solutions in the forums I tried to do the job but when I copy nodes where class="iD2" then it copies those children also which I don't want and also <br> tag appears as <br clear="none"/> in the output. Please tell me how can I achieve the required output.

Input file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>
    <body>
    
    <div class="LR" id="12">
    <div class="id1">
    <div class="iD2">
    <div id="rah" class="bl" style="dis:bl"></div>
    
    <div class="cl">
    <br/><br/>
    <div id="ta12" class="bl" style="dis:bl">1<span style="font-family: times;"> TEXT</span> </div>
    <br/>T <input type="radio" name="op12" id="t12" attr="event()"/> 
    F <input type="radio" name="op12" id="f12" attr="event()""/>
    <div>Right </div>
    <div>Wrong </div>
    </div>
    
    </div>
    </div>
    </div>
    </body></html>

Desired Output file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html;  charset=utf-8" />
    </head>
    <body>
    <div class="WZ" id="56">
    <div class="id1">
    <div class="iD2">
    <div class="pa" value="50"></div>

    <div class="cl">
    <div id="ta8" class="bl" style="dis:bl">TEXT
    </div><br/>
    <input type="radio" name="ke8" value="0" />
    <div id="qs8" class="bl" style="dis:bl">T
    </div>
    <input type="radio" name="ke8" value="1" />
    <div id="an8" class="bl" style="dis:bl">F
    </div>
    </div>
    </div>
    </div>
    </div>
    </body></html>

XSLT I tried as per suggestions:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xhtml="http://www.w3.org/1999/xhtml"
     xmlns="http://www.w3.org/1999/xhtml"
     exclude-result-prefixes="xhtml">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
    
    <xsl:template match="xhtml:div[@id='12']/@class">
      <xsl:attribute name="class">WZ</xsl:attribute>
       <xsl:attribute name="id">56</xsl:attribute> 
    </xsl:template>
    
    <xsl:template match="xhtml:div[@id='rah']">
      <xsl:attribute name="class">pa</xsl:attribute>
      <xsl:attribute name="value">50</xsl:attribute>
    </xsl:template>
    
    <xsl:template match="xhtml:div[@class='iD2']">
       <xsl:copy>
        <xsl:apply-templates select="@*|node()"/> 
       </xsl:copy>
    <div id="qs8" class="bl" style="dis:bl">XYZ</div>
    <br/>
       <input type="radio" name="ke8" value="0" />
    <div id="an8" class="bl" style="dis:bl">T</div>
       <input type="radio" name="ke8" value="1" />
    <div id="tab8" class="bl" style="dis:bl">F</div>
    </xsl:template>
      
    </xsl:stylesheet>










Copying node with its replaced children using XSLT
===

I have to copy the node with a particular attribute and then replace its children with the particular literals. I'm able to change the value of the class attribute from <div class="LR" to class="WZ" but not able to change the value of id in the first div tag just after body.

I want to copy any <div class="iD2"> with its replaced children as per requirement. By Going though the suggestions/solutions in the forums I tried to do the job but when I copy nodes where class="iD2" then it copies those children also which I don't want and also <br> tag appears as <br clear="none"/> in the output. Please tell me how can I achieve the required output.

Input file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>
    <body>
    
    <div class="LR" id="12">
    <div class="id1">
    <div class="iD2">
    <div id="rah" class="bl" style="dis:bl"></div>
    
    <div class="cl">
    <br/><br/>
    <div id="ta12" class="bl" style="dis:bl">1<span style="font-family: times;"> TEXT</span> </div>
    <br/>T <input type="radio" name="op12" id="t12" attr="event()"/> 
    F <input type="radio" name="op12" id="f12" attr="event()""/>
    <div>Right </div>
    <div>Wrong </div>
    </div>
    
    </div>
    </div>
    </div>
    </body></html>

Desired Output file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html;  charset=utf-8" />
    </head>
    <body>
    <div class="WZ" id="56">
    <div class="id1">
    <div class="iD2">
    <div class="pa" value="50"></div>

    <div class="cl">
    <div id="ta8" class="bl" style="dis:bl">TEXT
    </div><br/>
    <input type="radio" name="ke8" value="0" />
    <div id="qs8" class="bl" style="dis:bl">T
    </div>
    <input type="radio" name="ke8" value="1" />
    <div id="an8" class="bl" style="dis:bl">F
    </div>
    </div>
    </div>
    </div>
    </div>
    </body></html>

XSLT I tried as per suggestions:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xhtml="http://www.w3.org/1999/xhtml"
     xmlns="http://www.w3.org/1999/xhtml"
     exclude-result-prefixes="xhtml">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
    
    <xsl:template match="xhtml:div[@id='12']/@class">
      <xsl:attribute name="class">WZ</xsl:attribute>
       <xsl:attribute name="id">56</xsl:attribute> 
    </xsl:template>
    
    <xsl:template match="xhtml:div[@id='rah']">
      <xsl:attribute name="class">pa</xsl:attribute>
      <xsl:attribute name="value">50</xsl:attribute>
    </xsl:template>
    
    <xsl:template match="xhtml:div[@class='iD2']">
       <xsl:copy>
        <xsl:apply-templates select="@*|node()"/> 
       </xsl:copy>
    <div id="qs8" class="bl" style="dis:bl">XYZ</div>
    <br/>
       <input type="radio" name="ke8" value="0" />
    <div id="an8" class="bl" style="dis:bl">T</div>
       <input type="radio" name="ke8" value="1" />
    <div id="tab8" class="bl" style="dis:bl">F</div>
    </xsl:template>
      
    </xsl:stylesheet>









