Re: [xsl] removing nodes to parent based on all child nodes not having text value

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
At 2008-12-05 09:32 -0600, Cindy Lard wrote:
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.

Fine, that's a one line change, and the result is below.


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.

Yes it wasn't too confusing. Having the example helps.


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.

I hope the code below helps. All I changed was the match criteria for the elements that are preserved. It is all in how you say it! :{)}


. . . . . . . . . Ken


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>

</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/>
<LastName/>
<Middle/>
<Suffix/>
</Individual>
<Business>
<MailingAddress>
<Address>
<MailingAddressType/>
<MailingAddressLine1>123 Street</MailingAddressLine1>
<MailingAddressLine2/>
</Address>
</MailingAddress>
</Business>
</Entity>
</Registrant>
</Registrants>
</Registration>


T:\ftemp>call xslt cindy1.xml cindy.xsl cindy1.out

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>

<Business>
<MailingAddress>
<Address>
<MailingAddressType/>
<MailingAddressLine1>123 Street</MailingAddressLine1>
<MailingAddressLine2/>
</Address>
</MailingAddress>
</Business>
</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