Subject: Re: [xsl] removing nodes to parent based on all child nodes not having text value From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 03 Dec 2008 17:00:01 -0500 |
I am new to XSLT and I am trying to clean up an XML file built from a mainframe generated text file that contains all possible elements for all nodes. In the new XML I only need to copy the nodes that actually have elements that have text values. Is there a way to test the deepest child of an element for text value and then copy all it's ancestors back to the root? For example, in the attached input under the element <Entity> only <Individual> or <Business> should exist and not both. If any child elements have text value for the element <Individual> all child nodes should be copied, while the <Business> element and all it's child nodes should not be copied to the new XML file. See example input and output files.
Input
<Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual> <Prefix/> <FirstName></FirstName>/> <LastName></LastName>/>
<Middle/> <Suffix/> </Individual> <Business> <MailingAddress> <Address> <MailingAddressType/> <MailingAddressLine1>123 Street</MailingAddressLine1>
<MailingAddressLine2/> </Address> </MailingAddress> </Business> </Entity> </Registrant> </Registrants> </Registration>
Output
<Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual> <Prefix/> <FirstName>Joe</FirstName>/> <LastName>Doe</LastName>/> <Middle/> <Suffix/> </Individual> </Entity> </Registrant> </Registrants> </Registration>
T:\ftemp>type cindy.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--find all elements that have non-space descendent text characters--> <xsl:template match="*[normalize-space(.)]"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template>
<xsl:template match="*"> <!--do nothing for elements that don't have a text value descendant--> </xsl:template>
<xsl:template match="@*"><!--identity for all other nodes--> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
</xsl:stylesheet> T:\ftemp>type cindy1.xml <Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual> <Prefix/> <FirstName></FirstName>/> <LastName></LastName>/> <Middle/> <Suffix/> </Individual> <Business> <MailingAddress> <Address> <MailingAddressType/> <MailingAddressLine1>123 Street</MailingAddressLine1> <MailingAddressLine2/> </Address> </MailingAddress> </Business> </Entity> </Registrant> </Registrants> </Registration>
T:\ftemp>type cindy1.out <?xml version="1.0" encoding="utf-8"?><Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual>
/> />
</Individual> <Business> <MailingAddress> <Address>
</Address> </MailingAddress> </Business> </Entity> </Registrant> </Registrants> </Registration> T:\ftemp>type cindy2.xml <Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual> <Prefix/> <FirstName>Joe</FirstName> <LastName>Doe</LastName> <Middle/> <Suffix/> </Individual> <Business> <MailingAddress> <Address> <MailingAddressType/> <MailingAddressLine1></MailingAddressLine1> <MailingAddressLine2/> </Address> </MailingAddress> </Business> </Entity> </Registrant> </Registrants> </Registration>
T:\ftemp>type cindy2.out <?xml version="1.0" encoding="utf-8"?><Registration> <UserName>DID1901</UserName> <ActivityType>25</ActivityType> <ActivityDate>2008-10-30T12:10:26</ActivityDate> <RegistrationCounty>19</RegistrationCounty> <Registrants> <Registrant> <Entity> <Individual>
<FirstName>Joe</FirstName> <LastName>Doe</LastName>
</Entity> </Registrant> </Registrants> </Registration> T:\ftemp>rem Done!
-- Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes: : Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] removing nodes to parent base, Cindy Lard | Thread | Re: [xsl] removing nodes to parent , Vasu Chakkera |
[xsl] removing nodes to parent base, Cindy Lard | Date | Re: [xsl] automatic creation of nes, Ganesh Babu N |
Month |