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

  • Home
  • SEARCH
  • 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 8214281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:24:30+00:00 2026-06-07T11:24:30+00:00

I need to do rather simple thing of iterating over elements in my XML(more

  • 0

I need to do rather simple thing of iterating over elements in my XML(more explanation under the XML)

<form>
<field index="1" name="field_X_1" group="firstGroup" type="String">
<value>Value of Field X 1 of first group</value>
</field>

<field index="1" name="field_Y_1" group="firstGroup" type="String">
<value>Value of Field Y 1 of first group</value>
</field>

<field index="2" name="field_X_2" group="firstGroup" type="String">
<value>Value of Field X 2 of first group</value>
</field>

<field index="2" name="field_Y_2" group="firstGroup" type="String">
<value>Value of Field Y 2 of first group</value>
</field>

<field index="1" name="field_A_1" group="secondGroup" type="String">
<value>Value of Field A 1 of second group</value>
</field>

<field index="1" name="field_B_1" group="secondGroup" type="String">
<value>Value of Field B 1 of second group</value>
</field>

<field index="2" name="field_A_2" group="secondGroup" type="String">
<value>Value of Field A 2 of second group</value>
</field>

<field index="2" name="field_B_2" group="secondGroup" type="String">
<value>Value of Field B 2 of second group</value>
</field>


</form>

Right now I am iterating over ALL fields of firstGroup and everytime I found that index has changed(by comparing index to index of sibbling) I add new line character).
Problem is I need only to iterate over index (but still keep distiction between firstGroup and secondGroup).

My output should look like

Value of Field X 1 of first group, Value of Field Y 1 of first group
Value of Field X 2 of first group, Value of Field Y 2 of first group
Value of Field A 1 of second group, Value of Field B 1 of second group
Value of Field A 2 of second group, Value of Field B 2 of second group
etc

My XSL now looks like

    <xsl:for-each select='/form/field[@group="firstGroup"]'>
                <xsl:sort select="@index" order="ascending"></xsl:sort>     
                <xsl:if test='preceding-sibling::*[@group="firstGroup"][1]/@index != @index and count(preceding-sibling::*) != 0 '>
                    <xsl:text>&#xa;</xsl:text>
                </xsl:if>
                <xsl:value-of select="//field[@name=concat('field_X_', @index,')]/value"/>                              

                <xsl:value-of select="//field[@name=concat('field_Y_', @index,')]/value"/>                              
     </xsl:for-each>    
<!-- Differente iteration for second group-->
<xsl:text>&#xa;</xsl:text>
<xsl:for-each select='/form/field[@group="firstGroup"]'>
                <xsl:sort select="@index" order="ascending"></xsl:sort>     
                <xsl:if test='preceding-sibling::*[@group="firstGroup"][1]/@index != @index and count(preceding-sibling::*) != 0 '>
                    <xsl:text>&#xa;</xsl:text>
                </xsl:if>
                <xsl:value-of select="//field[@name=concat('field_A_', @index,')]/value"/>                              

                <xsl:value-of select="//field[@name=concat('field_B_', @index,')]/value"/>                              
     </xsl:for-each>    
  • 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-07T11:24:32+00:00Added an answer on June 7, 2026 at 11:24 am

    <xsl:output method="text" />
    
    <xsl:template match="form">
        <!--* find all the groups *-->
        <xsl:variable name="groups">
            <xsl:call-template name="get-group-names">
                <xsl:with-param name="nodes" select="field" />
            </xsl:call-template>
        </xsl:variable>
    
        <xsl:call-template name="process-all-groups">
            <xsl:with-param name="group-names" select="$groups" />
        </xsl:call-template>
    
    </xsl:template>
    
    <xsl:template name="get-group-names">
        <xsl:param name="nodes" />
        <xsl:param name="result-so-far" />
    
        <xsl:choose>
            <xsl:when test="not($nodes)">
                <xsl:value-of select="$result-so-far" />
            </xsl:when>
            <xsl:when test="contains(
                concat(' ', $result-so-far),
                concat(' ', $nodes[1]/@group, ' '))" >
    
                <xsl:call-template name="get-group-names">
                    <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" />
                    <xsl:with-param name="result-so-far" select="$result-so-far" />
                </xsl:call-template>
            </xsl:when>
    
            <xsl:otherwise>
                <xsl:call-template name="get-group-names">
                    <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" />
                    <xsl:with-param name="result-so-far"
                        select="concat($result-so-far, $nodes[1]/@group, ' ')" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="process-all-groups">
        <xsl:param name="group-names" />
    
        <xsl:variable name="group" select="substring-before($group-names, ' ')"/>
    
        <xsl:if test="not(string-length($group) = 0)">
            <xsl:call-template name="index">
                <xsl:with-param name="n" select="1" />
                <xsl:with-param name="group" select="$group" />
            </xsl:call-template>
    
            <xsl:call-template name="process-all-groups">
                <xsl:with-param name="group-names"
                    select="substring-after($group-names, ' ')" />
            </xsl:call-template>
    
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="index">
        <xsl:param name="n" select="1" />
        <xsl:param name="group" />
    
        <xsl:variable name="with-n"
            select="/form/field[@group = $group][@index = $n]" />
    
        <xsl:if test="$with-n">
    
            <xsl:for-each select="$with-n">
                <xsl:sort use="@index" order="ascending" />
    
                <xsl:value-of select="value" />
                <xsl:if test="not(position() = last())">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
            <xsl:text>&#xa;</xsl:text>
    
            <xsl:call-template name="index">
                <xsl:with-param name="n" select="$n + 1" />
                <xsl:with-param name="group" select="$group" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template match="field"></xsl:template>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a relational database schema to store vCard ver 3 data elements. Rather
I have a number rather large, complex xml documents that I need to loop
I have a collection of elements that I need to operate over, calling member
I would like to achieve rather simple thing I guess but I'm unable to
I have been asked to build a rather simple form processor application that interacts
I need a high precision time method in microseconds rather than milliseconds for actionscript,
I need to write a jUnit test for a rather complex application which runs
I need to unpack binary data that is encoded rather exotically: a 32 bit
I have a rather large table which I need to query for a reporting
I need to append multiple nodes to a container. Rather than doing a slow

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.