Home > Net >  XSLT - Remove parent element based on attribute value
XSLT - Remove parent element based on attribute value

Time:04-11

Here is my xml snippet:

<?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. The rest element (e.g., Platform) and any empty element should be excluded.

Expected 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:

<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. How can I remove the entire parent <mri:descriptiveKeywords/> if the keyword is not of type 'Parameter'?

CodePudding user response:

How about:

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>
  • Related