Xslt从输入xml中排除少量动态加载的属性

时间:2017-04-14 08:16:02

标签: java xml xslt

我需要编写xsl来从输入xml中排除一些属性。

注意:排除属性将使用动态加载 java中的transformer.setParameter(" attribiutes"," A,B")方法。

我的输入xml:

<root>
 <child1 A="" B="" C="" />
</root>

下面是我正在使用的xsl,但它不起作用。

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="atttributes" select="''" />
<xsl:template match="@*|node()">
    <xsl:choose>
        <xsl:when test="contains($atttributes, @*)">
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

结果应该类似于下面的xml。

<root>
 <child1 C="" />
</root>

请帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

正如Tim C在他删除的答案中指出的那样,您希望仅将测试限制为属性,并按原样复制所有其他节点 。并且您希望测试检查属性的名称,而不是内容。并且您希望通过在比较中包含分隔符来消除误报。

以这种方式尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="atttributes" select="'A,B'" />

<xsl:template match="node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*">
    <xsl:if test="not(contains(concat(',', $atttributes, ','), concat(',', name(), ',')))">
        <xsl:copy/>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
相关问题