Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:26:03+00:00 2026-06-12T23:26:03+00:00

Slightly different to the standard Meunchain grouping as I’m dealing with a null(?) attribute

  • 0

Slightly different to the standard Meunchain grouping as I’m dealing with a null(?) attribute for some of the tags that I’m transforming. I’d like the null groupings to be treated as their own individual group with the transformed output adding the grouped strings. Also if there is a grouping to do a count of how many there were.

<root>
<section>
    <subsection>
        <module>
            <comp>111</comp>
        </module>
        <module group='group01'>
            <comp>222</comp>
        </module>
        <module group='group01'>
            <comp>333</comp>
        </module>
        <module>
            <comp>444</comp>
        </module>
        <module>
            <comp>555</comp>
        </module>
    </subsection>
</section>
<section>
    <subsection>
        <module group ="group02">
            <comp>666</comp>
        </module>
        <module group ="group02">
            <comp>777</comp>
        </module>
        <module>
            <comp>888</comp>
        </module>
        <module group ="group03">
            <comp>999</comp>
        </module>
        <module group ="group03">
            <comp>101010</comp>
        </module>
    </subsection>
    <subsection>
        <module group ="group04">
            <comp>11111</comp>
        </module>
        <module group ="group04">
            <comp>121212</comp>
        </module>
        <module group ="group05">
            <comp>131313</comp>
        </module>
        <module group ="group05">
            <comp>141414</comp>
        </module>
        <module group ="group06">
            <comp>151515</comp>
        </module>
        <module group ="group06">
            <comp>161616</comp>
        </module>
        <module>
            <comp>171717</comp>
        </module>
    </subsection>
</section>

Wanted output:

<AllSections>
<section>
    <subsection>
        <page>
            <content>111</content>
        </page>
        <page>
            <content>222333</content>
            <count>2</count>
        </page>
        <page>
            <content>444</content>
        </page>
        <page>
            <content>555</content>
        </page>
    </subsection>
</section>
<section>
    <subsection>
        <page>
            <content>666777</content>
            <count>2</count>
        </page>
        <page>
            <content>888</content>
        </page>
        <page>
            <content>999101010</content>
            <count>2</count>
        </page>
    </subsection>
    <subsection>
        <page>
            <content>111111121212</content>
            <count>2</count>
        </page>
        <page>
            <content>131313141414161616</content>
            <count>3</count>
        </page>
        <page>
            <content>151515</content>
        </page>
        <page>
            <content>171717</content>
        </page>
    </subsection>
</section>

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:26:05+00:00Added an answer on June 12, 2026 at 11:26 pm

    For the elements with a group attribute, you are grouping by that attribute but also within the parent subsection element. Therefore you could start off by defining a key to group them this was

    <xsl:key name="modules" match="module[@group]" use="concat(generate-id(..), '|', @group)" />
    

    Next, you would need templates to match the various cases for the module elements. Firstly, you could have a template to match module elements with no group attribute, where you could format the output as required.

    <xsl:template match="module[not(@group)]">
        <page>
            <content>
                <xsl:value-of select="comp"/>
            </content>
        </page>
     </xsl:template>
    

    For modules, with group attributes you would need a match that checked that this particular module element occurred first in the group for the key defined above.

    <xsl:template 
      match="module
        [@group]
        [generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]">
    

    Within this template, you could then easily define a variable to hold the elements in the group, using the key, and then either output the child comp elements, or count them

    <xsl:variable name="modules" select="key('modules', concat(generate-id(..), '|', @group))"/>
    <page>
        <content>
            <xsl:apply-templates select="$modules/comp/text()"/>
        </content>
        <count>
            <xsl:value-of select="count($modules)" />
         </count>
    </page>
    

    Finally, you would need a third template to match all other module elements (i.e. elements with a group attribute, but not first in the group) to ignore them, to ensure they don’t get output twice. (The XSLT processor should always match more specific templates before this more generic one)

    <xsl:template match="module"/>
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:key name="modules" match="module[@group]" use="concat(generate-id(..), '|', @group)"/>
    
        <xsl:template match="root">
            <AllSections>
                <xsl:apply-templates />
            </AllSections>
        </xsl:template>
    
        <xsl:template match="module[not(@group)]">
            <page>
                <content>
                    <xsl:value-of select="comp"/>
                </content>
            </page>
        </xsl:template>
    
        <xsl:template match="module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]">
            <xsl:variable name="modules" select="key('modules', concat(generate-id(..), '|', @group))"/>
            <page>
                <content>
                    <xsl:apply-templates select="$modules/comp/text()"/>
                </content>
                <count>
                    <xsl:value-of select="count($modules)" />
                </count>
            </page>
        </xsl:template>
    
        <xsl:template match="module"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <AllSections>
        <section>
            <subsection>
                <page>
                    <content>111</content>
                </page>
                <page>
                    <content>222333</content>
                    <count>2</count>
                </page>
                <page>
                    <content>444</content>
                </page>
                <page>
                    <content>555</content>
                </page>
            </subsection>
        </section>
        <section>
            <subsection>
                <page>
                    <content>666777</content>
                    <count>2</count>
                </page>
                <page>
                    <content>888</content>
                </page>
                <page>
                    <content>999101010</content>
                    <count>2</count>
                </page>
            </subsection>
            <subsection>
                <page>
                    <content>11111121212</content>
                    <count>2</count>
                </page>
                <page>
                    <content>131313141414</content>
                    <count>2</count>
                </page>
                <page>
                    <content>151515161616</content>
                    <count>2</count>
                </page>
                <page>
                    <content>171717</content>
                </page>
            </subsection>
        </section>
    </AllSections>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need for two slightly different classes, that have the same members,
I read somewhere that the ?: operator in C is slightly different in C++,
I'd like to run a unit test where a constant is slightly different than
I'd like to change the color of a standard Android button slightly in order
I've got some inherited code that went something like this. (Please hold the laughter...
My question in slightly different from the $fopen $fwrite function Since what i would
My question is slightly different from Looking for a recommendation for a lightweight mobile
The images iam loading are slightly different sizes. how can i get the smallest
I know this question has been done but I have a slightly different twist
I read all the similar questions but mine is slightly different. The first time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.