Xml 使用XSL忽略空元素

Xml 使用XSL忽略空元素,xml,xslt,Xml,Xslt,我有一个XML工作流,我们从XML源构建输出文档,但是一些数据元素是空的,但仍然出现在输出中。问题是,我们正在根据最终输出生成段落返回和间距 在某些情况下,数据包含和元素。什么时候我们希望它看起来像这样(A): 123主街60060号 当元素中没有数据时,我们希望它以这种方式显示(B): 123主街M-F上午9:00-下午5:00 邮政信箱12360060 但是,XML包含空的数据元素,如,因此我们会出现以下情况(C): 123主街M-F上午9:00-下午5:00 60060 我们的XSL

我有一个XML工作流,我们从XML源构建输出文档,但是一些数据元素是空的,但仍然出现在输出中。问题是,我们正在根据最终输出生成段落返回和间距

在某些情况下,数据包含
元素。什么时候我们希望它看起来像这样(A):

123主街60060号
元素中没有数据时,我们希望它以这种方式显示(B):

123主街M-F上午9:00-下午5:00
邮政信箱12360060
但是,XML包含空的数据元素,如
,因此我们会出现以下情况(C):

123主街M-F上午9:00-下午5:00
60060
我们的XSL在遇到空元素之前工作正常。我确信有一种方法可以创建选项a,即使存在空元素。我试着使用
,但无法使其工作。我想去掉空的
元素,并将
元素上移到上一行

以下是我当前的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>

以下是XML数据源:

<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>
Ames国家公司测试。19758020-0135-0405第五大街RS:M-F 8-5 50010传真:(515)663-3033(515)232-6251网址:www.amesnational.complete见多银行控股公司部分
西行525 main 50010-6008传真:(515)232-3791(515)232-8664西行西德梅因办事处
迈克尔·桑德尔,英国医学博士
银行家信托公司1510七叶树50010(515)233-4424网址:www.bankerstrust.com银行家信托公司办公室,得梅因
约翰·拉塞尔,副总裁
交易所国家银行0739-0950-7823惠勒,Ste 3250010传真:(515)232-5068(515)232-5060电子邮件:ames@esb1.comOffice科林斯州立银行外汇管理局
艾莉森·阿佩尔,CPA副总裁克里斯汀·海因茨,AVP
美国第一银行。19560739-0080-71530圣达夫大道南1小时:M-TH 9-5 SAT 8-12 50010传真:(515)956-3160(515)233-2033网址:美国第一银行道奇堡办事处
史蒂夫·古德休,注册主席

您使用
字符串长度
函数的方法还不错,但可能您将其插入了错误的位置

替换XSLT行时,
元素位于前一行

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


这是你想要的吗?如果没有指定
,我不太确定您是否也要跳过

更新:如果
为空,要对元素应用不同的排序,请使用具有类似上述条件的
结构:

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


然后,您可以完全跳过
的模板。您提供给我们的信息中似乎存在许多不一致之处,但简单的解决方案是添加一个模板规则:

<xsl:template match="address2[not(child::node())]"/>


只是想知道(A)和(B)是交换的吗?此外,您的XML数据源的格式不正确(应该以
而不是
结尾)。此外,我不确定为什么对您来说,空
元素的结果包括
元素-对我来说不是这样。请注意正确的外壳。Xml是区分大小写的,因此
(在您的代码片段中)与
(在完整的Xml文档中)不同。对于这些小错误,我很抱歉,为了简单起见,我从一个更大的文件中提取了Xml,并重新添加了父标记,并引入了一些打字错误。实际的XML是格式良好的。这非常有效。伟大的就像他们说的餐馆,地点,地点,地点。。。。关于一个相关主题,如果
元素为空,我的老板希望zip显示在
之间。对我们如何设置该条件有什么建议吗?现在,它出现在
@JimMaivald之后:在以
开头的模板中,以固定顺序插入各种元素(使用
xsl:apply templates>
)。这些元素之一是
元素。尝试使用
检查
是否为空,如果为空,请按不同顺序插入元素。如果这些信息不够,请告诉我。我从未使用xsl:if函数。如果address2存在,我是否创建一个模板,如果address2在“否则”部分中丢失,我是否嵌套另一个模板?另外,如果
元素列在“xsl:if”语句中,我是否应该从上面提到的“match”部分删除它们?@ORMapper测试
是否存在且不为空的“xsl:if”或“xsl:Choose”的语法是什么?它是否与您建议的上面的“匹配”相同?@ORMapper如果存在
,则当
为空或缺失时,它应该出现在
之后,例如:
123 Main Str 60060 M-F 9:00am-5:00pm
对于小错误,非常抱歉。XML实际上格式良好,但为了节省空间,我只插入了它的一部分。谢谢你的回复
<xsl:template match="Address2"><xsl:text>
<xsl:template match="Address2[string-length() != 0]"><xsl:text>
<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>
<xsl:template match="address2[not(child::node())]"/>