【问题标题】:xslt transform using second xml document使用第二个 xml 文档的 xslt 转换
【发布时间】:2015-05-27 19:16:39
【问题描述】:

我有一个 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]">

我尝试过的一切都失败了。 想知道是否有人可以提供帮助! 谢谢!

【问题讨论】:

    标签: xslt-1.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>
    

    渲染为:

    【讨论】:

    • 对不起....页面上似乎没有任何对象指定关闭问题的操作。我该怎么做?
    • 我认为我不需要再采取行动了。至少我什么都看不到。我不能投票,因为我没有代表。我检查了绿色检查。
    猜你喜欢
    • 2012-01-12
    • 2012-03-11
    • 1970-01-01
    • 2020-11-22
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2011-08-29
    相关资源
    最近更新 更多