创建xslt 1.0将元素分组

时间:2019-03-11 16:18:23

标签: xslt

请帮我把xslt分组。 Input.xml:

     <EDI>
     <Header/>
     <Data>
     <ISA> <Identifier>123</Identifier>
     </ISA>
    <Function>
    <Transaction>
    <BPR><Account> 123<Account></BPR>
    <CUR><Code>USD</Code></CUR>
    </Transaction>
    <Transaction>
    <BPR><Account> 123<Account></BPR>
    <CUR><Code>EUR</Code></CUR>
    </Transaction>
    <Transaction>
    <BPR><Account> 123<Account></BPR>
    <CUR><Code>USD</Code></CUR>
    </Transaction>
    </Function>
    </Data>
    </EDI>

我想基于CUR / Code对输入xml进行分组。我也创建了key和generateid,但是在我的输出xml中,它也在Output之前也反映了ISA字段值。

输出xml:

<Output>
<Filename>USAFile</Filename> (If the current-group has Code USD)
<EDI>
<Data><ISA><Identifier>12</Identifier></ISA>
<Function>
<Transaction>
<BPR><Account> 123<Account></BPR>
<CUR><Code>USD</Code></CUR>
</Transaction>
<Transaction>
<BPR><Account> 123<Account></BPR>
<CUR><Code>USD</Code></CUR>
</Transaction>
</Function>
</Data>
</EDI>
</Output>

<Output>
<Filename>EURFile</Filename> (If the current-group has Code EUR)
<EDI>
<Data><ISA><Identifier>23</Identifier></ISA>
<Function>
<Transaction>
<BPR><Account> 123<Account></BPR>
<CUR><Code>EUR/Code></CUR>
</Transaction>
</Data>
</EDI>
</Output>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="no"/>
    <xsl:key name="groups" match="/EDI/Data/Function/Transaction" use="CUR/Code" />
      <xsl:template match="Function">
      <Function>
        <xsl:for-each select="Transaction[generate-id() = generate-id(key('groups', CUR/Code)[1])]"/>
          <xsl:for-each select="key('groups',CUR/Code)">
            <Transaction>
              <BR><Account><xsl:value-of select="BPR/Account"/></Account>
              <CUR><Code><xsl:value-of select="CUR/Code"/></Code>
            </Transaction>
          </xsl:for-each>
          </xsl:for-each>
        </Function>
      </xsl:template>
    </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

首先,使输入XML的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<EDI>
<Header />
<Data>
    <ISA>
        <Identifier>123</Identifier>
    </ISA>
    <Function>
        <Transaction>
            <BPR>
                <Account>
                    123
                </Account>
            </BPR>
            <CUR>
                <Code>USD</Code>
            </CUR>
        </Transaction>
        <Transaction>
            <BPR>
                <Account>
                    123
                </Account>
            </BPR>
            <CUR>
                <Code>EUR</Code>
            </CUR>
        </Transaction>
        <Transaction>
            <BPR>
                <Account>
                    123
                </Account>
            </BPR>
            <CUR>
                <Code>USD</Code>
            </CUR>
        </Transaction>
    </Function>
</Data>
</EDI>

使用与尝试过的方法不同的方法(以防万一,请允许):

注意::在输出XML中,您在<Identifier>中为<ISA>显示了两个不同的值。我猜是错别字。如果我缺少某些东西,请纠正我。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:key name="groups" match="/EDI/Data/Function/Transaction" use="CUR/Code" />

<xsl:template match="Function">
    <xsl:apply-templates mode="group" />
</xsl:template>

<xsl:template match="Transaction[count(. | key('groups',CUR/Code)[1]) = 1]" mode="group">
    <Output>
        <Filename>
            <xsl:choose>
                <xsl:when test="CUR/Code = 'USD'">
                    <xsl:value-of select="'USAFile'" />
                </xsl:when>
                <xsl:when test="CUR/Code = 'EUR'">
                    <xsl:value-of select="'EURFile'" />
                </xsl:when>
            </xsl:choose>
        </Filename>
        <EDI>
            <Data>
                <ISA>
                    <Identifier>
                        <xsl:value-of select="../preceding-sibling::*[1]" />
                    </Identifier>
                </ISA>
                <Function>
                    <Transaction>
                        <xsl:copy-of select="key('groups',CUR/Code)" />
                    </Transaction>
                </Function>
            </Data>
        </EDI>
    </Output>
</xsl:template>

<xsl:template match="ISA" />

<!-- remove nodes other than through the key -->
<xsl:template match="node()" mode="group" />

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/94rmq63