Ignoring empty elements with XSL

TheGreatMelon

New Member
I have an XML workflow where we're building an output document from an XML source but some of the data elements are empty but still appearing in the output. The problem is that we are generating paragraph returns and spacing based on the final output.In some cases the data contains \[code\]<address1>\[/code\] and \[code\]<address2>\[/code\] elements. When it does we want it to look like this (A):\[code\]<address1>123 Main Str</address1><xsl:text> </xsl:text><zip>60060</zip>\[/code\]When does not have data in the \[code\]<address2>\[/code\] element, we want it to appear this way (B): \[code\]<address1>123 Main Str</address1> <hours>M-F 9:00am - 5:00pm</hours><xsl:text> </xsl:text><address2>PO Box 123</address2> <zip>60060</zip>\[/code\]BUT, the XML contains EMPTY data elements such as \[code\]<address2></address2>\[/code\] so we end up with the following situation (C):\[code\]<address1>123 Main Str</address1> <hours>M-F 9:00am - 5:00pm</hours><xsl:text> </xsl:text><address2/><xsl:text> </xsl:text><zip>60060</zip>\[/code\]Our XSL works fine until it hits an empty element. I'm sure there's a way to create option A even when there is an empty element. I tried using \[code\]<xsl:if test="string-length(node) != 0">\[/code\] but I couldn't get it to work. I want to get rid of the empty \[code\]<address2/>\[/code\] elements and move the \[code\]<zip>\[/code\] element up to the previous line.Here is my current XSL:\[code\]<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="IndividualBanks_2011 final.xml" --><!DOCTYPE xsl:stylesheet><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:strip-space elements="*"/><xsl:output method="xml"/><xsl:template match="/"><Root><Story><xsl:apply-templates select="Root"/></Story></Root></xsl:template><xsl:template match="BankName | Address1 | Hours | Established | RoutingNbr | CO/CityOfficePhone | CO/CityOfficeAddress2 "><xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element></xsl:template><xsl:template match="BK"><xsl:apply-templates select="BankName"/><xsl:text> </xsl:text><xsl:apply-templates select="Established"/> <xsl:text> </xsl:text><xsl:apply-templates select="RoutingNbr"/><xsl:text></xsl:text><xsl:apply-templates select="OfficeOfLabel"/><xsl:apply-templates select="Address1"/><xsl:text> </xsl:text><xsl:apply-templates select="Hours"/><xsl:apply-templates select="Address2"/><xsl:apply-templates select="Zip"/></xsl:template><xsl:template match="Address2"><xsl:text></xsl:text><Address2><xsl:value-of select="."/></Address2><xsl:text> </xsl:text></xsl:template><xsl:template match="Zip"><Zip><xsl:value-of select="."/></Zip><xsl:text></xsl:text></xsl:template></xsl:stylesheet>\[/code\]Here is the XML data source:\[code\]<Root><BK><BankName>Ames National Corporation</BankName><Established>Est. 1975</Established><RoutingNbr>8020-0135-0</RoutingNbr><Address1>405 5th Street</Address1><Hours>Hrs: M-F 8-5</Hours><Address2></Address2> <Zip>50010</Zip><Fax>FAX: (515) 663-3033</Fax><Phone>(515) 232-6251</Phone><WebURL>Web: www.amesnational.com</WebURL><MultiBankLabel>Please see Multi-Bank Holding Companies section</MultiBankLabel></BK><BK><BankName>Bank of the West</BankName><Address1>525 Main</Address1><Zip>50010-6008</Zip><Fax>FAX: (515) 232-3791</Fax><Phone>(515) 232-8664</Phone><OfficeOfLabel>Office of Bank of the West, West Des Moines</OfficeOfLabel><EH><Employee>Michael Sondall, BM</Employee></EH></BK><BK><BankName>Bankers Trust Company</BankName><Address1>1510 Buckeye </Address1><Zip>50010</Zip><Phone>(515) 233-4424</Phone><WebURL>Web: www.bankerstrust.com</WebURL><OfficeOfLabel>Office of Bankers Trust Company, Des Moines</OfficeOfLabel><EH><Employee>John Russell, VP</Employee></EH></BK><BK><BankName>Exchange State Bank</BankName><RoutingNbr>0739-0950-7</RoutingNbr><Address1>823 Wheeler, Ste 32</Address1><Zip>50010</Zip><Fax>FAX: (515) 232-5068</Fax><Phone>(515) 232-5060</Phone><Email>e-Mail: [email protected]</Email><OfficeOfLabel>Office of Exchange State Bank, Collins</OfficeOfLabel><EH><Employee>Allison Appel, VP, CPA</Employee></EH><EH><Employee>Christine Heintz, AVP</Employee></EH></BK><BK><BankName>First American Bank</BankName><Established>Est. 1956</Established><RoutingNbr>0739-0080-7</RoutingNbr><Address1>1530 S Duff Avenue, Ste 1</Address1><Hours>Hrs: M-TH 9-5 SAT 8-12</Hours><Address2> </Address2><Zip>50010</Zip><Fax>FAX: (515) 956-3160</Fax><Phone>(515) 233-2033</Phone><WebURL>Web: </WebURL> <OfficeOfLabel>Office of First American Bank, Fort Dodge</OfficeOfLabel><EH><Employee>Steve Goodhue, Reg Pres</Employee></EH></BK></Root>\[/code\]
 
Back
Top