您可以使用以下样式表:
输入:
$more input.xml
<?xml version="1.0"?>
<a>
<b Ccy="123" name="test1" BadAttyNameTest="toRemove1" BadAttyNameTestt="toRemovee1" other="hey1">toto</b>
<b Ccy="456" name="test2" BadAttyNameTest="toRemove2" BadAttyNameTestt="toRemovee2" other="hey2">abc</b>
<b Ccy="789" name="test3" BadAttyNameTest="toRemove3" BadAttyNameTestt="toRemovee3" other="hey3">uvw</b>
</a>
联合:
::::::::::::::
inputUnion.xsl
::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="@Ccy | @name"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出联合:
$xsltproc inputUnion.xsl input.xml
<a>
<b Ccy="123" name="test1">toto</b>
<b Ccy="456" name="test2">abc</b>
<b Ccy="789" name="test3">uvw</b>
</a>
只会复制@Ccy | @name属性的并集,其他属性不考虑。
除了:
::::::::::::::
inputNOT.xsl
::::::::::::::
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="@*[not(starts-with(name(),'BadAttyName'))]"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出除外:
$xsltproc inputNOT.xsl input.xml
<a>
<b Ccy="123" name="test1" other="hey1">toto</b>
<b Ccy="456" name="test2" other="hey2">abc</b>
<b Ccy="789" name="test3" other="hey3">uvw</b>
</a>
语法@*[not(starts-with(name(),'BadAttyName'))] 将采用满足括号中条件的所有属性。条件是所有不以BadAttyName开头的元素,这是通过组合not()和starts-with()创建的。