![](/img/trans.png)
[英]XSLT 1.0 : Remove element based on attribute of the element matching an attribute of the parent
[英]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>
我想转换仅包含参数和 UOM 关键字的 xml。 应排除 rest 元素(例如,Platform)和任何空元素。
预计 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>
我的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 生成了两个空的 <mri:descriptiveKeywords/> 用于 'Platform. 如果关键字不是“参数”类型,如何删除整个父级 <mri:descriptiveKeywords/>?
怎么样:
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.