开发者

Removing empty node created by XSL

开发者 https://www.devze.com 2023-04-05 20:15 出处:网络
I want remove an element created by xsl. I am checking for children after creating an element .For example

I want remove an element created by xsl. I am checking for children after creating an element .For example

<projectRisks>    (element to be removed if it empty after creation)

 <for-each risk>

  <if riskid=projid >

      COPY RISK

  </if >

 </foreach>

<projectRisks>

After completing this, if there are no children for projectRisks, i need to remove it. I tried all the solutions provided here. But, as the projectRisks created here in xsl, not able to remove it.

here im providing sample 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>

Resultant xml looks like

   <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>
 <addProj开发者_运维问答ectDetail>
<uniqueProjectId>100</uniqueProjectId>
</addProjectDetail>

In the above xml, there 3 codes, 142, 915 and 100. Matching to 142 and 915 there 2 risks and for 100 there are no risks So for 100 it should not create a addProjectRisks element


You can use this transform, for which I've used modes so that it simplifies integration in your current (not shown) stylesheet:

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

When applied on the following input sample (changed to make it well-formed by adding a top-most element and to make it cover more cases):

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

Produces this fragment:

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

精彩评论

暂无评论...
验证码 换一张
取 消