gpt4 book ai didi

xml - 使用 XSLT 从整个 XML 文件中删除属性

转载 作者:行者123 更新时间:2023-12-04 18:15:50 25 4
gpt4 key购买 nike

在使用 XSLT 将其转换为其他 XML 时,我必须从整个 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>

更改 nameid属性并为每个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com