xsl xml批处理

时间:2011-03-31 09:52:26

标签: xml xslt batch-file

我需要在xsl映射中实现某种批处理。 例如:

输入:

    <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唯一行(key = Reference)。 所以在我的输入消息中我有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节点内有行)。显然,在“行/行”项目中应该有更多数据,但我为此示例简化了它。

任何人都知道解决这个问题的最佳方法吗? (在XSLT 1.0中)

非常感谢!

1 个答案:

答案 0 :(得分: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

当记录数和不同的键值很重要时,这是最有效的已知分组方法。