Ignoring empty elements with XSL

2019-07-19 12:03发布

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 <address1> and <address2> elements. When it does we want it to look like this (A):

<address1>123 Main Str</address1><xsl:text> </xsl:text><zip>60060</zip>

When does not have data in the <address2> element, we want it to appear this way (B):

<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>

BUT, the XML contains EMPTY data elements such as <address2></address2> so we end up with the following situation (C):

<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>

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 <xsl:if test="string-length(node) != 0"> but I couldn't get it to work. I want to get rid of the empty <address2/> elements and move the <zip> element up to the previous line.

Here is my current XSL:

<?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>

Here is the XML data source:

<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: ames@esb1.com</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>

标签: xml xslt
2条回答
乱世女痞
2楼-- · 2019-07-19 12:53

There seem to be numerous inconsistencies in the information you have given us, but the simple solution is to add a template rule:

<xsl:template match="address2[not(child::node())]"/>
查看更多
别忘想泡老子
3楼-- · 2019-07-19 13:02

Your approach with the string-length function wasn't bad, but maybe you inserted it in the wrong place?

The <Zip> element is on the previous line for me when I replace your XSLT line

<xsl:template match="Address2"><xsl:text>

with

<xsl:template match="Address2[string-length() != 0]"><xsl:text>

Does that do what you want? I'm not quite sure whether you also want to skip the <Hours> if there's no <Address2> specified.

Update: In order to apply a different ordering to the elements if <Address2> is empty, use an <xsl:choose> structure with a similar condition as above:

<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:choose>
<xsl:when test="string-length(Address2) != 0">
<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:when>
<xsl:otherwise>
<xsl:apply-templates select="Address1"/><xsl:text>   </xsl:text><xsl:apply-templates select="Zip"/><xsl:text>  </xsl:text><xsl:apply-templates select="Hours"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

You can then completely skip the template for <Address2>.

查看更多
登录 后发表回答