Home > other >  How to remove all parent nodes if a child node value contains a specific string using XSLT?
How to remove all parent nodes if a child node value contains a specific string using XSLT?

Time:03-31

I have an XML document where if the emailAddress tag contains '@test' then the entire User and its child nodes need to be deleted.

Here is the XML.

<Users>
    <User>
        <empInfo>
            <EmpEmployment>
                <personNav>
                    <PerPerson>
                        <personalInfoNav>
                            <PerPersonal>
                                <firstName>Gr</firstName>
                                <lastName>Coll</lastName>
                                <secondLastName>Gre</secondLastName>
                                <middleName>M</middleName>
                                <thirdName>Cos</thirdName>
                            </PerPersonal>
                        </personalInfoNav>
                        <customString1>NetID6</customString1>
                        <personEmpTerminationInfoNav>
                            <PersonEmpTerminationInfo>
                                <latestTerminationDate/>
                            </PersonEmpTerminationInfo>
                        </personEmpTerminationInfoNav>
                        <phoneNav>
                            <PerPhone>
                                <phoneType>5892</phoneType>
                                <extension/>
                                <phoneNumber>4882928200</phoneNumber>
                                <isPrimary>true</isPrimary>
                                <phoneTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business Mobile</localeLabel>
                                    </PicklistOption>
                                </phoneTypeNav>
                            </PerPhone>
                        </phoneNav>
                        <emailNav>
                            <PerEmail>
                                <emailAddress>[email protected]</emailAddress>
                                <emailTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business</localeLabel>
                                    </PicklistOption>
                                </emailTypeNav>
                                <emailType>5877</emailType>
                                <isPrimary>true</isPrimary>
                            </PerEmail>
                        </emailNav>
                    </PerPerson>
                </personNav>
            </EmpEmployment>
        </empInfo>
    </User>
    <User>
        <empInfo>
            <EmpEmployment>
                <personNav>
                    <PerPerson>
                        <personalInfoNav>
                            <PerPersonal>
                                <firstName>Gr</firstName>
                                <lastName>Coll</lastName>
                                <secondLastName>Gre</secondLastName>
                                <middleName>M</middleName>
                                <thirdName>Cos</thirdName>
                            </PerPersonal>
                        </personalInfoNav>
                        <customString1>NetID6</customString1>
                        <personEmpTerminationInfoNav>
                            <PersonEmpTerminationInfo>
                                <latestTerminationDate/>
                            </PersonEmpTerminationInfo>
                        </personEmpTerminationInfoNav>
                        <phoneNav>
                            <PerPhone>
                                <phoneType>5892</phoneType>
                                <extension/>
                                <phoneNumber>4882928200</phoneNumber>
                                <isPrimary>true</isPrimary>
                                <phoneTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business Mobile</localeLabel>
                                    </PicklistOption>
                                </phoneTypeNav>
                            </PerPhone>
                        </phoneNav>
                        <emailNav>
                            <PerEmail>
                                <emailAddress>[email protected]</emailAddress>
                                <emailTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business</localeLabel>
                                    </PicklistOption>
                                </emailTypeNav>
                                <emailType>5877</emailType>
                                <isPrimary>true</isPrimary>
                            </PerEmail>
                        </emailNav>
                    </PerPerson>
                </personNav>
            </EmpEmployment>
        </empInfo>
    </User>
</Users>

This XSLT transformation does this but only works on the entire string in the emailAddress tag, how can I get it to work only if the string contains specific text inside it?

Here is the XSLT

<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="User[not(.//emailAddress = '[email protected]')]" />
</xsl:stylesheet>

Expected Output

<Users>
    <User>
        <empInfo>
            <EmpEmployment>
                <personNav>
                    <PerPerson>
                        <personalInfoNav>
                            <PerPersonal>
                                <firstName>Gr</firstName>
                                <lastName>Coll</lastName>
                                <secondLastName>Gre</secondLastName>
                                <middleName>M</middleName>
                                <thirdName>Cos</thirdName>
                            </PerPersonal>
                        </personalInfoNav>
                        <customString1>NetID6</customString1>
                        <personEmpTerminationInfoNav>
                            <PersonEmpTerminationInfo>
                                <latestTerminationDate/>
                            </PersonEmpTerminationInfo>
                        </personEmpTerminationInfoNav>
                        <phoneNav>
                            <PerPhone>
                                <phoneType>5892</phoneType>
                                <extension/>
                                <phoneNumber>4882928200</phoneNumber>
                                <isPrimary>true</isPrimary>
                                <phoneTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business Mobile</localeLabel>
                                    </PicklistOption>
                                </phoneTypeNav>
                            </PerPhone>
                        </phoneNav>
                        <emailNav>
                            <PerEmail>
                                <emailAddress>[email protected]</emailAddress>
                                <emailTypeNav>
                                    <PicklistOption>
                                        <externalCode>B</externalCode>
                                        <localeLabel>Business</localeLabel>
                                    </PicklistOption>
                                </emailTypeNav>
                                <emailType>5877</emailType>
                                <isPrimary>true</isPrimary>
                            </PerEmail>
                        </emailNav>
                    </PerPerson>
                </personNav>
            </EmpEmployment>
        </empInfo>
    </User>
</Users>

CodePudding user response:

This XSLT transformation does this but only works on the entire string in the emailAddress tag, how can I get it to work only if the string contains specific text inside it?

Try changing this…

<xsl:template match="User[not(.//emailAddress = '[email protected]')]" />

to this…

<xsl:template match="User[not(.//emailAddress[contains(.,'@test')]]" />

CodePudding user response:

To delete that node() you should use the contains function and remove the not(), because then all these nodes will fall in that match doing nothing and therefore being deleted.

<xsl:template match="User[.//emailAddress[contains(.,'@test']]" />
  • Related