使用第二个 xml 文档进行 xslt 转换
xslt transform using second xml document
我有一个 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]">
我试过的一切都失败了。
想知道是否有人可以提供帮助!
谢谢!
鉴于这两个 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>
将return:
<?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>
呈现为:
我有一个 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]">
我试过的一切都失败了。 想知道是否有人可以提供帮助! 谢谢!
鉴于这两个 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>
将return:
<?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>
呈现为: