我有一个几乎完成的 XML 工作流。我还有一个问题要解决,我的老板刚刚扔给我,但我很难过。源 XML 包含四个元素来显示银行的地址和营业时间:<Address1>
、<Address2>
和。<Zip>
<Hours>
当所有元素都存在并包含数据时,元素顺序应显示为 (A):
<Address1>123 Main St</Address1> <Hours>M-F 9:00am-5:00pm</Hours><xsl:text>
</xsl:text><Address2>PO Box 2345</Address2> <Zip>60050</Zip>
如果<Address2>
缺少或为空,则元素的顺序应为(B):
<Address1>123 Main St</Address1> <Zip>60050</Zip> <Hours>M-F 9:00am-5:00pm</Hours>
请注意 Zip 现在是如何移动到出现在<Address1>
. 这就是难倒我的地方。我试过使用“选择”但没有成功。有什么建议么?
这是当前的 XSL:
<?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>
这是一些示例 XML:
<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: 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></Story</Root>