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 7808415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:07:45+00:00 2026-06-02T03:07:45+00:00

I am trying to write a loop using XSLT so that it automatically groups

  • 0

I am trying to write a loop using XSLT so that it automatically groups all items with the same ID but in a case insensitive way. Unfortunately the data that I am trying to parse through is client driven so I cannot change it prior to load.
regardless here is a XML structure…

<Document>
    <Row>
        <Cell>ID</Cell>
    </Row>
    <Row>
        <Cell>hi</Cell>
    </Row>
    <Row>
        <Cell>Hi</Cell>
    </Row>
    <Row>
        <Cell>Hello</Cell>
    </Row>
    <Row>
        <Cell>Hello</Cell>
    </Row>
    <Row>
        <Cell>Hola</Cell>
    </Row>
</Document>

This is the XSLT I am currently using…

  <xsl:template match="Document">
    <NewDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:for-each select="//Row[position() &gt; 1]/Cell[1][not(.=preceding::Row/Cell[1])]">
        <xsl:variable name="currentOrderID" select="." />
        <xsl:variable name="currentOrderGroup" select="//Row[Cell[1] = $currentOrderID]" />

        <MainID>
          <xsl:value-of select="$currentOrderGroup[1]/Cell[1]"/>
        </MainID>
        <IDs>
          <xsl:for-each select="$currentOrderGroup">
            <id>
              <xsl:value-of select="Cell[1]"/>
            </id>
          </xsl:for-each>
        </IDs>
      </xsl:for-each>
    </NewDocument>
  </xsl:template>

This is just wrapping up things as expected in a CaSe SeNSiTiVe way…
I’ve been trying to use a translate in there in order to make everything uppercase, however I can’t seem to get the syntax just right.

The result I am trying to achieve here is this:

<NewDocument>
  <MainID>hi</MainID>
  <IDs>
    <id>hi</id>
    <id>Hi</id>
  </IDs>  
  <MainID>Hello</MainID>
  <IDs>
    <id>Hello</id>
    <id>Hello</id>
  </IDs>
  <MainID>Hola</MainID>
  <IDs>
    <id>Hola</id>
  </IDs>
</NewDocument>

Can’t seem to find anything specifically for what I need.
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-02T03:07:46+00:00Added an answer on June 2, 2026 at 3:07 am

    In XSLT1.0, to convert strings to lower case you need to use the rather cumbersome translate function in xpath.

    translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
    

    Furthermore, your problem is one of grouping, and in XSLT1.0 that usually means a technique known as Meunchian Grouping. To do, this you first define a key to look up items in the groups you require

    <xsl:key 
       name="Cell" 
       match="Cell" 
       use="translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
    

    Here we are looking up cells based on their (lower-case) text content.

    To find the first element in each group, you look for Cell elements in the XML which also happen to be the first element occurring in your look-up key

    <xsl:apply-templates 
       select="Row/Cell
       [generate-id() 
        = generate-id(
           key('Cell', 
             translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))[1])]"/>
    

    Then, when you match the first element, you can then match all elements within the group by looking at the key.

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:key name="Cell" match="Cell" use="translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
    
       <xsl:template match="Document">
          <NewDocument>
             <xsl:apply-templates select="Row/Cell[generate-id() = generate-id(key('Cell', translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))[1])]"/>
          </NewDocument>
       </xsl:template>
    
       <xsl:template match="Cell">
          <MainID>
             <xsl:value-of select="."/>
          </MainID>
          <IDs>
             <xsl:apply-templates select="key('Cell', translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))" mode="group"/>
          </IDs>
       </xsl:template>
    
       <xsl:template match="Cell" mode="group">
          <id>
             <xsl:value-of select="."/>
          </id>
       </xsl:template>
    </xsl:stylesheet>
    

    Note the use of the mode attribute, to distinguish between the two templates matching Cell elements.

    When applied to your XML, the following is output:

    <NewDocument>
       <MainID>ID</MainID>
       <IDs>
          <id>ID</id>
       </IDs>
       <MainID>hi</MainID>
       <IDs>
          <id>hi</id>
          <id>Hi</id>
       </IDs>
       <MainID>Hello</MainID>
       <IDs>
          <id>Hello</id>
          <id>Hello</id>
       </IDs>
       <MainID>Hola</MainID>
       <IDs>
          <id>Hola</id>
       </IDs>
    </NewDocument>
    

    Note, I wasn’t sure what to do with the Cell with ID as a value, so I left that it in. If you do want to exclude it, just add this line to the XSLT

    <xsl:template match="Cell[. = 'ID']" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a unit test that will loop through all action methods
I have the following line of xml that I'm trying transform using XSLT but
I am trying to write a method using reflection to return all classes that
Background: I'm trying to write a shell script using php that will automatically checkout
Trying to write a batch file that will loop through all the files in
I am trying write a function that generates simulated data but if the simulated
I am trying to write a general function in F# that would return all
I am trying to write simple program using ONLY for loop ( if then
I'm trying to write an app using GtkClutter but I can't get the actors
I'm trying to write a file using the FileOutputStream and OutputStreamWriter but I keep

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.