如果子元素不存在,则删除父元素

时间:2014-10-01 09:45:22

标签: xslt-1.0

在下面的文件中,如果属性UMProductId的值为“Radiation Therapy”,我想删除/删除CPT元素。

            <?xml version="1.0" encoding="us-ascii"?>
            <AuthBatch SchemaVersion="1.0" InsurerId="123" Sequence="110" CreateDate="2014-07-10T02:45:02.317" AuthorizationCount="3">
                <Authorizations>
                    <Authorization EffectiveDate="2014-07-09" Id="12345">
                        <CPTs>
                            <CPT Sequence="1" UMProductId="Radiation Therapy" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                            <CPT Sequence="2" UMProductId="Radiation Therapy" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                            <CPT Sequence="3" UMProductId="Radiation Therapy" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                        </CPTs>
                        <Program Id="46756" Name="test" ChangeStatus="ADD" />
                    </Authorization>  
                    <Authorization EffectiveDate="2014-07-09" Id="12345">
                        <CPTs>
                            <CPT Sequence="1" UMProductId="test" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                            <CPT Sequence="2" UMProductId="Radiation Therapy" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                            <CPT Sequence="3" UMProductId="Radiation Therapy" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                        </CPTs>
                        <Program Id="46756" Name="test" ChangeStatus="ADD" />
                    </Authorization>  
                   </Authorizations>
            </AuthBatch>

但是,在一个授权中,如果所有CPT / UMProductId值都是“放射治疗”,那么我想删除整个授权本身。

下面是我的XSLT,它删除了CPT元素,但没有删除授权:

            <xsl:stylesheet
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

               <xsl:template match="@* | node()">

                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                    </xsl:copy>

                </xsl:template>

                <xsl:template match="AuthBatch/Authorizations/Authorization/CPTs/CPT[@UMProductId='RADIATION THERAPY' or
                    @UMProductId='Radiation Therapy']"/>

                <xsl:template match="AuthBatch/Authorizations/Authorization[not(CPTs/CPT)]"/>

            </xsl:stylesheet>

MyOutput中:

            <?xml version="1.0" encoding="UTF-8"?>
            <?xml version="1.0" encoding="us-ascii"?>
            <AuthBatch SchemaVersion="1.0" InsurerId="123" Sequence="110" CreateDate="2014-07-10T02:45:02.317" AuthorizationCount="3">
                <Authorizations>
                    <Authorization EffectiveDate="2014-07-09" Id="12345">
                        <CPTs/>
                        <Program Id="46756" Name="test" ChangeStatus="ADD" />
                    </Authorization>  
                    <Authorization EffectiveDate="2014-07-09" Id="4567">
                        <CPTs>
                            <CPT Sequence="1" UMProductId="test" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                        </CPTs>
                        <Program Id="46756" Name="test" ChangeStatus="ADD" />
                    </Authorization>  
                </Authorizations>
            </AuthBatch>

预期产出:

        <?xml version="1.0" encoding="UTF-8"?>
        <?xml version="1.0" encoding="us-ascii"?>
        <AuthBatch SchemaVersion="1.0" InsurerId="123" Sequence="110" CreateDate="2014-07-10T02:45:02.317" AuthorizationCount="3">
            <Authorizations>

                <Authorization EffectiveDate="2014-07-09" Id="4567">
                    <CPTs>
                        <CPT Sequence="1" UMProductId="test" ChangeStatus="ADD" ResendIndicator="INITIAL" />
                    </CPTs>
                    <Program Id="46756" Name="test" ChangeStatus="ADD" />
                </Authorization>  
            </Authorizations>
        </AuthBatch>

请帮帮我。在此先感谢!!。

谢谢, 阿贾伊

1 个答案:

答案 0 :(得分:0)

尝试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- suppress Authorizations where all CPT nodes are "Radiation Therapy" -->    
<xsl:template match="Authorization[not(CPTs/CPT/@UMProductId!='Radiation Therapy')]"/>
<!-- suppress CPTs whose value is "Radiation Therapy" -->    
<xsl:template match="CPT[@UMProductId='Radiation Therapy']"/>

</xsl:stylesheet>