Note that there are some explanatory texts on larger screens.

plurals
  1. POXML to XML with XSLT- Add, Remove, Modify Elements and Attributes
    primarykey
    data
    text
    <p>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 <code>identity.xsl</code> file, which copies the entire source file and then I created a new XSLT which includes <code>identity.xsl</code> 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).</p> <p>My Original file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="o"&gt; &lt;div id="nd"&gt; &lt;p&gt;1&lt;/p&gt; &lt;/div&gt; &lt;div class="TF id="id12"&gt; &lt;element1 name="abc" src="abc.jpg"&gt;&lt;/script&gt; &lt;input type="radio" id="1" event="xyz"&gt; &lt;div class="q"&gt; &lt;br/&gt; &lt;div id="ta3" class="block"&gt; &lt;span style="a"&gt;ABC&lt;/span&gt; &lt;/div&gt; &lt;br/&gt;T &lt;input/&gt; F &lt;input/&gt; &lt;div id="sf"&gt; &lt;div id="ta3"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Required file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt; &lt;/head&gt; &lt;!--HAVE TO AD THESE TWO ELEMENTS--&gt; &lt;element add="xyz" id="23"&gt; &lt;element add="xyz" id="24"&gt; &lt;!--ADD ATTRIBUTES IN BODY TAG--&gt; &lt;body onLoad="ada" bgcolor="pink"&gt; &lt;div id="o"&gt; &lt;div id="nd"&gt; &lt;p&gt;1&lt;/p&gt; &lt;/div&gt; &lt;div class="TF id="id12"&gt; &lt;!--HAVE TO UPATE THE VALUE OF SRC ATTRIBUTE --&gt; &lt;element1 name="abc" src="xyz.jpg"&gt;&lt;/script&gt; &lt;!--ADD THIS FORM ELEMENT WITH ATTRIBUTE--&gt; &lt;form name="form"&gt; &lt;input type="radio" id="1" event="xyz"&gt; &lt;div class="q"&gt; &lt;br/&gt; &lt;div id="ta3" class="block"&gt; &lt;span style="a"&gt;ABC&lt;/span&gt; &lt;/div&gt; &lt;br/&gt;T &lt;!--ADD TABLE/TR/TD TAG--&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;input/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;/td&gt; F &lt;input/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div id="sf"&gt; &lt;div id="ta3"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;!--ADD INPUT TAG--&gt; &lt;input type="submit" value="Done"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;!--CLOSE FORM TAG--&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>XSLT:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;!-- Import the identity transformation. --&gt; &lt;xsl:import href="identity.xsl"/&gt; &lt;xsl:template match="body"&gt; &lt;body&gt; &lt;xsl:apply-templates select="body"&gt; &lt;/xsl:apply-templates&gt; &lt;/body&gt; &lt;/xsl:template&gt; &lt;xsl:template match="body"&gt; &lt;body onLoad="ada" bgcolor="pink"&gt;&lt;/body&gt; &lt;/xsl:template&gt; &lt;!--REMOVES THE MATCHING ATTRIBUTE and DOES THE JOB--&gt; &lt;xsl:template match="@attr"&gt; &lt;/xsl:template&gt; &lt;xsl:template match="input"&gt; &lt;xsl:element name="input"&gt; &lt;xsl:attribute name="type"&gt;submit&lt;/xsl:attribute&gt; &lt;xsl:attribute name="value"&gt;Done&lt;/xsl:attribute&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload