Xml 在XSLT中为每个组选择不同的值

Xml 在XSLT中为每个组选择不同的值,xml,xslt,xpath,Xml,Xslt,Xpath,我在为每个组选择不同的值时遇到了一个问题。结果并没有显示在屏幕上。预期结果如下所示: <xsl:for-each-group select="Items/Item" group-by="ProviderName"> <xsl:sort select="current-grouping-key()"/> <tr> <th> <xsl:value-of select="current-gro

我在为每个组选择不同的值时遇到了一个问题。结果并没有显示在屏幕上。预期结果如下所示:

<xsl:for-each-group select="Items/Item" group-by="ProviderName">
    <xsl:sort select="current-grouping-key()"/>
    <tr>
        <th>
            <xsl:value-of select="current-grouping-key()"/>
        </th>
    </tr>
    <tr>
        <td>
            <xsl:text>Item Number</xsl:text>
        </td>
        <td>
            <xsl:text>Quantity</xsl:text>
        </td>
        <td>
            <xsl:text>Unit Price</xsl:text>
        </td>
        <td>
            <xsl:text>Total</xsl:text>
        </td>
    </tr>
    <xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
我在网上读过几个示例,最好的方法是使用
generate-id()
函数为每个组使用
循环。但我试过了,却没有得到积极的结果。这是源XML:

<Items>
    <Item ItemNumber="1148087">
        <ProductName>Dolby Metal-Black-Matte</ProductName>
        <ProviderName>Vestal Watches</ProviderName>
        <Quantity>4</Quantity>
        <UnitPrice>67.99</UnitPrice>
    </Item>
    <Item ItemNumber="1150197">
        <ProductName>Vercilli Blk-Tan</ProductName>
        <ProviderName>Boston Babes</ProviderName>
        <Quantity>1</Quantity>
        <UnitPrice>23.99</UnitPrice>
    </Item>
    <Item ItemNumber="1151464">
        <ProductName>Spritz Grape Seat and Extra Seat</ProductName>
        <ProviderName>Bambeano</ProviderName>
        <Quantity>1</Quantity>
        <UnitPrice>56.99</UnitPrice>
    </Item>
    <Item ItemNumber="1148087">
        <ProductName>Dolby Metal-Black-Matte</ProductName>
        <ProviderName>Vestal Watches</ProviderName>
        <Quantity>2</Quantity>
        <UnitPrice>67.99</UnitPrice>
    </Item>
    <Item ItemNumber="1150197">
        <ProductName>Vercilli Blk-Tan</ProductName>
        <ProviderName>Boston Babes</ProviderName>
        <Quantity>2</Quantity>
        <UnitPrice>23.99</UnitPrice>
    </Item>
    <Item ItemNumber="1150173">
        <ProductName>Lucille Tan</ProductName>
        <ProviderName>Boston Babes</ProviderName>
        <Quantity>1</Quantity>
        <UnitPrice>24.99</UnitPrice>
    </Item>
    <Item ItemNumber="1151464">
        <ProductName>Spritz Grape Seat and Extra Seat</ProductName>
        <ProviderName>Bambeano</ProviderName>
        <Quantity>1</Quantity>
        <UnitPrice>56.99</UnitPrice>
    </Item>
    <Item ItemNumber="1148089">
        <ProductName>Plexi Leather-Silver-Black</ProductName>
        <ProviderName>Vestal Watches</ProviderName>
        <Quantity>1</Quantity>
        <UnitPrice>189.99</UnitPrice>
    </Item>
</Items>

杜比金属黑亚光
维斯塔尔手表
4.
67.99
陈伟志
波士顿宝贝
1.
23.99
Spritz葡萄座椅和额外座椅
班比亚诺
1.
56.99
杜比金属黑亚光
维斯塔尔手表
2.
67.99
陈伟志
波士顿宝贝
2.
23.99
陈露西尔
波士顿宝贝
1.
24.99
Spritz葡萄座椅和额外座椅
班比亚诺
1.
56.99
树脂皮革银黑色
维斯塔尔手表
1.
189.99
请注意,源XML包含几个相同的
ProductName
元素,这些元素具有不同的
ItemNumber
属性。我希望将所有
项目
元素与类似的
项目编号
进行分组,并对数量进行求和。谢谢你们的帮助。非常感谢

示例输出为:

我认为您只需要为每组嵌套两个

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output method="html" version="5.0" indent="yes"/>

<xsl:template match="Items">
  <table>
    <xsl:for-each-group select="Item" group-by="ProviderName">
      <tbody>
        <tr>
          <th colspan="4">Provider: <xsl:value-of select="current-grouping-key()"/></th>
        </tr>
        <tr>
          <th>Item Number</th>
          <th>Quantity</th>
          <th>Unit Price</th>
          <th>Total</th>
        </tr>
      </tbody>
      <tbody>
        <xsl:for-each-group select="current-group()" group-by="@ItemNumber">
          <tr>
            <td><xsl:value-of select="current-grouping-key()"/></td>
            <td><xsl:value-of select="sum(current-group()/Quantity)"/></td>
            <td><xsl:value-of select="UnitPrice"/></td>
            <td><xsl:value-of select="format-number(sum(current-group()/(Quantity * UnitPrice)), '0.00')"/></td>
          </tr>
        </xsl:for-each-group>
      </tbody>
      <tbody>
        <tr>
          <th colspan="3">Subtotal</th>
          <td><xsl:value-of select="format-number(sum(current-group()/(Quantity * UnitPrice)), '0.00')"/></td>
        </tr>
      </tbody>
    </xsl:for-each-group>
      <tbody>
        <tr>
          <th colspan="3">Total</th>
          <td><xsl:value-of select="format-number(sum(Item/(Quantity * UnitPrice)), '0.00')"/></td>
        </tr>
      </tbody>
  </table>
</xsl:template>

</xsl:stylesheet>

供应商:
项目编号
量
单价
全部的
小计
全部的

请查看XSLT的修改版本:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output indent="yes"/>

  <xsl:template match="/">
    <table>
      <thead>
        <tr>
          <th>
            <xsl:text>Item Number</xsl:text>
          </th>
          <th>
            <xsl:text>Quantity</xsl:text>
          </th>
          <th>
            <xsl:text>Unit Price</xsl:text>
          </th>
          <th>
            <xsl:text>Total</xsl:text>
          </th>
        </tr>
      </thead>
      <tbody>
        <xsl:for-each-group select="Items/Item" group-by="@ItemNumber">
          <xsl:sort select="current-grouping-key()"/>
          <tr>
            <td>
              <xsl:value-of select="current-grouping-key()"/>
            </td>
            <td>
              <xsl:value-of select="sum(current-group()/Quantity)"/>
            </td>
            <td>
              <xsl:value-of select="current-group()/UnitPrice"/>
            </td>
            <td>
              <xsl:value-of
                select="sum(for $x in current-group() return $x/UnitPrice * $x/Quantity)"/>
            </td>
          </tr>
          <xsl:apply-templates select="current-group()"/>
        </xsl:for-each-group>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="Item">
    <xsl:value-of select="distinct-values(./ItemName)"/>
  </xsl:template>
</xsl:stylesheet>

项目编号
量
单价
全部的
输出:

<table>
   <thead>
      <tr>
         <th>Item Number</th>
         <th>Quantity</th>
         <th>Unit Price</th>
         <th>Total</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>1148087</td>
         <td>6</td>
         <td>67.99</td>
         <td>407.93999999999994</td>
      </tr>
      <tr>
         <td>1148089</td>
         <td>1</td>
         <td>189.99</td>
         <td>189.99</td>
      </tr>
      <tr>
         <td>1150173</td>
         <td>1</td>
         <td>24.99</td>
         <td>24.99</td>
      </tr>
      <tr>
         <td>1150197</td>
         <td>3</td>
         <td>23.99</td>
         <td>71.97</td>
      </tr>
      <tr>
         <td>1151464</td>
         <td>2</td>
         <td>56.99</td>
         <td>113.98</td>
      </tr>
   </tbody>
</table>

项目编号
量
单价
全部的
1148087
6.
67.99
407.93999999999994
1148089
1.
189.99
189.99
1150173
1.
24.99
24.99
1150197
3.
23.99
71.97
1151464
2.
56.99
113.98

如果您不解释此XSLT操作的源XML,我们将无能为力。很抱歉,我已经编辑了代码,感谢@abachconsive为您现在发布的XML输入示例发布您想要的输出。为什么要执行不同的值(./ItemName)
?您发布的示例没有任何
ItemName
元素,只有
ProductName
ProviderName
。RMIT计算机将拾取此页面,您的作业将至少少0%的作业,我可以标记
总和(对于当前组中的$x(),返回$x/单价*$x/数量)
可以缩短为
总和(当前-组()/(单价*数量))
。您好@MartinHonnen,是否可以为不同的项目组获取总和(当前-组()/(单价*数量))的小计。我在下面添加了示例输出,如何获得小计和总计?谢谢你的帮助。你是如何计算总数的?@NavinRawat,所有小计的总和,不知道如何计算
<table>
   <thead>
      <tr>
         <th>Item Number</th>
         <th>Quantity</th>
         <th>Unit Price</th>
         <th>Total</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>1148087</td>
         <td>6</td>
         <td>67.99</td>
         <td>407.93999999999994</td>
      </tr>
      <tr>
         <td>1148089</td>
         <td>1</td>
         <td>189.99</td>
         <td>189.99</td>
      </tr>
      <tr>
         <td>1150173</td>
         <td>1</td>
         <td>24.99</td>
         <td>24.99</td>
      </tr>
      <tr>
         <td>1150197</td>
         <td>3</td>
         <td>23.99</td>
         <td>71.97</td>
      </tr>
      <tr>
         <td>1151464</td>
         <td>2</td>
         <td>56.99</td>
         <td>113.98</td>
      </tr>
   </tbody>
</table>