0

在下面的输入中,我们必须检查供应商代码是否与任何节点供应商代码匹配,然后我们必须对数量执行求和操作。否则直接映射数量。

输入:

<Move-Afile>
  <Afile>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>1234</PackNumber>
      <Quantity>12</Quantity>
    </Item>
    <Item>
    <suppliercode>2</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>3</Quantity>
    </Item>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>8</Quantity>
    </Item>
    <Item>
    <suppliercode>3</suppliercode>
      <PackNumber>126</PackNumber>
      <Quantity>11</Quantity>
    </Item>
    <Item>
    <suppliercode>4</suppliercode>
      <PackNumber>876</PackNumber>
      <Quantity>32</Quantity>
    </Item>
  </Afile>
</Move-Afile>

如果供应商代码相等,则对数量进行求和运算,否则直接映射数量。

输出:

<A>
  <target>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>1234</PackNumber>
      <Quantity>20</Quantity>
    </Item>
    <Item>
    <suppliercode>2</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>3</Quantity>
    </Item>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>20</Quantity>
    </Item>
    <Item>
    <suppliercode>3</suppliercode>
      <PackNumber>126</PackNumber>
      <Quantity>11</Quantity>
    </Item>
    <Item>
    <suppliercode>4</suppliercode>
      <PackNumber>876</PackNumber>
      <Quantity>32</Quantity>
    </Item>
  </target>
</A>

我需要一个单独的临时变量中的求和逻辑,如下所示。

<varaible name=tempvar>
<xsl:choose>
<xsl:when suppliercode=suppliercode>

<xsl:value-of select=sum(quntity)/>

<xsl:when>

<xsl:otherwise>

<xsl:value-of select=quntity/>

</xsl:otherwise>

</xsl:choose>

</variable>
4

2 回答 2

0

这个简短而有效(使用密钥)的转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kQuantityBySupplier" match="Quantity" use="../suppliercode"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
  <A>
    <target>
      <xsl:apply-templates select="*/node()"/>
    </target>
  </A>
 </xsl:template>

 <xsl:template match="Quantity/text()">
  <xsl:value-of select="sum(key('kQuantityBySupplier', ../../suppliercode))"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<Move-Afile>
  <Afile>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>1234</PackNumber>
      <Quantity>12</Quantity>
    </Item>
    <Item>
    <suppliercode>2</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>3</Quantity>
    </Item>
    <Item>
    <suppliercode>1</suppliercode>
      <PackNumber>567</PackNumber>
      <Quantity>8</Quantity>
    </Item>
    <Item>
    <suppliercode>3</suppliercode>
      <PackNumber>126</PackNumber>
      <Quantity>11</Quantity>
    </Item>
    <Item>
    <suppliercode>4</suppliercode>
      <PackNumber>876</PackNumber>
      <Quantity>32</Quantity>
    </Item>
  </Afile>
</Move-Afile>

产生想要的正确结果

<A>
   <target>
      <Item>
         <suppliercode>1</suppliercode>
         <PackNumber>1234</PackNumber>
         <Quantity>20</Quantity>
      </Item>
      <Item>
         <suppliercode>2</suppliercode>
         <PackNumber>567</PackNumber>
         <Quantity>3</Quantity>
      </Item>
      <Item>
         <suppliercode>1</suppliercode>
         <PackNumber>567</PackNumber>
         <Quantity>20</Quantity>
      </Item>
      <Item>
         <suppliercode>3</suppliercode>
         <PackNumber>126</PackNumber>
         <Quantity>11</Quantity>
      </Item>
      <Item>
         <suppliercode>4</suppliercode>
         <PackNumber>876</PackNumber>
         <Quantity>32</Quantity>
      </Item>
   </target>
</A>

请注意

这种变换的时间复杂度是线性的 (O(N))。这比重复扫描所有元素以找到具有给定的元素的效率要高出几个数量级suppliercode——它具有二次 (O(N^2)) 时间复杂度。


更新

OP 指定了一个新要求,即在变量中捕获总和或单个数量:

只需修改这个

 <xsl:template match="Quantity/text()">
  <xsl:value-of select="sum(key('kQuantityBySupplier', ../../suppliercode))"/>
 </xsl:template>

进入这个

 <xsl:template match="Quantity/text()">
  <xsl:variable name="vSum" select="sum(key('kQuantityBySupplier', ../../suppliercode))"/>
  <xsl:value-of select="$vSum"/>
 </xsl:template>
于 2013-05-19T17:19:57.167 回答
0

该样式表可以满足您的要求。它从下复制所有元素Item,并有一个特殊的模板来更改 的值,方法是将具有相同值的元素中Quantity的所有Quantity元素的值相加。Itemsuppliercode

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <A>
      <target>
        <xsl:apply-templates select="Move-Afile/Afile/Item"/>
      </target>
    </A>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Quantity">
    <xsl:variable name="supplier-code" select="parent::Item/suppliercode"/>
    <xsl:copy>
      <xsl:value-of select="sum(ancestor::Afile/Item[suppliercode = $supplier-code]/Quantity)"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

输出

<A>
   <target>
      <Item>
         <suppliercode>1</suppliercode>
         <PackNumber>1234</PackNumber>
         <Quantity>20</Quantity>
      </Item>
      <Item>
         <suppliercode>2</suppliercode>
         <PackNumber>567</PackNumber>
         <Quantity>3</Quantity>
      </Item>
      <Item>
         <suppliercode>1</suppliercode>
         <PackNumber>567</PackNumber>
         <Quantity>20</Quantity>
      </Item>
      <Item>
         <suppliercode>3</suppliercode>
         <PackNumber>126</PackNumber>
         <Quantity>11</Quantity>
      </Item>
      <Item>
         <suppliercode>4</suppliercode>
         <PackNumber>876</PackNumber>
         <Quantity>32</Quantity>
      </Item>
   </target>
</A>

更新 要在使用之前将总计放入变量中,您可以将最后一个模板替换为

  <xsl:template match="Quantity">
    <xsl:variable name="supplier-code" select="parent::Item/suppliercode"/>
    <xsl:variable name="total" select="sum(ancestor::Afile/Item[suppliercode = $supplier-code]/Quantity)"/>
    <xsl:copy>
      <xsl:value-of select="$total"/>
    </xsl:copy>
  </xsl:template>

它将 的值设置为$total具有相同供应商代码的数量之和。

于 2013-05-19T17:00:43.427 回答