0

请有人协助创建密钥以便使用 xsl 正确分组 xml 文件?使用收到的 xml,我需要能够将数据带入我的 ERP 系统,从而为每个新供应商以及每个目标仓库创建一个新的采购订单。换句话说,从下面我需要为 LIY0001 创建并转到 A4 的采购订单(这必须包括与供应商和仓库相关的所有行)。同样,需要为 WOR0001 创建一个新的采购订单到 A4,另一个为 WOR0001 创建一个到 A5(同样与这两个键相关的所有行都转到必要的采购订单分组)。对于下面的示例,应该创建 3 个单独的采购订单。

这是xml的示例:

<rows>
<row>
<SUPPLIER>LIY0001</SUPPLIER>
<DESTWHS>A4</DESTWHS>
<RELEASE_DATE>2013-02-14</RELEASE_DATE>
<DUE_DATE>2013-05-13</DUE_DATE>
<ITEM>5021616</ITEM>
<QTY>528</QTY>
<LINE_ITEM_NO>1</LINE_ITEM_NO>
<CUST_PO_NO>LIY0001-2013-02-14</CUST_PO_NO>
<PURCHASEPRICE>5.25</PURCHASEPRICE>
</row>
<row>
<SUPPLIER>LIY0001</SUPPLIER>
<DESTWHS>A4</DESTWHS>
<RELEASE_DATE>2013-02-14</RELEASE_DATE>
<DUE_DATE>2013-05-13</DUE_DATE>
<ITEM>5021816</ITEM>
<QTY>222</QTY>
<LINE_ITEM_NO>2</LINE_ITEM_NO>
<CUST_PO_NO>LIY0001-2013-02-14</CUST_PO_NO>
<PURCHASEPRICE>5.90</PURCHASEPRICE>
</row>
<row>
<SUPPLIER>WOR0001</SUPPLIER>
<DESTWHS>A4</DESTWHS>
<RELEASE_DATE>2013-02-14</RELEASE_DATE>
<DUE_DATE>2013-05-13</DUE_DATE>
<ITEM>650616</ITEM>
<QTY>129</QTY>
<LINE_ITEM_NO>9</LINE_ITEM_NO>
<CUST_PO_NO>WOR0001-2013-02-14</CUST_PO_NO>
<PURCHASEPRICE>4.46</PURCHASEPRICE>
</row>
<row>
<row>
<SUPPLIER>WOR0001</SUPPLIER>
<DESTWHS>A5</DESTWHS>
<RELEASE_DATE>2013-02-14</RELEASE_DATE>
<DUE_DATE>2013-05-13</DUE_DATE>
<ITEM>650610</ITEM>
<QTY>129</QTY>
<LINE_ITEM_NO>1</LINE_ITEM_NO>
<CUST_PO_NO>WOR0001-2013-02-14</CUST_PO_NO>
<PURCHASEPRICE>3.5</PURCHASEPRICE>
</row>
<row>
</rows>

这是我创建的翻译文件:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="Windows-1252" omit-xml-declaration="yes" />
<xsl:template match="/">
<PostPurchaseOrders>
    <Orders>
      <OrderHeader> 
      <xsl:for-each select = "rows/row">
      <CustomerPoNumber><xsl:value-of select="CUST_PO_NO"/></CustomerPoNumber> 
      <Supplier><xsl:value-of select="SUPPLIER"/></Supplier>
          <OrderDate><xsl:value-of select="RELEASE_DATE"/></OrderDate>
          <DueDate><xsl:value-of select="DUE_DATE"/></DueDate>
         <Warehouse><xsl:value-of select="DESTWHS"/></Warehouse>
     </xsl:for-each>
     </OrderHeader> 
      <OrderDetails>
     <xsl:for-each select = "rows/row">
       <StockLine>
         <PurchaseOrderLine><xsl:value-of select="LINE_ITEM_NO"/></PurchaseOrderLine>
            <StockCode><xsl:value-of select="ITEM"/></StockCode>
            <Warehouse><xsl:value-of select="DESTWHS"/></Warehouse>
           <OrderQty><xsl:value-of select="QTY"/></OrderQty>
           <Price><xsl:value-of select="PURCHASEPRICE"/></Price>
          </StockLine>
         </xsl:for-each>
       </OrderDetails>       
       </Orders>
<PostPurchaseOrders>
</xsl:template>
</xsl:stylesheet>   

这是我需要为我们的 ERP 系统转换为 xml 的内容:

<PostPurchaseOrder>
<Orders>
    <OrderHeader>
        <Supplier>LIY0001</Supplier>
        <CustomerPoNumber>example po</CustomerPoNumber>
        <OrderDate>2013-02-03</OrderDate>
        <Warehouse>A1</Warehouse>
    </OrderHeader>
    <OrderDetails>
        <StockLine>
            <PurchaseOrderLine>1</PurchaseOrderLine>
            <StockCode>12022</StockCode>
            <OrderQty>10</OrderQty>
        </StockLine>
        <StockLine>
            <PurchaseOrderLine>2</PurchaseOrderLine>
            <StockCode>15014</StockCode>
            <OrderQty>15</OrderQty>
        </StockLine>
    </OrderDetails>
</Orders>
</PostPurchaseOrders>

任何关于如何将我的翻译文件更改为 2 个键的分组以便为每个供应商以及每个目标仓库将多行带入一个采购订单的帮助将不胜感激。

谢谢

4

2 回答 2

1

您的 XSLT 和示例输出之间存在一些不一致,因此我将模拟示例输出。这应该实现您正在寻找的分组:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="kOrder" match="row" use="concat(SUPPLIER, '+', DESTWHS)"/>

  <xsl:template match="/*">
    <PostPurchaseOrder>
      <xsl:apply-templates
        select="row[generate-id() = 
                    generate-id(key('kOrder', concat(SUPPLIER, '+', DESTWHS))[1])]"
        mode="group"/>
    </PostPurchaseOrder>
  </xsl:template>

  <xsl:template match="row" mode="group">
    <OrderHeader>
      <Supplier>
        <xsl:value-of select="SUPPLIER" />
      </Supplier>
      <CustomerPoNumber>
        <xsl:value-of select="CUST_PO_NO" />
      </CustomerPoNumber>
      <OrderDate>
        <xsl:value-of select="RELEASE_DATE" />
      </OrderDate>
      <Warehouse>
        <xsl:value-of select="DESTWHS" />
      </Warehouse>
    </OrderHeader>
    <OrderDetails>
      <xsl:apply-templates select="key('kOrder', concat(SUPPLIER, '+', DESTWHS))" />
    </OrderDetails>
  </xsl:template>

  <xsl:template match="row">
    <StockLine>
      <PurchaseOrderLine>
        <xsl:value-of select="LINE_ITEM_NO" />
      </PurchaseOrderLine>
      <StockCode>
        <xsl:value-of select="ITEM" />
      </StockCode>
      <OrderQty>
        <xsl:value-of select="QTY" />
      </OrderQty>
    </StockLine>
  </xsl:template>

</xsl:stylesheet>

在您的示例输入上运行时,这会产生:

<PostPurchaseOrder>
  <OrderHeader>
    <Supplier>LIY0001</Supplier>
    <CustomerPoNumber>LIY0001-2013-02-14</CustomerPoNumber>
    <OrderDate>2013-02-14</OrderDate>
    <Warehouse>A4</Warehouse>
  </OrderHeader>
  <OrderDetails>
    <StockLine>
      <PurchaseOrderLine>1</PurchaseOrderLine>
      <StockCode>5021616</StockCode>
      <OrderQty>528</OrderQty>
    </StockLine>
    <StockLine>
      <PurchaseOrderLine>2</PurchaseOrderLine>
      <StockCode>5021816</StockCode>
      <OrderQty>222</OrderQty>
    </StockLine>
  </OrderDetails>
  <OrderHeader>
    <Supplier>WOR0001</Supplier>
    <CustomerPoNumber>WOR0001-2013-02-14</CustomerPoNumber>
    <OrderDate>2013-02-14</OrderDate>
    <Warehouse>A4</Warehouse>
  </OrderHeader>
  <OrderDetails>
    <StockLine>
      <PurchaseOrderLine>9</PurchaseOrderLine>
      <StockCode>650616</StockCode>
      <OrderQty>129</OrderQty>
    </StockLine>
  </OrderDetails>
  <OrderHeader>
    <Supplier>WOR0001</Supplier>
    <CustomerPoNumber>WOR0001-2013-02-14</CustomerPoNumber>
    <OrderDate>2013-02-14</OrderDate>
    <Warehouse>A5</Warehouse>
  </OrderHeader>
  <OrderDetails>
    <StockLine>
      <PurchaseOrderLine>1</PurchaseOrderLine>
      <StockCode>650610</StockCode>
      <OrderQty>129</OrderQty>
    </StockLine>
  </OrderDetails>
</PostPurchaseOrder>
于 2013-02-19T21:30:11.593 回答
0

您可以使用 XSLT 2.0 实现此目的,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="rows">
        <PostPurchaseOrder>
            <!-- Generate Orders with OrderHeader -->
            <xsl:for-each-group select="row" group-by="SUPPLIER">
                <Orders>
                    <OrderHeader>
                        <Supplier><xsl:value-of select="SUPPLIER" /></Supplier>
                        <CustomerPoNumber><xsl:value-of select="CUST_PO_NO" /></CustomerPoNumber>
                        <OrderDate><xsl:value-of select="RELEASE_DATE" /></OrderDate>
                        <Warehouse><xsl:value-of select="DESTWHS" /></Warehouse>
                    </OrderHeader>
                    <!-- Generate OrderDetails -->
                    <OrderDetails>
                        <!-- Generate StockLine -->
                        <xsl:for-each select="current-group()">
                            <StockLine>
                                <PurchaseOrderLine><xsl:value-of select="LINE_ITEM_NO" /></PurchaseOrderLine>
                                <StockCode><xsl:value-of select="ITEM" /></StockCode>
                                <OrderQty><xsl:value-of select="QTY" /></OrderQty>
                            </StockLine>
                        </xsl:for-each>
                    </OrderDetails>
                </Orders>
            </xsl:for-each-group>
        </PostPurchaseOrder>
    </xsl:template>
</xsl:stylesheet>
于 2013-02-19T21:42:06.453 回答