Change element order only when one element is empty or missing

ScriptKing

New Member
I have an XML workflow that is nearly complete. I have one issue left to resolve that my boss just threw at me, but I am stumped. The source XML contains four elements to display a bank's address and hours of operation: \[code\]<Address1>\[/code\], \[code\]<Address2>\[/code\], \[code\]<Zip>\[/code\] and \[code\]<Hours>\[/code\]. When all elements are present and contain data the element order should appear as (A):\[code\]<Address1>123 Main St</Address1> <Hours>M-F 9:00am-5:00pm</Hours><xsl:text>\[/code\]\[code\]</xsl:text><Address2>PO Box 2345</Address2> <Zip>60050</Zip>\[/code\]If \[code\]<Address2>\[/code\] is missing or empty the order of the elements should be as (B):\[code\]<Address1>123 Main St</Address1> <Zip>60050</Zip> <Hours>M-F 9:00am-5:00pm</Hours>\[/code\]Note how the Zip is now moved to appear after \[code\]<Address1>\[/code\]. This is what is stumping me. I've tried using "choose" without success. Any suggestions?Here is the current XSL:\[code\]<?xml version="1.0" encoding="UTF-8"?><!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/BK"/></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:apply-templates select="Phone"/><xsl:apply-templates select="Fax"/><xsl:text></xsl:text></xsl:template><xsl:template match="Address2[string-length() != 0]"><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 some sample XML:\[code\]<Root><Story><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></Story</Root>\[/code\]
 
Back
Top