XSLT muenchian grouping by value in child node

212 views Asked by At

I'm trying to convert an sap iDoc to another xml file with a BizTalk map written in xslt. The problem is I need to group nodes based on a value that we can find in a specific subnode.

Muenchian grouping (I use XSLT 1.0) seems to be the solution but I can't find out how to make it works as I need to group on subnode value.

<Receive>
    <idocData>
        <E2EDL20003GRP>
            <E2EDL2003>
                <ParentValue>PV</ParentValue>
            </E2EDL2003>
            ...
            <E2EDL24007GRP>
                <E2EDL24007>
                    <ChildValue>CHV1</ChildValue>
                </E2EDL24007>
                <E2EDL43000>
                    <QUALF>C</QUALF>
                    <BELNR>0000045690</BELNR>
                </E2EDL43000>
                ...
            </E2EDL24007GRP>
            <E2EDL24007GRP>
                <E2EDL24007>
                    <ChildValue>CHV2</ChildValue>
                </E2EDL24007>
                <E2EDL43000>
                    <QUALF>C</QUALF>
                    <BELNR>0000045690</BELNR>
                </E2EDL43000>
                ...
            </E2EDL24007GRP>
            <E2EDL24007GRP>
                <E2EDL24007>
                    <ChildValue>CHV3</ChildValue>
                </E2EDL24007>
                <E2EDL43000>
                    <QUALF>C</QUALF>
                    <BELNR>0000045691</BELNR>
                </E2EDL43000>
                ...
            </E2EDL24007GRP>
        </E2EDL20003GRP>
    </idocData>
</Receive>

After applying the XSLT transformation, what I am looking for is :

<ns0:Root>
    <RecordA>
        <ID>PV</ID>
        <RecordB>
            <ID>0000045690</ID>
            <RecordC>
                <Value>CHV1</Value>
            </RecordC>
            <RecordC>
                <Value>CHV2</Value>
            </RecordC>
        </RecordB>
        <RecordB>
            <ID>0000045691</ID>
            <RecordC>
                <Value>CHV3</Value>
            </RecordC>
        </RecordB>
    </RecordA>
</ns0:Root>

As you can see, I need to group E2EDL24007GRP by E2EDL43000[QUALF='C']/BELNR. I tried the following muenchian grouping :

<xsl:key name="command" match="s0:E2EDL24007GRP" use="s0:E2EDL43000[s0:QUALF='C']/s0:BELNR" />

<xsl:template match="/">
    <xsl:apply-templates select="/s1:Receive" />
</xsl:template>

<xsl:template match="/s1:Receive/s1:idocData">
    <xsl:for-each select="s0:E2EDL20003GRP">
        <ns0:Root>
            <!-- Record A -->
            <RecordA>
                <ID>
                    <xsl:value-of select="s0:E2EDL2003/s0:ParentValue" />
                </ID>
                <xsl:apply-templates select="s0:E2EDL24007GRP[generate-id()=generate-id(key('command',s0:E2EDL43000[s0:QUALF='C']/s0:BELNR)[1])]"/>
            </RecordA>
        </ns0:Root>
    </xsl:for-each>
</xsl:template>

<xsl:template match="E2EDL24007GRP">
...
</xsl:template>

But it didn't work at all, any idea ?

1

There are 1 answers

0
michael.hor257k On BEST ANSWER

The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://example.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

<xsl:key name="k" match="E2EDL24007GRP" use="E2EDL43000/BELNR" />

<xsl:template match="/Receive">
    <ns0:Root>
        <xsl:for-each select="idocData/E2EDL20003GRP">
            <RecordA>
                <ID>
                    <xsl:value-of select="E2EDL2003/ParentValue" />
                </ID>
                <xsl:for-each select="E2EDL24007GRP[generate-id()=generate-id(key('k', E2EDL43000/BELNR)[1])]">
                    <RecordB>
                        <ID>
                            <xsl:value-of select="E2EDL43000/BELNR" />
                        </ID>
                        <xsl:for-each select="key('k', E2EDL43000/BELNR)">
                            <RecordC>
                                <Value>
                                    <xsl:value-of select="E2EDL24007/ChildValue" />
                                </Value>
                            </RecordC>
                         </xsl:for-each>
                    </RecordB>
                </xsl:for-each>
            </RecordA>
        </xsl:for-each>
    </ns0:Root>
</xsl:template>

</xsl:stylesheet>

applied to your input example, will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Root xmlns:ns0="http://example.com">
  <RecordA>
    <ID>PV</ID>
    <RecordB>
      <ID>0000045690</ID>
      <RecordC>
        <Value>CHV1</Value>
      </RecordC>
      <RecordC>
        <Value>CHV2</Value>
      </RecordC>
    </RecordB>
    <RecordB>
      <ID>0000045691</ID>
      <RecordC>
        <Value>CHV3</Value>
      </RecordC>
    </RecordB>
  </RecordA>
</ns0:Root>

Note:

  1. Your input example has no namespaces;
  2. Your output has a prefix not bound to a namespace, which is not allowed; I have used a bogus namespace to produce a well-formed XML document.