【发布时间】:2015-09-15 07:28:54
【问题描述】:
我有这个输入 XML:
<mods:relatedItem type="constituent">
<mods:name type="corporate">
<mods:namePart>Financijer projekta</mods:namePart>
</mods:name>
<mods:name type="corporate">
<mods:namePart/>
</mods:name>
</mods:name>
<mods:namePart/>
</mods:name>
</mods:name>
<mods:namePart>Program financiranja</mods:namePart>
</mods:name>
<mods:identifier>Šifra projekta</mods:identifier>
<mods:identifier/>
<mods:titleInfo>
<mods:title>Naziv projekta</mods:title>
</mods:titleInfo>
<mods:titleInfo>
<mods:title/>
</mods:titleInfo>
<mods:titleInfo type="alternative">
<mods:title>Akronim projekta</mods:title>
</mods:titleInfo>
<mods:titleInfo type="alternative">
<mods:title/>
</mods:titleInfo>
</mods:relatedItem>
我想去掉所有空的元素,以及它们的父节点。
最终的 XML 应该如下所示:
<mods:relatedItem type="constituent">
<mods:name type="corporate">
<mods:namePart>Financijer projekta</mods:namePart>
</mods:name>
</mods:name>
<mods:namePart>Program financiranja</mods:namePart>
</mods:name>
<mods:identifier>Šifra projekta</mods:identifier>
<mods:titleInfo>
<mods:title>Naziv projekta</mods:title>
</mods:titleInfo>
<mods:titleInfo type="alternative">
<mods:title>Akronim projekta</mods:title>
</mods:titleInfo>
</mods:relatedItem>
我尝试进行转换,但从未让删除空元素部分起作用
<xsl:template match="/">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="/mods:mods/mods:relatedItem/mods:name[@type]/mods:namePart[not(string(.))]"/>
<xsl:template match="/mods:mods/mods:relatedItem/mods:name[not(@type)]/mods:namePart[not(string(.))]"/>
<xsl:template match="/mods:mods/mods:relatedItem/mods:identifier[not(string(.))]"/>
<xsl:template match="/mods:mods/mods:relatedItem/mods:titleInfo[not(@type)]/mods:title[not(string(.))]"/>
<xsl:template match="/mods:mods/mods:relatedItem/mods:titleInfo[@type]/mods:title[not(string(.))]"/>
【问题讨论】:
标签: xslt