简体   繁体   English

Muenchian 分组 XSLT 1.0 多重分组

[英]Muenchian grouping XSLT 1.0 multiple grouping

I've read similar posts, but can't figure out how to apply Muenchian grouping in XSLT 1.0 based on multiple columns.我读过类似的帖子,但无法弄清楚如何基于多列在 XSLT 1.0 中应用 Muenchian 分组。

I'm stuck with the worst XML-file there is, can't change the layout.我受困于最糟糕的 XML 文件,无法更改布局。 This is a sample:这是一个示例:

<DataSet>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000009</Cell>
            <Cell>F29888</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12350</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12135</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-B</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12350</Cell>
        </Cells>
    </Row>
</DataSet>

I want to use Muenchian grouping in XSLT1.0 to group by the first, second and third cell.我想在 XSLT1.0 中使用 Muenchian 分组按第一个、第二个和第三个单元格进行分组。 The fourth cell needs to be linked to that key.第四个单元格需要链接到该密钥。 Expected result:预期结果:

<DataSet>
    <Invoice>
        <Key>
            <Company>COMPANY-A</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000009</Num>
        </Key>
        <Customers>
            <Customer>F29888</Customer>
        </Customers>
    </Invoice>
    <Invoice>
        <Key>
            <Company>COMPANY-A</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000010</Num>
        </Key>
        <Customers>
            <Customer>F12350</Customer>
            <Customer>F12135</Customer>
        </Customers>
    </Invoice>
    <Invoice>
        <Key>
            <Company>COMPANY-B</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000010</Num>
        </Key>
        <Customers>
            <Customer>F12350</Customer>
        </Customers>
    </Invoice>
</DataSet>

I've tried this, but there is no result:我试过这个,但没有结果:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="document-by-number" match="GenericBrowseResponse/Select/Response/Selection/DataSet/Row" use="Cells/Cell[2]"></xsl:key>

<xsl:template match="GenericBrowseResponse/Select/Response/Selection/DataSet/Row">
    <Invoices>
        <xsl:apply-templates select="Cells[generate-id(.)=generate-id(key('document-by-number',Cells/Cell[2])[1])]"/>
    </Invoices>
</xsl:template>

<xsl:template match="Cells">
    <Invoice>
        <xsl:for-each select="key('document-by-number', Cell[2])">
            <Document><xsl:value-of select="Cell[3]"/></Document>
        </xsl:for-each> 
    </Invoice>
</xsl:template>

<xsl:template match="text()"></xsl:template>

</xsl:stylesheet>

Tried some possibilities with the way the key is defined, Solved the issue with the following code:尝试了一些定义键的方式的可能性,解决了以下代码的问题:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="k1" match="Cells" use="concat(Cell[1], '|', Cell[2], '|', Cell[3])" />
<!--<xsl:key name="k1" match="Cells" use="Cell[3]"></xsl:key>-->

<xsl:template match="/DataSet">
  <DataSet>
  <xsl:apply-templates select="Row/Cells[generate-id()=generate-id(key('k1',concat(Cell[1], '|', Cell[2], '|', Cell[3]))[1])]"/>
  </DataSet>
</xsl:template>

<xsl:template match="Row/Cells">
    <Invoice>
      <Key>
        <Company><xsl:value-of select="Cell[1]"/></Company>
        <Type><xsl:value-of select="Cell[2]"/></Type>
        <Num><xsl:value-of select="Cell[3]"/></Num>
      </Key>
      <Customer>
        <xsl:for-each select="key('k1', concat(Cell[1], '|', Cell[2], '|', Cell[3]))">
            <Customers><xsl:value-of select="Cell[4]"/></Customers>
        </xsl:for-each> 
      </Customer>
    </Invoice>
</xsl:template>

<xsl:template match="text()"></xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM