2011-03-31 78 views
1

我需要在xsl映射中實現某種批處理。 實施例:xsl xml批處理

輸入:

<FinTrans_Dinas_FF xmlns="http://MH.ESB.Dinas.Schemas.FinTrans_FF.FinTrans_FF"> 
     <Line xmlns=""> 
      <Header> 
       <DocumentDate>03022011</DocumentDate> 
       <Reference>71013849</Reference> 
      </Header> 
      <Item> 
       <PostingKey>01</PostingKey> 
       <AccountNumber>0000560141</AccountNumber> 
       <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
       <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
      </Item> 
     </Line> 
     <Line xmlns=""> 
      <Header> 
       <DocumentDate>03022011</DocumentDate> 
       <Reference>71013849</Reference> 
      </Header> 
      <Item> 
       <PostingKey>01</PostingKey> 
       <AccountNumber>0000560141</AccountNumber> 
       <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
       <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
      </Item> 
     </Line> 
     <Line xmlns=""> 
      <Header> 
       <DocumentDate>03022011</DocumentDate> 
       <Reference>77777777</Reference> 
      </Header> 
      <Item> 
       <PostingKey>02</PostingKey> 
       <AccountNumber>0000560141</AccountNumber> 
       <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
       <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
      </Item> 
     </Line> 
    </FinTrans_Dinas_FF> 

現在我需要創建一個記錄的foreach獨特線(鍵=參考)。 所以在我輸入消息我有2個獨特的記錄:

Reference = 71013849 
Reference = 77777777 

所以我的輸出文件需要這個樣子(簡化了一點):

 <Trans> 
     <Record> 
      <Lines> 
       <Line> 
        <Reference>71013849</Reference> 
        <Account>Account1</Account> 
       </Line> 
       <Line> 
        <Reference>71013849</Reference> 
        <Account>Account2</Account> 
       </Line> 
      </Lines> 
     </Record> 
     <Record> 
      <Lines> 
       <Line> 
        <Reference>77777777</Reference> 
        <Account>Account3</Account> 
       </Line> 
      </Lines> 
     </Record> 
    </Trans> 

因此,大家可以看到我的輸入文件包含3個'Line'項目,我的輸出包含2個'Record'項目(在Record節點內部有行)。顯然在'Lines/Line'項目裏面應該有更多的數據,但是我在這個例子中簡化了它。

任何人都知道解決此問題的最佳方法? (在XSLT 1.0中)

Thx非常多!

+0

好問題,+1。查看我的答案,獲得完整,簡短的解決方案和解釋。 – 2011-03-31 12:47:50

回答

3

這種轉變

<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="kLineByRef" match="Line" 
      use="Header/Reference"/> 

<xsl:template match= 
    "Line[generate-id() 
     = 
     generate-id(key('kLineByRef', Header/Reference)[1]) 
     ]"> 
    <Record> 
    <xsl:copy-of select="key('kLineByRef', Header/Reference)"/> 
    </Record> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當應用於提供的XML文檔

<FinTrans_Dinas_FF xmlns="http://MH.ESB.Dinas.Schemas.FinTrans_FF.FinTrans_FF"> 
    <Line xmlns=""> 
     <Header> 
      <DocumentDate>03022011</DocumentDate> 
      <Reference>71013849</Reference> 
     </Header> 
     <Item> 
      <PostingKey>01</PostingKey> 
      <AccountNumber>0000560141</AccountNumber> 
      <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
      <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
    <Line xmlns=""> 
     <Header> 
      <DocumentDate>03022011</DocumentDate> 
      <Reference>71013849</Reference> 
     </Header> 
     <Item> 
      <PostingKey>01</PostingKey> 
      <AccountNumber>0000560141</AccountNumber> 
      <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
      <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
    <Line xmlns=""> 
     <Header> 
      <DocumentDate>03022011</DocumentDate> 
      <Reference>77777777</Reference> 
     </Header> 
     <Item> 
      <PostingKey>02</PostingKey> 
      <AccountNumber>0000560141</AccountNumber> 
      <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
      <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
</FinTrans_Dinas_FF> 

產生想要的,正確的結果:

<Record> 
    <Line> 
     <Header> 
     <DocumentDate>03022011</DocumentDate> 
     <Reference>71013849</Reference> 
     </Header> 
     <Item> 
     <PostingKey>01</PostingKey> 
     <AccountNumber>0000560141</AccountNumber> 
     <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
     <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
    <Line> 
     <Header> 
     <DocumentDate>03022011</DocumentDate> 
     <Reference>71013849</Reference> 
     </Header> 
     <Item> 
     <PostingKey>01</PostingKey> 
     <AccountNumber>0000560141</AccountNumber> 
     <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
     <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
</Record> 
<Record> 
    <Line> 
     <Header> 
     <DocumentDate>03022011</DocumentDate> 
     <Reference>77777777</Reference> 
     </Header> 
     <Item> 
     <PostingKey>02</PostingKey> 
     <AccountNumber>0000560141</AccountNumber> 
     <AmountInDocumentCurrency>/</AmountInDocumentCurrency> 
     <AmountInLocalCurrency>21,42</AmountInLocalCurrency> 
     </Item> 
    </Line> 
</Record> 

說明Muenchian method for grouping

當記錄數和不同鍵值顯着時,這是最有效的已知分組方法。

+0

哇,thx。我首先用循環foreach行做了這個解決方案,並檢查前面的引用是否相同,然後跳過循環。但表現真的很糟糕。 Thx很多這個解決方案! – 2011-04-04 08:08:11