我有一个xml数据文件,其中包含大量重复字段,每个字段与大约10个唯一设施名称相关联,如下所示:
<Dailyreport>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>North</facility>
<ispass>0</ispass>
</msg>
<msg>
<msgdate>05/27/2015</msgdata>
<facility>South</facility>
<ispass>1</ispass>
</msg>
</Dailyreport>
我有一个XSL样式表版本1.0正在工作,我可以按设施获取出现次数,如下所示:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<table style="margin-left:auto;margin-right:auto" rules="all" border="1">
<xsl:if test="Dailyreport//msg[facility='North']">
<tr><td>North Building:</td></tr>
<tr><td>Total:<xsl:value-of select="count(Dailyreport/msg[facility='North'])"/></td></tr>
<tr><td>Pass:<xsl:value-of select="count(Dailyreport/msg[facility='North' and ispass='1'])"/></td></tr>
<tr><td>Fail:<xsl:value-of select="count(DailyELRreport/msg[facility='North' and ispass='0'])"/></td></tr>
<tr><td>-------------------</td></tr>
<tr><td>
</td></tr>
</xsl:if>
</table>
</html>
</xsl:template>
</xsl:stylesheet>
但是,为了获得所有可能设施的计数,我必须使用每个设施名称重复(xsl:if test)部分。
我想看看我是否可以在第二个xml数据文件中找到设施名称,并使用document()函数通过将它们加载到全局参数中来迭代它们,然后使用调用模板函数来反复调用一个单独的(xsl:if test)部分。并让它使用参数值而不是固定的设施名称......如下所示:
<xsl:if test="Dailyreport//msg[facility=$sender]">
我尝试的一切都失败了。 想知道是否有人可以提供帮助! 谢谢!
答案 0 :(得分:1)
鉴于这两个XML文档:
<强> facilities.xml 强>
<facilities>
<facility code="North">North Building</facility>
<facility code="South">South Building</facility>
</facilities>
XML(这是由XSLT处理的文档)
<Dailyreport>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>North</facility>
<ispass>1</ispass>
</msg>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>North</facility>
<ispass>1</ispass>
</msg>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>North</facility>
<ispass>0</ispass>
</msg>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>South</facility>
<ispass>0</ispass>
</msg>
<msg>
<msgdate>05/27/2015</msgdate>
<facility>South</facility>
<ispass>0</ispass>
</msg>
</Dailyreport>
下面的样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="msg" match="msg" use="facility" />
<xsl:template match="/">
<xsl:variable name="report" select="." />
<table border="1">
<xsl:for-each select="document('facilities.xml')/facilities/facility">
<tr>
<th><xsl:value-of select="."/></th>
</tr>
<xsl:variable name="code" select="@code" />
<!-- switch context back to XML document -->
<xsl:for-each select="$report">
<xsl:variable name="messages" select="key('msg', $code)" />
<tr>
<td>Total:<xsl:value-of select="count($messages)"/></td>
</tr>
<tr>
<td>Pass:<xsl:value-of select="count($messages[ispass='1'])"/></td>
</tr>
<tr>
<td>Fail:<xsl:value-of select="count($messages[ispass='0'])"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
将返回:
<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
<tr>
<th>North Building</th>
</tr>
<tr>
<td>Total:3</td>
</tr>
<tr>
<td>Pass:2</td>
</tr>
<tr>
<td>Fail:1</td>
</tr>
<tr>
<th>South Building</th>
</tr>
<tr>
<td>Total:2</td>
</tr>
<tr>
<td>Pass:0</td>
</tr>
<tr>
<td>Fail:2</td>
</tr>
</table>
呈现为: