0
votes

I have an xml snippet like this:

 <root>
    <order>
        <item>
            <item_type>A</item_type>
            <item_type>A</item_type>
            <item_type>B</item_type>
            <item_type>C</item_type>
        </item>
    </order>
    <order>
        <item>
            <item_type>A</item_type>
            <item_type>B</item_type>
            <item_type>C</item_type>
            <item_type>C</item_type>
        </item>
    </order>
    <order>
        <item>
            <item_type>C</item_type>
            <item_type>C</item_type>
            <item_type>B</item_type>
        </item>
    </order>
 </root>

and I need to group it by item_type element, but on "order" element scope, so my desired output would be:

 <root>
    <order>
        <item>A</item>
        <item>B</item>
        <item>C</item>
    </order>
    <order>
        <item>A</item>
        <item>B</item>
        <item>C</item>
    </order>
    <order>
        <item>B</item>
        <item>C</item>
    </order>
 </root>

I'm using this xslt version 1.0, but I can't figure it out.

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
    <xsl:key name="groups" match="order/item" use="item_type">
    </xsl:key>
     <xsl:template match="/">
         <xsl:for-each select="//root/order">
             <order>                
                 <xsl:for-each select="item[generate-id() = generate-id(key('groups', item_type))]">
                     <item>
                         <xsl:value-of select="key('groups', item_type)"/>
                     </item>
                 </xsl:for-each>
             </order>
         </xsl:for-each>
     </xsl:template>
 </xsl:stylesheet>

Solution have to be in xslt 1.0.

2

2 Answers

1
votes

You have numerous issues, the most important ones being:

  • You want to group item_types, not items. That means your key must match item_type;

  • You want to group them within their orders. That means your key must include a unique order identifier.

Therefore your key needs to be defined as:

<xsl:key name="groups" match="item_type" use="concat(., '|', generate-id(ancestor::order))"/>

In addition, your syntax for Muenchian grouping is wrong. And you're not outputting a root element.

Try it this way:

XSLT 1.0

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

<xsl:key name="groups" match="item_type" use="concat(., '|', generate-id(ancestor::order))"/>

<xsl:template match="/root">
    <root>
        <xsl:for-each select="order">
            <order>                
                <xsl:for-each select="item/item_type[generate-id() = generate-id(key('groups', concat(., '|', generate-id(ancestor::order)))[1])]">
                    <item>
                        <xsl:value-of select="."/>
                    </item>
                </xsl:for-each>     
            </order>
        </xsl:for-each>     
    </root>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <order>
    <item>A</item>
    <item>B</item>
    <item>C</item>
  </order>
  <order>
    <item>A</item>
    <item>B</item>
    <item>C</item>
  </order>
  <order>
    <item>C</item>
    <item>B</item>
  </order>
</root>
0
votes

If you want to do it with just XPath you can use this.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="//root/order">
            <order>                
                <xsl:for-each select="item/item_type[not(preceding-sibling::item_type=.)]">
                    <item>
                        <xsl:value-of select="."/>
                    </item>
                </xsl:for-each>
            </order>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>