Reputation: 709
I have to remove a particular attribute from the entire XML file while transforming it into other XML using XSLT. I have to remove 'onclick' event from the entire document, whenever it occurs.
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="iDev">
<div class="q">
T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/>
</div>
<div class="q">
T <input type="radio" name="o1" id="t1" onclick="getFeedback()" />
</div>
</div>
</body>
</html>
My XSLT: I tried the following way to remove this attribute (After identity template):
<xsl:template match="xhtml:body//xhtml:input/@onclick />
In some cases it removed the 'onclick' event but when I passed one more template for changing the values of 'name' and 'id' attributes and adding one more attribute inside input tag then this 'onclick' event remains as it is. Please help me out in sorting out this problem. Thanking you!
Upvotes: 2
Views: 2743
Reputation: 243469
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="x">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@onclick"/>
<xsl:template match="x:input">
<xsl:element name="input" namespace="http://www.w3.org/1999/xhtml">
<xsl:attribute name="name">
<xsl:value-of select="concat('n',substring(@name,2))"/>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:value-of select="concat('i',substring(@id,2))"/>
</xsl:attribute>
<xsl:attribute name="someNew">newVal</xsl:attribute>
<xsl:apply-templates select=
"@*[not(name()='name' or name()='id')] | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="iDev">
<div class="q"> T
<input type="radio" name="o0" id="t0" onclick="getFeedback()"/>
</div>
<div class="q"> T
<input type="radio" name="o1" id="t1" onclick="getFeedback()" />
</div>
</div>
</body>
</html>
alters the name
and id
attribute and adds one new attribute to each input
element. It also "deletes" the onclick
attribute:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<div class="iDev">
<div class="q"> T
<input name="n0" id="i0" someNew="newVal" type="radio"/>
</div>
<div class="q"> T
<input name="n1" id="i1" someNew="newVal" type="radio"/>
</div>
</div>
</body>
</html>
Upvotes: 4