基于非相同行项目的分组 xsl1.0

grouping based on non-identical line items xsl1.0

我有如下要求只使用XSL1.0实现转换,

  1. 第一个条件是根据 parentLineNumber.
  2. 分组行项目
  3. 那么第二个条件是当ParentLinenumberLineNumber相同时忽略发票行,仅当组有多个发票时订单项。

示例输入:

<InvoiceNotification>
    <Invoice>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000010</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK06</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000011</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK07</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000012</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK08</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000020</parentLineNumber>
            </LineSection>
            <LineNumber>000020</LineNumber>
            <proprietaryInformation>
                <FreeFormText>GK01</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
    </Invoice>
</InvoiceNotification>

我在 XSLT 下进行了部分开发。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="Invoices" match="InvoiceLineItem" use="LineSection/parentLineNumber"/>
    <xsl:template match="InvoiceNotification">
        <Invoices>
            <xsl:for-each select="Invoice/InvoiceLineItem [ count ( key('Invoices',LineSection/parentLineNumber)[1] | . ) = 1 ]">
                <Batchorder>
                    <xsl:for-each select="key('Invoices',LineNumber)">
                        <Items>
                            <LineItem>
                                <xsl:value-of select="proprietaryInformation"/>
                            </LineItem>
                        </Items>
                    </xsl:for-each>
                </Batchorder>
            </xsl:for-each>
        </Invoices>
    </xsl:template>
</xsl:stylesheet>

结果输出:

<?xml version="1.0" encoding="UTF-8"?>
<Invoices>
    <Batchorder>
        <Items>
            <proprietaryInformation>PK06</proprietaryInformation>
        </Items>
        <Items>
            <proprietaryInformation>PK07</proprietaryInformation>
        </Items>
        <Items>
            <proprietaryInformation>PK08</proprietaryInformation>
        </Items>
    </Batchorder>
    <Batchorder>
        <Items>
            <proprietaryInformation>GK01</proprietaryInformation>
        </Items>
    </Batchorder>
</Invoices>

但我期望低于输出,

<?xml version="1.0" encoding="UTF-8"?>
<Invoices>
    <Batchorder>
        <Items>
            <proprietaryInformation>PK07</proprietaryInformation>
        </Items>
        <Items>
            <proprietaryInformation>PK08</proprietaryInformation>
        </Items>
    </Batchorder>
    <Batchorder>
        <Items>
            <proprietaryInformation>GK01</proprietaryInformation>
        </Items>
    </Batchorder>
</Invoices>

您可以在 for-each 上使用谓词 我认为:

<xsl:for-each select="key('Invoices',LineNumber)[LineNumber != LineSection/parentLineNumber or count(key('Invoices', LineNumber)) = 1]">

与 Martin Honnen 提出的解决方案几乎相同,但可能效率更高:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:key name="Invoices" match="InvoiceLineItem" use="LineSection/parentLineNumber"/>
    <xsl:template match="InvoiceNotification">
        <Invoices>
            <xsl:for-each select=
            "Invoice/InvoiceLineItem
            [count ( key('Invoices',LineSection/parentLineNumber)[1] | . ) = 1]">
                <Batchorder>
                    <xsl:for-each select="key('Invoices',LineNumber)
                    [not(LineNumber = LineSection/parentLineNumber)
                    or not(key('Invoices',LineNumber)[2])]">
                        <Items>
                            <LineItem>
                                <xsl:value-of select="proprietaryInformation"/>
                            </LineItem>
                        </Items>
                    </xsl:for-each>
                </Batchorder>
            </xsl:for-each>
        </Invoices>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的源 XML 文档时:

<InvoiceNotification>
    <Invoice>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000010</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK06</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000011</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK07</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000010</parentLineNumber>
            </LineSection>
            <LineNumber>000012</LineNumber>
            <proprietaryInformation>
                <FreeFormText>PK08</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
        <InvoiceLineItem>
            <LineSection>
                <parentLineNumber>000020</parentLineNumber>
            </LineSection>
            <LineNumber>000020</LineNumber>
            <proprietaryInformation>
                <FreeFormText>GK01</FreeFormText>
            </proprietaryInformation>
        </InvoiceLineItem>
    </Invoice>
</InvoiceNotification>

产生了想要的、正确的结果:

<Invoices>
   <Batchorder>
      <Items>
         <LineItem>
                PK07
            </LineItem>
      </Items>
      <Items>
         <LineItem>
                PK08
            </LineItem>
      </Items>
   </Batchorder>
   <Batchorder>
      <Items>
         <LineItem>
                GK01
            </LineItem>
      </Items>
   </Batchorder>
</Invoices>