[英]XSLT - Remove all Attributes Except for Specified Attributes
I am trying to find a way to remove all attributes in an XML document aside from a few specified ones. 我试图找到一种方法来删除XML文档中除一些指定属性之外的所有属性。 I am able to remove one attribute from a specified element, but I am unable to remove all attributes (minus the ones I want to keep) from all elements in the document.
我可以从指定的元素中删除一个属性,但是无法从文档中的所有元素中删除所有属性(减去要保留的属性)。
For example: if i want to keep only id and class attributes, 例如:如果我只想保留id和class属性,
this input: 此输入:
<body>
<div id="div1" class="hello" length="1">inner text</div>
<span id="div2" class="bye" length="2">inner text</span>
<ol id="div3" class="goodbye" length="3">inner text</ol>
</body>
should be this output: 应该是这样的输出:
<body>
<div id="div1" class="hello">inner text</div>
<span id="div2" class="bye">inner text</span>
<ol id="div3" class="goodbye">inner text</ol>
<body>
xslt: xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="org.dita.dost.util.GenUtils" exclude-result-prefixes="java">
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@id | @class| node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you're only using a single template, it should be: 如果仅使用单个模板,则应为:
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@id | @class"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
What you have now is applied only to the body
element, and it copies all its descendants as is . 您现在拥有的内容仅应用于
body
元素,它按原样复制其所有后代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.