【问题标题】:XSLT: Loop through and get unique attribute valueXSLT:循环并获取唯一的属性值
【发布时间】:2017-04-22 02:18:35
【问题描述】:

我必须遍历Main/MainLines/MainLine 并得到 sum(Quantity) for-each - Item/ItemNo。每个ItemNo 的输出应该只有一个MainLine 元素。我正在做的事情是重复的.

输入

<Main Company="ABC" MainNo="213211" >
  <MainLines>
    <MainLine Quantity="2"  Node="9999">
      <Item ItemNo="123123" Class="NEW"/>
    </MainLine>
    <MainLine Quantity="1"  Node="9999">
      <Item ItemNo="123123" Class="NEW"/>
    </MainLine>
    <MainLine Quantity="3"  Node="9999">
      <Item ItemNo="123123" Class="NEW"/>
    </MainLine>
    <MainLine Quantity="2"  Node="9999">
      <Item ItemNo="22222" Class="NEW"/>
    </MainLine>
  </MainLines>
</Main>

输出

<Main Company="ABC" MainNo="213211" Reason="NewFile">
  <MainLines>
    <MainLine ExtnQuantity="6">
      <MainLine Quantity="2" Node="9999">
        <Item ItemNo="123123" Class="NEW" />
      </MainLine>
    </MainLine>
    <MainLine ExtnQuantity="6">
      <MainLine Quantity="1" Node="9999">
        <Item ItemNo="123123" Class="NEW" />
      </MainLine>
    </MainLine>
    <MainLine ExtnQuantity="6">
      <MainLine Quantity="3" Node="9999">
        <Item ItemNo="123123" Class="NEW" />
      </MainLine>
    </MainLine>
    <MainLine ExtnQuantity="2">
      <MainLine Quantity="2" Node="9999">
        <Item ItemNo="22222" Class="NEW" />
      </MainLine>
    </MainLine>
  </MainLines>
</Main>

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
  <Main>
    <xsl:copy-of select="Main/@*"/>
    <xsl:attribute name="Reason">
      <xsl:value-of select="'NewFile'"/>
    </xsl:attribute>
    <MainLines>

      <xsl:for-each select="Main/MainLines/MainLine">
        <MainLine>
          <xsl:variable name="ITEM_ID">
            <xsl:value-of select="Item/@ItemNo"/>
          </xsl:variable>

          <xsl:attribute name="ExtnQuantity">
            <xsl:value-of select="sum(../MainLine[Item/@ItemNo = $ITEM_ID]/@Quantity)"/>
          </xsl:attribute>
          <xsl:copy-of select="."/>
        </MainLine>
      </xsl:for-each>

    </MainLines>
  </Main>
  </xsl:template>
</xsl:stylesheet>

预期输出

<Main Company="ABC" MainNo="213211" Reason="NewFile">
  <MainLines>
    <MainLine ExtnQuantity="6">
      <MainLine Quantity="2" Node="9999">
        <Item ItemNo="123123" Class="NEW" />
      </MainLine>
    </MainLine>
    <MainLine ExtnQuantity="2">
      <MainLine Quantity="2" Node="9999">
        <Item ItemNo="22222" Class="NEW" />
      </MainLine>
    </MainLine>
  </MainLines>
</Main>

【问题讨论】:

    标签: xml for-loop foreach xslt-1.0


    【解决方案1】:

    这是一个Muenchian grouping 问题。 您可以通过仅迭代 ItemNo 的唯一值来解决它。 这是您的样式表的改编版本:

    请注意,我已将 &lt;MainLines&gt; 元素移动到其自己的模板中以减少嵌套。另外,你可以直接给元素添加属性,不需要&lt;xsl:attribute&gt;

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes" />
    
      <!-- Define key -->
      <xsl:key name="ItemNoKey" match="MainLine/Item" use="@ItemNo" />
    
      <xsl:template match="/Main">
        <Main Reason="NewFile">
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates select="MainLines"/>
        </Main>
      </xsl:template>
    
      <xsl:template match="MainLines">
        <MainLines>
    
          <!-- For each first MainLine with a given ItemNo -->
          <xsl:for-each select="MainLine[count(Item | key('ItemNoKey', Item/@ItemNo)[1]) = 1]">
            <xsl:variable name="CurrentItemNo" select="Item/@ItemNo"/>
    
            <!-- Print MainLine with quantity sum -->
            <MainLine ExtnQuantity="{sum(../MainLine[Item/@ItemNo = $CurrentItemNo]/@Quantity)}">
    
              <!-- Copy the selected MainLine element -->
              <xsl:copy-of select="."/>
            </MainLine>
          </xsl:for-each>
    
        </MainLines>
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2019-06-21
      • 2014-02-28
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多