0

我是 XSLT 的新手。我有合并和添加的要求。

XML:

<OrderDetails>
  <OrderDetail action="add">
    <OrderedUnits>18</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295755</PONumber>
  </OrderDetail>
  <OrderDetail action="add">
    <OrderedUnits>12</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295755</PONumber>
  </OrderDetail>
  <IOrderDetail action="add">
    <OrderedUnits>18</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295762</PONumber>
  </OrderDetail>
<OrderDetails> 

如果LocationCodeDatePONumber字段匹配,我需要添加OrderedUnits并使其只有一个条目。

预期的输出 XML:

<OrderDetails>
  <OrderDetail action="add">
    <OrderedUnits>30</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295755</PONumber>
  </OrderDetail>
  <IOrderDetail action="add">
    <OrderedUnits>18</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295762</PONumber>
  </OrderDetail>
<OrderDetails>

我该如何编写这个 XSLT?

4

2 回答 2

1

由于您没有指定版本并且应该有更多人使用它,因此这里有一个 XSLT 2.0 选项。

XML 输入

<OrderDetails>
    <OrderDetail action="add">
        <OrderedUnits>18</OrderedUnits>
        <Date>2013-09-30T00:00:00</Date>
        <LocationCode>3202</LocationCode>
        <PONumber>022548295755</PONumber>
    </OrderDetail>
    <OrderDetail action="add">
        <OrderedUnits>12</OrderedUnits>
        <Date>2013-09-30T00:00:00</Date>
        <LocationCode>3202</LocationCode>
        <PONumber>022548295755</PONumber>
    </OrderDetail>
    <OrderDetail action="add">
        <OrderedUnits>18</OrderedUnits>
        <Date>2013-09-30T00:00:00</Date>
        <LocationCode>3202</LocationCode>
        <PONumber>022548295762</PONumber>
    </OrderDetail>
</OrderDetails>

XSLT 2.0

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

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

    <xsl:template match="OrderDetails">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="OrderDetail" group-by="string-join((Date,LocationCode,PONumber),'|')">
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <xsl:for-each-group select="current-group()/*" group-by="name()">
                        <xsl:apply-templates select="current-group()[1]"/>
                    </xsl:for-each-group>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="OrderedUnits">
        <xsl:copy>
            <xsl:value-of select="sum(current-group())"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<OrderDetails>
   <OrderDetail action="add">
      <OrderedUnits>30</OrderedUnits>
      <Date>2013-09-30T00:00:00</Date>
      <LocationCode>3202</LocationCode>
      <PONumber>022548295755</PONumber>
   </OrderDetail>
   <OrderDetail action="add">
      <OrderedUnits>18</OrderedUnits>
      <Date>2013-09-30T00:00:00</Date>
      <LocationCode>3202</LocationCode>
      <PONumber>022548295762</PONumber>
   </OrderDetail>
</OrderDetails>
于 2013-10-26T01:51:49.317 回答
1

那个怎么样?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" encoding="utf-8" indent="no" />
   <xsl:template match="/">
      <OrderDetails>
         <xsl:for-each select="//OrderDetail">
            <xsl:variable name="this_date" select="Date" />
            <xsl:variable name="this_pon" select="PONumber" />
            <xsl:variable name="this_loc" select="LocationCode" />
            <xsl:variable name="all" select="count(//OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]) " />
            <xsl:variable name="before" select="count(preceding-sibling::OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]) " />
            <xsl:if test="$before + 1 = $all">
               <!-- we are in the last distinct order, display it  -->
               <OrderDetail action="add">
                  <OrderedUnits>
                     <xsl:value-of select="sum(//OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]/OrderedUnits)" />
                  </OrderedUnits>
                  <Date>
                     <xsl:value-of select="Date" />
                  </Date>
                  <LocationCode>
                     <xsl:value-of select="LocationCode" />
                  </LocationCode>
                  <PONumber>
                     <xsl:value-of select="PONumber" />
                  </PONumber>
               </OrderDetail>
            </xsl:if>
         </xsl:for-each>
      </OrderDetails>
   </xsl:template>
</xsl:stylesheet>

更正的 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<OrderDetails>
  <OrderDetail action="add">
    <OrderedUnits>18</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295755</PONumber>
  </OrderDetail>

 <OrderDetail action="add">
    <OrderedUnits>12</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295755</PONumber>
  </OrderDetail>
  <OrderDetail action="add">
    <OrderedUnits>18</OrderedUnits>
    <Date>2013-09-30T00:00:00</Date>
    <LocationCode>3202</LocationCode>
    <PONumber>022548295762</PONumber>
  </OrderDetail>
</OrderDetails>

结果(测试)

<?xml version="1.0" encoding="UTF-8"?>
<OrderDetails>
   <OrderDetail action="add">
      <OrderedUnits>30</OrderedUnits>
      <Date>2013-09-30T00:00:00</Date>
      <LocationCode>3202</LocationCode>
      <PONumber>022548295755</PONumber>
   </OrderDetail>
   <OrderDetail action="add">
      <OrderedUnits>18</OrderedUnits>
      <Date>2013-09-30T00:00:00</Date>
      <LocationCode>3202</LocationCode>
      <PONumber>022548295762</PONumber>
   </OrderDetail>
</OrderDetails>
于 2013-10-25T22:16:02.983 回答