使用xslt 2.0在属性和文本内容匹配时从xml中删除节点

时间:2018-05-20 18:42:42

标签: xml xslt xslt-2.0

我在转换xml时遇到了困难。这是我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <organisation>
        <school>
            <name>school of arts Berlin</name>
            <address>123street</address>
        </school>
    </organisation>
    <teachers>
        <wo_number>34A</wo_number>
        <publication>
            <date>14-09-2018</date>
            <name>J. doe</name>
        </publication>
        <teacher id="A254">
            <situation>
                <ill>yes</ill>
            </situation>
        </teacher>
        <teacher id="A254">
            <situation>
                <ill>no</ill>
            </situation>
        </teacher>
        <teacher id="B254">
            <situation>
                <ill>probable</ill>
            </situation>
        </teacher>
        <teacher id="X92">
            <situation>
                <ill>no</ill>
            </situation>
        </teacher>
        <teacher id="G56">
            <situation>
                <ill>probable</ill>
            </situation>
        </teacher>
        <teacher id="G56">
            <situation>
                <ill>yes</ill>
            </situation>
        </teacher>
    </teachers>
</root>

我想要实现的目标:

  • teacher元素有一个属性id,如果它以&#34; A2&#34;并且同一教师节点内元素的文本内容等于&#34;是&#34;必须删除教师节点
  • teacher元素有一个属性id,如果它以&#34; G5&#34;并且同一教师节点内元素ill的文本内容等于&#34;可能&#34;必须删除教师节点

正确的结果应该是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <organisation>
        <school>
            <name>school of arts Berlin</name>
            <address>123street</address>
        </school>
    </organisation>
    <teachers>
        <wo_number>34A</wo_number>
        <publication>
            <date>14-09-2018</date>
            <name>J. doe</name>
        </publication>
        <teacher id="A254">
            <situation>
                <ill>no</ill>
            </situation>
        </teacher>
        <teacher id="B254">
            <situation>
                <ill>probable</ill>
            </situation>
        </teacher>
        <teacher id="X92">
            <situation>
                <ill>no</ill>
            </situation>
        </teacher>
        <teacher id="G56">
            <situation>
                <ill>yes</ill>
            </situation>
        </teacher>
    </teachers>
</root>
到目前为止,我还没有能够实现这个目标。我被困在上面写的第一个(要求)bullit。这是我的xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--xsl:template match="teachers"-->
<xsl:output omit-xml-declaration="yes"/>

<xsl:param name="teacher-to-remove" select="'yes'"/>

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

<xsl:template match="teacher">
        <xsl:if test="(not(contains(concat(',', $teacher-to-remove, ','), concat(',', situation/ill, ','))) and not(starts-with(@id, 'A2')))">   
            <xsl:call-template name="identity"/>
        </xsl:if>
</xsl:template>

结果如下:

    <root>
        <organisation>
            <school>
                <name>school of arts Berlin</name>
                <address>123street</address>
            </school>
        </organisation>
        <teachers>
            <wo_number>34A</wo_number>
            <publication>
                <date>14-09-2018</date>
                <name>J. doe</name>
            </publication>


            <teacher id="B254">
                <situation>
                    <ill>probable</ill>
                </situation>
            </teacher>
            <teacher id="X92">
                <situation>
                    <ill>no</ill>
                </situation>
            </teacher>
            <teacher id="G56">
                <situation>
                    <ill>probable</ill>
                </situation>
            </teacher>

        </teachers>
    </root>

所有具有元素的教师节点 <ill>yes</ill>被删除,这是不正确的,所有ID为A254的教师节点都被删除,这也是不正确的。 xsl:if条件是否按照预期(或想要)的方式工作。一些帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

您可以使用两个空模板来实现此目的:

<xsl:template match="teacher[starts-with(@id,'A2') and situation/ill='yes']" />
<xsl:template match="teacher[starts-with(@id,'G5') and situation/ill='probable']" />

他们过滤掉了不需要的元素。