Subject: Re: [xsl] removing nodes to parent based on all child nodes not having text value From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Fri, 05 Dec 2008 10:54:38 -0500 |
Thank you all so much for your help. The example data was not correct as you pointed out. The input and desired output should have been as follows:
Input ... Output ...
In this case there only needs to be either <Individual> or <Business> under <Entity>. Either the registration is for an individual or business. Both should not exist in the same file. Because the business registration information is being pulled from a legacy system certain information required in the XML may not be available, such as <MailingAddressType/> and <MailingAddressLine2/> as in the preceding example. However, if any descendant element of a child has a text value such as <MailingAddressLine1>123 Street</MailingAddressLine1> it's parent and siblings all need to be copied even if those siblings have no text value the nodes need to be copied.
If no descendant elements have a text value as is the case with <Individual> that node and all descendant nodes of that element need to be stripped out. Wow, I hope that wasn't too confusing.
All the templates I have used thus far will strip out any element with no text value, which removes the needed (dare I say required) elements of <MailingAddressType/> and <MailingAddressLine2/> under the <Address> element.
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">
<!--preserve all elements whose descendant has text or who has a parent that has a leaf child with text--> <xsl:template match="*[.//*[normalize-space(.)] or ../*[not(*) and 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>
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>
-- 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 -> |
---|---|---|
Re: [xsl] removing nodes to parent , Cindy Lard | Thread | Re: [xsl] removing nodes to parent , Cindy Lard |
RE: [xsl] Keep-with-previous.within, NINN (Niels Nygaard | Date | [xsl] [XSL] Typo in the XSLT2.0 nor, Alain |
Month |