XSLT 2.0动态表计数

时间:2013-12-07 17:15:30

标签: xml function xslt

我无法将XML转换中的正确输出生成到html表中。

输入XML:

    <Report>
      <Report>
        <objective objective="Y1">
          <function function="X1" />
          <function function="X3" />
          <function function="X4" />
          <function function="X2" />
          <function function="X6" />
        </objective>

        <objective objective="Y2" />

        <objective objective="Y3" />

        <objective objective="Y4">
          <function function="X5" />
          <function function="X3" />
        </objective>
        <objective objective="Y5">
          <function function="X2" />
          <function function="X1" />
          <function function="X4" />
        </objective>
      </Report>
    </Report>

通缉输出:

enter image description here

这是我到目前为止所拥有的;我的XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">



<xsl:template match ="Report/Report" >
<html>
    <head>
    <title></title>
    </head>
    <body>

            <table>
            <tbody>
                <tr>
                    <th>Objective\Function</th>

                    <xsl:for-each-group select="objective/function" group-by="@function">
                    <xsl:sort select="current-grouping-key()"/>

                    <th><xsl:value-of select="current-grouping-key()"/></th>
                     </xsl:for-each-group>
                </tr>
                    <xsl:variable name="allData" select="objective"/>
                    <xsl:for-each select="objective">
                <tr>

                        <td><xsl:value-of select="@objective"></xsl:value-of></td>                          

                        <xsl:for-each-group select="$allData" group-by="function/@function">

                             <xsl:sort select="current-grouping-key()"/>

                                  <td><xsl:value-of select="count(current-grouping-key())"/></td>

                        </xsl:for-each-group>

                </tr>

                </xsl:for-each>

            </tbody>
            </table>

    </body>
    </html>

</xsl:template>
</xsl:stylesheet>

我认为我很接近,我认为必须使用count函数完成一些事情:

enter image description here

基本上我想要的是:

如果属于函数节点的@function与父元素@objective相关,则计数1(取决于事件),否则为0.我提前感谢你,过去几天一直在苦苦挣扎。

1 个答案:

答案 0 :(得分:1)

目标元素的当前 xsl:for-each 循环中,您可以定义一个变量来保存对当前元素的引用

<xsl:for-each select="objective">
   <xsl:variable name="current" select="." />

然后,在第二个 xsl:for-each-group 中迭代函数元素,您可以简单地计算函数的数量当前目标(保存在变量中)与分组键匹配的元素(因此将为0或1)

<xsl:value-of select="count($current[function/@function=current-grouping-key()])"/>

试试这个XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
      exclude-result-prefixes="xs fn">
<xsl:template match ="Report/Report" >
<html>
    <head>
    <title></title>
    </head>
    <body>
            <table>
            <tbody>
                <tr>
                    <th>Objective\Function</th>
                    <xsl:for-each-group select="objective/function" group-by="@function">
                    <xsl:sort select="current-grouping-key()"/>
                    <th><xsl:value-of select="current-grouping-key()"/></th>
                     </xsl:for-each-group>
                </tr>
                    <xsl:variable name="allData" select="objective"/>
                    <xsl:for-each select="objective">
                    <xsl:variable name="current" select="." />
                <tr>
                        <td><xsl:value-of select="@objective"></xsl:value-of></td>                          
                        <xsl:for-each-group select="$allData" group-by="function/@function">
                             <xsl:sort select="current-grouping-key()"/>
                             <td><xsl:value-of select="count($current[function/@function=current-grouping-key()])"/></td>
                        </xsl:for-each-group>
                </tr>
                </xsl:for-each>
            </tbody>
            </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>

这会产生以下输出

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title></title>
   </head>
   <body>
      <table>
         <tbody>
            <tr>
               <th>Objective\Function</th>
               <th>X1</th>
               <th>X2</th>
               <th>X3</th>
               <th>X4</th>
               <th>X5</th>
               <th>X6</th>
            </tr>
            <tr>
               <td>Y1</td>
               <td>1</td>
               <td>1</td>
               <td>1</td>
               <td>1</td>
               <td>0</td>
               <td>1</td>
            </tr>
            <tr>
               <td>Y2</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
            </tr>
            <tr>
               <td>Y3</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
               <td>0</td>
            </tr>
            <tr>
               <td>Y4</td>
               <td>0</td>
               <td>0</td>
               <td>1</td>
               <td>0</td>
               <td>1</td>
               <td>0</td>
            </tr>
            <tr>
               <td>Y5</td>
               <td>1</td>
               <td>1</td>
               <td>0</td>
               <td>1</td>
               <td>0</td>
               <td>0</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>