0

My XML file looks like

<templates>
    <template type="ORC">
        <field/>
    </template>
    <template type="OBR">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="SPM">
        <field/>
    </template>
    <template type="ORC">
        <field/>
    </template>
    <template type="OBR">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="OBX">
        <field/>
    </template>
    <template type="SPM">
        <field/>
    </template>
</templates>

I would like to group the order details (template/@type='ORC') and convert above sample XML into below format with XSLT 2.0

<templates>
    <order-details>
        <template type="ORC">
            <field/>
        </template>
        <template type="OBR">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="SPM">
            <field/>
        </template>
    </order-details>
    <order-details>
        <template type="ORC">
            <field/>
        </template>
        <template type="OBR">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="OBX">
            <field/>
        </template>
        <template type="SPM">
            <field/>
        </template>
    </order-details>
</templates>
2
  • Grouping would require a common value, you want to split on @type="ORC" Commented May 11, 2012 at 17:56
  • @Tommy - In this case grouping only requires that the group start with @type="ORC". See Pavel's answer to see how to do this in XSLT 2.0. It's also easy in 1.0; I can add an answer with a 1.0 solution if you'd like. Commented May 11, 2012 at 18:46

1 Answer 1

2

You can use the group-starting-with attribute of the xsl:for-each-group to do the grouping the way you want.

Here:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="templates">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="template" 
                                group-starting-with="*[@type='ORC']">
                <order-details> 
                    <xsl:apply-templates select="current-group()"/>
                </order-details>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

when applied to your input document produces the expected output as you have specified:

<templates>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
   <order-details>
      <template type="ORC">
         <field/>
      </template>
      <template type="OBR">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="OBX">
         <field/>
      </template>
      <template type="SPM">
         <field/>
      </template>
   </order-details>
</templates>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.