转换文件从xml引入多行

时间:2013-02-15 13:08:33

标签: xml xslt

请有人协助我不能为我的生活弄清楚如何调整我的翻译文件,以便跟随xml,每个供应商有多行,多个供应商在一个xml上。换句话说,我需要将文件带入我们的ERP系统,以便为xml中的每个供应商创建新的采购订单,但订单行相应地按供应商分组(例如,供应商A在xml和供应商中有多行) B有多行。所有属于A的行必须在A的同一采购订单上,同样对于B)

以下是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>A4</DESTWHS>
<RELEASE_DATE>2013-02-14</RELEASE_DATE>
<DUE_DATE>2013-05-13</DUE_DATE>
<ITEM>650610</ITEM>
<QTY>129</QTY>
<LINE_ITEM_NO>3</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>   

有关如何将我的翻译文件更改为循环或任何内容的任何帮助,以便为每个供应商将许多行添加到一个采购订单中,我们将不胜感激。

由于

更新

以下是我需要将xml转换为ERP系统的内容:

<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>

1 个答案:

答案 0 :(得分:0)

如果没有看到您的预期输出,确切地知道您想要什么有点棘手,但看起来您希望按供应商对订单进行分组。分组项是XSLT中的常见问题,在XSLT1.0中,您使用一种名为Muenchian Grouping的技术来执行此操作。

如果您按供应商对元素进行分组,则可以从定义以下键开始:

<xsl:key name="suppliers" match="row" use="SUPPLIER"/>

然后,为了获得不同的供应商,您可以选择使用给定供应商在密钥组中首先出现的元素

<xsl:apply-templates 
   select="row[generate-id() = generate-id(key('suppliers',SUPPLIER)[1])]" 
   mode="header"/>

请注意在这里使用模式,这是因为在最终的XSLT中会有两个匹配元素的模板,您需要一种方法来区分它们。< / p>

最后,对于每个'标题'元素(即第一次出现给定供应商的元素),您可以获得构成该元素的行像这样的小组:

<xsl:apply-templates select="key('suppliers', SUPPLIER)"/>

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:key name="suppliers" match="row" use="SUPPLIER"/>

   <xsl:template match="rows">
      <PostPurchaseOrders>
         <xsl:apply-templates select="row[generate-id() = generate-id(key('suppliers',SUPPLIER)[1])]" mode="header"/>
      </PostPurchaseOrders>
   </xsl:template>

   <xsl:template match="row" mode="header">
      <Orders>
         <OrderHeader>
            <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>
         </OrderHeader>
         <OrderDetails>
            <xsl:apply-templates select="key('suppliers', SUPPLIER)"/>
         </OrderDetails>
      </Orders>
   </xsl:template>

   <xsl:template match="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:template>
</xsl:stylesheet>

当应用于XSLT时,输出以下内容:

<PostPurchaseOrders>
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>LIY0001-2013-02-14</CustomerPoNumber>
         <Supplier>LIY0001</Supplier>
         <OrderDate>2013-02-14</OrderDate>
         <DueDate>2013-05-13</DueDate>
         <Warehouse>A4</Warehouse>
      </OrderHeader>
      <OrderDetails>
         <StockLine>
            <PurchaseOrderLine>1</PurchaseOrderLine>
            <StockCode>5021616</StockCode>
            <Warehouse>A4</Warehouse>
            <OrderQty>528</OrderQty>
            <Price>5.25</Price>
         </StockLine>
         <StockLine>
            <PurchaseOrderLine>2</PurchaseOrderLine>
            <StockCode>5021816</StockCode>
            <Warehouse>A4</Warehouse>
            <OrderQty>222</OrderQty>
            <Price>5.90</Price>
         </StockLine>
      </OrderDetails>
   </Orders>
   <Orders>
      <OrderHeader>
         <CustomerPoNumber>WOR0001-2013-02-14</CustomerPoNumber>
         <Supplier>WOR0001</Supplier>
         <OrderDate>2013-02-14</OrderDate>
         <DueDate>2013-05-13</DueDate>
         <Warehouse>A4</Warehouse>
      </OrderHeader>
      <OrderDetails>
         <StockLine>
            <PurchaseOrderLine>9</PurchaseOrderLine>
            <StockCode>650616</StockCode>
            <Warehouse>A4</Warehouse>
            <OrderQty>129</OrderQty>
            <Price>4.46</Price>
         </StockLine>
         <StockLine>
            <PurchaseOrderLine>3</PurchaseOrderLine>
            <StockCode>650610</StockCode>
            <Warehouse>A4</Warehouse>
            <OrderQty>129</OrderQty>
            <Price>3.5</Price>
         </StockLine>
      </OrderDetails>
   </Orders>
</PostPurchaseOrders>