English 中文(简体)
XSL 条件总数
原标题:XSL Conditional Totalling
  • 时间:2010-12-08 21:58:47
  •  标签:
  • xslt

能否在Xsl进行有条件的合计?

我有以下xml样本:

<?xml version="1.0" encoding="utf-8"?>
<export>
<stats set="1">
  <columns>
    <column id="0">
      <sum>100</sum>
    </column>
    <column id="1">
      <sum>102</sum>
    </column>
    <column id="2">
      <sum>12</sum>
    </column>
  </columns>
</stats>
  <stats set="2">
    <columns>
      <column id="0">
        <sum>100</sum>
      </column>
      <column id="1">
        <sum>101</sum>
      </column>
      <column id="2">
        <sum>19</sum>
      </column>
    </columns>
  </stats>
</export>

可否计算每个统计组各栏中不等于一栏的总数? 因此,它将产生以下产出:

               Set 1       Set 2       Diff(Set 1 - Set 2)
Total (Diff)   114         120           -6
column 2       102         101            1  
column 3       12          19            -7 

因此,在产出第1栏中,由于两个统计组的总和相同,将予以删除。

我可以拿我的xsl来输出一栏,这些栏目是不同的,但却无法保证如何将这些栏目全部.起来,并放入总行。

非常感谢

Andez

最佳回答

This transformation (64 lines):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kColByPosAndVal" match="column"
  use="concat(count(preceding-sibling::*),
               + ,
              sum)"/>
 <xsl:key name="kIdByVal" match="column/@id"
  use="."/>

 <xsl:template match="/*">
  <xsl:variable name="vsum1" select=
   "sum(stats[@set=1]/*/column
                  [not(key( kColByPosAndVal ,
                            concat(count(preceding-sibling::*),
                                   + ,
                                   sum)
                           )[2]
                       )])"/>
  <xsl:variable name="vsum2" select=
   "sum(stats[@set=2]/*/column
                  [not(key( kColByPosAndVal ,
                            concat(count(preceding-sibling::*),
                                   + ,
                                   sum)
                           )[2]
                       )])"/>

                 Set 1          Set 2       Diff(Set 1 - Set 2)
  Total (Diff)   <xsl:text/>
  <xsl:value-of select="$vsum1"/>
  <xsl:text>             </xsl:text>
  <xsl:value-of select="$vsum2"/>
  <xsl:text>             </xsl:text>
  <xsl:value-of select="$vsum1 -$vsum2"/>
  <xsl:text>&#xA;</xsl:text>

  <xsl:for-each select=
   "*/*/column/@id
       [generate-id()
       = generate-id(key( kIdByVal ,.)[1])
       ]
       [not(key( kColByPosAndVal ,
                concat(count(../preceding-sibling::*),
                        + ,
                       ../sum)
                )[2]
            )]">
    <xsl:variable name="vcolSet1" select=
     "/*/stats[@set=1]/*/column[@id=current()]/sum"/>

    <xsl:variable name="vcolSet2" select=
     "/*/stats[@set=2]/*/column[@id=current()]/sum"/>
  Column <xsl:value-of select=".+1"/><xsl:text/>
    <xsl:text>       </xsl:text>
    <xsl:value-of select="$vcolSet1"/>
    <xsl:text>             </xsl:text>
    <xsl:value-of select="$vcolSet2"/>
    <xsl:text>             </xsl:text>
    <xsl:value-of select="$vcolSet1 -$vcolSet2"/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在应用XML文件时:

<export>
<stats set="1">
  <columns>
    <column id="0">
      <sum>100</sum>
    </column>
    <column id="1">
      <sum>102</sum>
    </column>
    <column id="2">
      <sum>12</sum>
    </column>
  </columns>
</stats>
  <stats set="2">
    <columns>
      <column id="0">
        <sum>100</sum>
      </column>
      <column id="1">
        <sum>101</sum>
      </column>
      <column id="2">
        <sum>19</sum>
      </column>
    </columns>
  </stats>
</export>

produces the wanted, correct result:

                 Set 1          Set 2       Diff(Set 1 - Set 2)
  Total (Diff)   114             120             -6

  Column 2       102             101             1

  Column 3       12             19             -7

<>Explanation:

  1. www.un.org/Depts/DGACM/index_spanish.htm 关键名称为kColByPosAndVal,用于选择具有特定职位(所有<代码>>>columnsiblings)和其sum儿童要素的特定价值的所有栏目。

  2. www.un.org/Depts/DGACM/index_spanish.htm 在Muenchian集团中使用了“kIdByVal的关键名称,以找到id上的所有不同数值。

  3. www.un.org/Depts/DGACM/index_spanish.htm 这两栏的计算方法仅包括<代码>k的栏目。 ColByPosAndVal 关键选择只有一个<代码>column要素 (如果它选择两个<代码>column要素,这两个要素都处于相同地位,并具有相同的<代码>sum/code>。)

  4. www.un.org/Depts/DGACM/index_spanish.htm 其余部分应易于理解。

问题回答

暂无回答




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签