xsl xml 批处理

发布于 2024-10-29 01:33:21 字数 3045 浏览 1 评论 0原文

我需要在 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>

现在我需要为每个唯一行创建一条记录(键 = 参考)。 因此,在我的输入消息中,我有 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 个“行”项目,我的输出包含 2 个“记录”项目(在记录节点内有行)。显然,“Lines/Line”项中应该有更多数据,但我在本示例中对其进行了简化。

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

非常感谢!

I need to implement some kind of batching in an xsl mapping.
Example:

Input:

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

Now I need to create a record foreach unique line (key = Reference).
So in my input message I have 2 unique records:

Reference = 71013849
Reference = 77777777

So my output file needs to look like this (simplified it a bit):

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

So as you can see my input file contains 3 'Line' items, my output contains 2 'Record' items (with inside the Record node the lines). Obviously inside the 'Lines/Line' item there should be more data, but I simplified it for this example.

Anyone knows the best way to solve this? (In XSLT 1.0)

Thx very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2024-11-05 01:33:21

此转换

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

说明慕尼黑分组方法

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

This transformation:

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

when applied to the provided XML document:

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

produces the wanted, correct result:

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

Explanation: Muenchian method for grouping.

When the number of records and different key values is significant, this is the most efficient known grouping method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文