XSLT - 根据属性值删除父元素

XSLT - Remove parent element based on attribute value

这是我的 xml 片段:

<?xml version="1.0" encoding="UTF-8"?>
<data>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">air pressure</Anchor>
        </keyword>
        <keyword>
            <Anchor title="UOM" role="uri" type="simple" href="" show="new">Millibar</Anchor>
        </keyword>
        <keyword>
            <Anchor title="Platform" role="uri" type="simple" href="" show="new">Station 1</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">air temperature</Anchor>
        </keyword>
        <keyword>
            <Anchor title="UOM" role="uri" type="simple" href="" show="new">Degree Celsius</Anchor>
        </keyword>
        <keyword/>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">relative humidity</Anchor>
        </keyword>
        <keyword/>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword/>
        <keyword>
            <Anchor title="Platform" role="uri" type="simple" href="" show="new">Station 2</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword/>
        <keyword>
            <Anchor title="Platform" role="uri" type="simple" href="" show="new">Station 3</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
</data>

我想转换 xml 只包含参数和 UOM 关键字。应排除其余元素(例如,Platform)和任何空元素。

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<data>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">air pressure</Anchor>
        </keyword>
        <keyword>
            <Anchor title="UOM" role="uri" type="simple" href="" show="new">Millibar</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">air temperature</Anchor>
        </keyword>
        <keyword>
            <Anchor title="UOM" role="uri" type="simple" href="" show="new">Degree Celsius</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
<descriptiveKeywords>
    <MD_Keywords uuid="Data_Group">
        <keyword>
            <Anchor title="Parameter" role="uri" type="simple" href="" show="new">relative humidity</Anchor>
        </keyword>
    </MD_Keywords>
</descriptiveKeywords>
</data>

我的 xslt:

<xsl:stylesheet version="2.0">
<xsl:template match="descriptiveKeywords/MD_Keywords[@uuid = 'Data_Group']">
    <xsl:choose>
        <xsl:when test="keyword/Anchor[@xlink:title = ('Parameter', 'UOM')]">
            <xsl:element name="MD_Keywords">
                <xsl:attribute name="uuid">Data_Group</xsl:attribute>
                <xsl:element name="keyword">
                    <xsl:apply-templates select="keyword/Anchor[@xlink:title = 'Parameter']"/>
                </xsl:element>
                <xsl:variable name="uom" select="keyword/Anchor[@xlink:title = 'UOM']"/>
                <xsl:if test="$uom">
                    <xsl:element name="keyword">
                        <xsl:apply-templates select="keyword/Anchor[@xlink:title = 'UOM']"/>
                    </xsl:element>
                </xsl:if>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <!-- how to remove descriptiveKeywords here-->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

上面的 xslt 为“平台”的最后一个关键字生成了两个空的 。如果关键字不是 'Parameter' 类型,如何删除整个父级 ?

怎么样:

XSLT 2.0

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

<xsl:variable name="eligible-keywords" select="//keyword[Anchor/@title='Parameter' or Anchor/@title='UOM']"/>

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

<xsl:template match="(descriptiveKeywords | MD_Keywords | keyword)[not(descendant-or-self::keyword intersect $eligible-keywords)]"/>

</xsl:stylesheet>