I want to change from one XML (XHTML) file to another using XSLT. In the new XML file I have to remove/add/modify some elements. So for that I created one identity.xsl
file, which copies the entire source file and then I created a new XSLT which includes identity.xsl
and then in that new XSLT I'm trying to do the modifications. I'm able to eliminate a few attributes, which are not required, by passing a template match which does nothing but I'm unable to add the new attributes in the existing tags and also unable to add the new elements at the specific location (with closing tags at particular location).
My Original 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 id="o">
<div id="nd">
<p>1</p>
</div>
<div class="TF id="id12">
<element1 name="abc" src="abc.jpg"></script>
<input type="radio" id="1" event="xyz">
<div class="q">
<br/>
<div id="ta3" class="block">
<span style="a">ABC</span>
</div>
<br/>T <input/> F <input/>
<div id="sf">
<div id="ta3">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Required 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>
<!--HAVE TO AD THESE TWO ELEMENTS-->
<element add="xyz" id="23">
<element add="xyz" id="24">
<!--ADD ATTRIBUTES IN BODY TAG-->
<body onLoad="ada" bgcolor="pink">
<div id="o">
<div id="nd">
<p>1</p>
</div>
<div class="TF id="id12">
<!--HAVE TO UPATE THE VALUE OF SRC ATTRIBUTE -->
<element1 name="abc" src="xyz.jpg"></script>
<!--ADD THIS FORM ELEMENT WITH ATTRIBUTE-->
<form name="form">
<input type="radio" id="1" event="xyz">
<div class="q">
<br/>
<div id="ta3" class="block">
<span style="a">ABC</span>
</div>
<br/>T
<!--ADD TABLE/TR/TD TAG-->
<table>
<tr>
<td>
<input/>
</td>
</tr>
<tr>
</td>
F <input/>
</td>
</tr>
</table>
<div id="sf">
<div id="ta3">
</div>
</div>
</div>
<!--ADD INPUT TAG-->
<input type="submit" value="Done"/>
</div>
</div>
<!--CLOSE FORM TAG-->
</form>
</div>
</body>
</html>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Import the identity transformation. -->
<xsl:import href="identity.xsl"/>
<xsl:template match="body">
<body>
<xsl:apply-templates select="body">
</xsl:apply-templates>
</body>
</xsl:template>
<xsl:template match="body">
<body onLoad="ada" bgcolor="pink"></body>
</xsl:template>
<!--REMOVES THE MATCHING ATTRIBUTE and DOES THE JOB-->
<xsl:template match="@attr"> </xsl:template>
<xsl:template match="input">
<xsl:element name="input">
<xsl:attribute name="type">submit</xsl:attribute>
<xsl:attribute name="value">Done</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>