Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 12/5/2008 3:55:00 PM
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent