简体   繁体   English

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

[英]XSLT - Remove parent element based on attribute value

Here is my xml snippet:这是我的 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>

I want to transform the xml with only Parameter and UOM keyword are included.我想转换仅包含参数和 UOM 关键字的 xml。 The rest element (eg, Platform) and any empty element should be excluded.应排除 rest 元素(例如,Platform)和任何空元素。

Expected output:预计 output:

<?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>

My xslt:我的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>

The xslt above generate two empty <mri:descriptiveKeywords/> for the last keywords of 'Platform.上面的 xslt 生成了两个空的 <mri:descriptiveKeywords/> 用于 'Platform. How can I remove the entire parent <mri:descriptiveKeywords/> if the keyword is not of type 'Parameter'?如果关键字不是“参数”类型,如何删除整个父级 <mri:descriptiveKeywords/>?

How about:怎么样:

XSLT 2.0 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM