Skip to Content

For Each Group equivalent function in XSLT 1.0

Hi Folks,

I have issue getting XSLT 2.0 working with PO 7.5.

I'm using For each group to group the orders by po_no.

I would request your help with similar code that is compatible with XSLT 1.0

I receive data as below:

Source Structure:

<data>

<po_no>100</po_no>

<po_item>01</po_item>

<date>20171004</date>

<supplier>12</supplier>

</data> <data>

<po_no>100</po_no>

<po_item>02</po_item>

<date>20171004</date>

<supplier>13</supplier>

</data>

I need to transform it as below:

<data>

<Header>

<date>20171004</date>

<po_no>100</po_no>

</Header>

<Item>

<po_item>01</po_item>

<supplier>12</supplier>

</Item>

<Item>

<po_item>02</po_item>

<supplier>12</supplier>

</Item>

</data>

I used XSLT 2.0 foreachgroup & code works fine but I cannot get it working on PO.

I have also applied note 2221350- but no go. Could you please help me with xslt 1.0 equivalent grouping code.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 13, 2017 at 06:14 AM

    you can check below blog on how to enable 2.0 xslt to work.

    https://blogs.sap.com/2014/10/14/how-to-import-and-use-xslt-20-mappings-in-sap-pipo/

    otherwise use this code for xslt 1.0

    input

    <root>
    <data>
    
    
    <po_no>100</po_no>
    
    
    <po_item>01</po_item>
    
    
    <date>20171004</date>
    
    
    <supplier>12</supplier>
    
    
    </data> <data>
    
    
    <po_no>100</po_no>
    
    
    <po_item>02</po_item>
    
    
    <date>20171004</date>
    
    
    <supplier>13</supplier>
    
    
    </data>
    <data>
    
    
    <po_no>101</po_no>
    
    
    <po_item>01</po_item>
    
    
    <date>20171005</date>
    
    
    <supplier>12</supplier>
    
    
    </data> <data>
    
    
    <po_no>102</po_no>
    
    
    <po_item>02</po_item>
    
    
    <date>20171007</date>
    
    
    <supplier>13</supplier>
    
    
    </data>
    
    
    </root>
    

    xsl

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        >
    
    
      <xsl:output method="xml" indent="yes"/>      
      <xsl:key name="groups" match="/root/data" use="po_no" />
    
    
      <xsl:template match="/root">
      <root>
        <xsl:apply-templates select="data[generate-id() = generate-id(key('groups', po_no)[1])]"/>
    	</root>
      </xsl:template>
      <xsl:template match="data">
    	<data>
    	<header>
    	<po_no><xsl:value-of select="po_no"/></po_no>
    	<date><xsl:value-of select="date"/></date>
    	</header>
            <xsl:for-each select="key('groups', po_no)">
            <Item>
              <po_item><xsl:value-of select="po_item"/></po_item>
              <supplier><xsl:value-of select="supplier"/></supplier>
            </Item>
          </xsl:for-each>
        </data>
      </xsl:template>
    </xsl:stylesheet>
    

    output

    <?xml version="1.0" encoding="UTF-8"?><root>
    <data>
    <header>
    <po_no>100</po_no>
    <date>20171004</date>
    </header>
    <Item>
    <po_item>01</po_item>
    <supplier>12</supplier>
    </Item>
    <Item>
    <po_item>02</po_item>
    <supplier>13</supplier>
    </Item>
    </data>
    <data>
    <header>
    <po_no>101</po_no>
    <date>20171005</date>
    </header>
    <Item>
    <po_item>01</po_item>
    <supplier>12</supplier>
    </Item>
    </data>
    <data>
    <header>
    <po_no>102</po_no>
    <date>20171007</date>
    </header>
    <Item>
    <po_item>02</po_item>
    <supplier>13</supplier>
    </Item>
    </data>
    </root>
    
    
    

    Regards,

    Muni

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 13, 2017 at 01:21 PM

    Thanks again for the XSLT 1.0 equivalent code.

    The Java code in the note was compiled on SAXON 9.5 jar.

    When I tried with Saxon 9.5 Jar file my xslt 2.0 code works fine.

    Add comment
    10|10000 characters needed characters exceeded