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>
You can use the
group-starting-withattribute of thexsl:for-each-groupto do the grouping the way you want.Here:
when applied to your input document produces the expected output as you have specified: