• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 50
  • Last Modified:

XSL Grouping

Can you please let me know how to group below input file:

<root>
<group>
   <fInfo>21-21-02.xml</fInfo>
   <bInfo>21-21-02-01</bInfo>
</group>
<group>
   <fInfo>21-21-02-01.xml</fInfo>
   <bInfo>21-21-02-01-01</bInfo>
   <bInfo>21-21-02-01-02</bInfo>
   <bInfo>21-21-02-01-03</bInfo>
</group>
<group>
   <fInfo>21-21-02-01-03.xml</fInfo>
   <bInfo>21-21-02-01-03A</bInfo>
   <bInfo>21-21-02-01-03B</bInfo>
</group>
<group>
   <fInfo>21-21-02-01-04.xml</fInfo>
   <bInfo>21-21-02-01-04</bInfo>
</group>
<group>
   <fInfo>21-21-02-01-03.xml</fInfo>
   <bInfo>21-21-02-01-03C</bInfo>
   <bInfo>21-21-02-01-04A</bInfo>
</group>
</root>

to as below:

<?xml version="1.0" encoding="utf-8"?>
<root>
<group>
   <fInfo>21-21-02.xml</fInfo>
   <bInfo>21-21-02-01</bInfo>
</group>
<group>
   <fInfo>21-21-02-01.xml</fInfo>
   <bInfo>21-21-02-01-01</bInfo>
   <bInfo>21-21-02-01-02</bInfo>
   <bInfo>21-21-02-01-03</bInfo>
   <bInfo>21-21-02-01-04 <info>New data</info></bInfo>
</group>
<group>
   <fInfo>21-21-02-01-03.xml</fInfo>
   <bInfo>21-21-02-01-03A</bInfo>
   <bInfo>21-21-02-01-03B</bInfo>
   <bInfo>21-21-02-01-03C <info>New data</info></bInfo>
</group>
<group>
   <fInfo>21-21-02-01-04.xml</fInfo>
   <bInfo>21-21-02-01-04A <info>New data</info></bInfo>
</group>
<group>
   <fInfo>21-21-02-01-03.xml</fInfo>
</group>
</root>

Thanks
0
rshk2001
Asked:
rshk2001
  • 3
2 Solutions
 
Geert BormansCommented:
I think the following comes close

But isn't exactly there
The hard part is done (the grouping)
You can now easily take care of the rest

It seems a bit unapropriate to dump source and result and expect the XSLT to magically happen.
At least you could have taken the effort to explain the mapping and write some specs

Anyway, I hope the attached XSLT gives you enough basis to get the job done
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:key name="group" match="group" use="fInfo"/>
    
    <xsl:template match="root">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>
    
    <xsl:template match="group[generate-id() = generate-id(key('group', fInfo)[1])]">
        <xsl:variable name="this-id" select="generate-id()"></xsl:variable>
        <xsl:copy>
            <xsl:copy-of select="node()"/>
            <xsl:for-each select="key('group', fInfo)[not(generate-id() = $this-id)]">
                <xsl:for-each select="bInfo">
                    <xsl:copy>
                        <xsl:copy-of select="node()"/>
                        <info>New data</info>
                    </xsl:copy>
                    </xsl:for-each>
            </xsl:for-each>
        </xsl:copy>
        
    </xsl:template>
    
    <xsl:template match="group[not(generate-id() = generate-id(key('group', fInfo)[1]))]">
        <xsl:copy>
            <xsl:copy-of select="fInfo"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
Daniel JungesCommented:
0
 
Geert BormansCommented:
dear junges,
thanks for your contribution
BUT my approach is using the Muenchian Grouping pattern.
read this line: group[generate-id() = generate-id(key('group', fInfo)[1])], looks familiar?

So your claim "is more efficient"... more efficient than what? Muenchian Grouping is more efficient than Muenchian Grouping???
In theory Muenchian grouping is more efficient than finding uniqueness through walking the sibling axis.
Something I don't do here, so your general (found on google) statement is not relevant in this context.
Note also that even that statement is likely not true, simply because most modern XSLT1 processors are optimised to take away that efficiency gain.
Statements about efficiency and XSLT are dangerous in a general context
Before you come up with a suggestion that you claim to be superior to an earlier post, make sure that you understand the earlier post.

If you would had studied the problem (which sadly did not get any follow up from the original poster)
you would have spotted that the nodes should be grouped, but the grouped nodes also remain in their old location.
So we need a variant on Muenchian here, sufficiently done by putting the uniqueness constraint in the predicate of the template match attribute.
Something I developed, posted and now waiting for feedback from the original poster.

Oh, if you want to teach people about Muenchian Grouping,
this article beats the other references as for quality
http://www.jenitennison.com/xslt/grouping/muenchian.xml

0
 
rshk2001Author Commented:
Gertone! Apologies since I was away whereby I didnt checked my emails. Also, thanks for your solution and I will try to fit that into my requirements. As you said, true this is bit complicated the way the grouping is required. I appreciate your help.
0
 
Geert BormansCommented:
Please accept https:#a36941614 as the answer. It does solve the grouping part completely.

After that response there is some noise, but that made me give some more explanation and references in https:#a36968349, that if you want you can make an assisted answer simply for the references
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now