我想删除xsl创建的元素。我在创建元素后检查子项。例如
<projectRisks> (element to be removed if it empty after creation)
<for-each risk>
<if riskid=projid >
COPY RISK
</if >
</foreach>
<projectRisks>
完成此操作后,如果projectRisks没有子项,我需要将其删除。 我尝试了这里提供的所有解决方案。但是,由于在xsl中创建的projectRisks,无法删除它。
这里我提供了示例XML
<addProjectDetail>
<uniqueProjectId>142</uniqueProjectId>
</addProjectDetail>
<addProjectDetail>
<uniqueProjectId>915</uniqueProjectId>
</addProjectDetail>
<addProjectDetail>
<uniqueProjectId>100</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>142</uniqueProjectId>
<agencyProjectRiskId>852</agencyProjectRiskId>
</addProjectRisk>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>853</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
结果xml看起来像
<addProjectDetail>
<uniqueProjectId>142</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>142</uniqueProjectId>
<agencyProjectRiskId>852</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
<addProjectDetail>
<uniqueProjectId>915</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>853</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
<addProjectDetail>
<uniqueProjectId>100</uniqueProjectId>
</addProjectDetail>
在上面的xml中,有3个代码,142,915和100。 匹配142和915有2个风险,100个没有风险 因此,对于100,它不应该创建addProjectRisks元素
答案 0 :(得分:0)
您可以使用此变换,我已使用模式,以便简化当前(未显示)样式表中的集成:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="ProjectRiskById" match="addProjectRisk" use="uniqueProjectId"/>
<xsl:template match="addProjectDetail">
<xsl:copy-of select="."/>
<xsl:apply-templates select="key('ProjectRiskById',uniqueProjectId)[1]"
mode="add"/>
</xsl:template>
<xsl:template match="addProjectRisk" mode="add">
<addProjectRisks>
<xsl:copy-of select="key('ProjectRiskById',uniqueProjectId)"/>
</addProjectRisks>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
当应用于以下输入样本时(更改为通过添加最顶层元素使其格式良好并使其覆盖更多情况):
<xml>
<addProjectDetail>
<uniqueProjectId>142</uniqueProjectId>
</addProjectDetail>
<addProjectDetail>
<uniqueProjectId>915</uniqueProjectId>
</addProjectDetail>
<addProjectDetail>
<uniqueProjectId>100</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>142</uniqueProjectId>
<agencyProjectRiskId>852</agencyProjectRiskId>
</addProjectRisk>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>853</agencyProjectRiskId>
</addProjectRisk>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>857</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
</xml>
制作此片段:
<addProjectDetail>
<uniqueProjectId>142</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>142</uniqueProjectId>
<agencyProjectRiskId>852</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
<addProjectDetail>
<uniqueProjectId>915</uniqueProjectId>
</addProjectDetail>
<addProjectRisks>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>853</agencyProjectRiskId>
</addProjectRisk>
<addProjectRisk>
<uniqueProjectId>915</uniqueProjectId>
<agencyProjectRiskId>857</agencyProjectRiskId>
</addProjectRisk>
</addProjectRisks>
<addProjectDetail>
<uniqueProjectId>100</uniqueProjectId>
</addProjectDetail>