使用 XSL 忽略空元素 [英] Ignoring empty elements with XSL

查看:26
本文介绍了使用 XSL 忽略空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在某些情况下,数据包含 元素.当它出现时,我们希望它看起来像这样 (A):

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>

元素中没有数据时,我们希望它以这种方式出现(B):

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>

但是,XML 包含 EMPTY 数据元素,例如 <address2></address2> 所以我们最终会遇到以下情况 (C):

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>

我们的 XSL 工作正常,直到它遇到一个空元素.我确信即使有一个空元素,也有一种方法可以创建选项 A.我尝试使用 <xsl:if test="string-length(node) != 0"> 但我无法让它工作.我想去掉空的 元素并将 元素移到上一行.

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.

这是我当前的 XSL:

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>

这里是 XML 数据源:

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>

推荐答案

您使用 string-length 函数的方法还不错,但也许您将其插入了错误的位置?

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

当我替换您的 XSLT 行时,<Zip> 元素位于我的前一行

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

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

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

这篇关于使用 XSL 忽略空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆