我必须从整个 XML 文件中删除特定属性,同时使用 XSLT 将其转换为其他 XML。无论何时发生,我都必须从整个文档中删除“onclick”事件。
输入文件:
<?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>
我的 XSLT: 我尝试了以下方法来删除此属性(在身份模板之后):
<xsl:template match="xhtml:body//xhtml:input/@onclick />
在某些情况下,它删除了“onclick”事件,但是当我传递了另一个模板来更改“name”和“id”属性的值并在输入标记内添加了另一个属性时,此“onclick”事件保持不变是。 请帮我解决这个问题。 谢谢你!
最佳答案
这种转变:
<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>
应用于提供的 XML 文档时:
<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>
更改 name
和 id
属性,并向每个 input
元素添加一个新属性。它还“删除”onclick
属性:
<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>
关于xml - 使用 XSLT 从整个 XML 文件中删除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750508/