根据XML中的条件从父节点中删除子节点

时间:2016-10-18 09:58:00

标签: xml xslt xslt-1.0

我正在尝试根据条件删除父节点中的子节点。但只删除了值而不是元素标记。我还要删除元素标签。

XML:

 <?xml version="1.0" encoding="UTF-8"?>
    <DM CT="REJECT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <atict:info tracking="on"      xmlns:atict="http://www.arbortext.com/namespace/atict"/>
    <atict:add user=""><SPECPARA>
    <WARNING VITAL="1">
    <PARA>abcdefghijk</PARA>
    </WARNING>
    </SPECPARA></atict:add>
    </DM>

当前输出:

<?xml version="1.0" encoding="UTF-8"?>
    <DM CT="REJECT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <atict:add user=""><SPECPARA>
    <WARNING VITAL="1">
    <PARA></PARA>
    </WARNING>
    </SPECPARA></atict:add>
    </DM>

期望的输出:

  <?xml version="1.0" encoding="UTF-8"?>
        <DM CT="REJECT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

        </DM>

XSLT 1.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" extension-element-prefixes="img" exclude-result-prefixes="atict">
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="atict:add//text()[ancestor::DM/@CT='REJECT']"/>
</xsl:stylesheet>

如何根据条件删除所有子节点?

感谢您的支持。

1 个答案:

答案 0 :(得分:1)

我猜你想做:

XSLT 1.0

links = list()
...
# iteration happens here
link =  'https://' + host + ':' + console_port + '...'
print link
links.append(link)
# script done here; return or use links