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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:27:50+00:00 2026-06-12T05:27:50+00:00

XML: <sample> <test> <Cell1>John</Cell1> <Cell2>A</Cell2> <Cell4>xy</Cell4> </test> <test> <Cell1>John</Cell1> <Cell2>B</Cell2> <Cell6>10</Cell6> </test> <test> <Cell1>John,Jade</Cell1>

  • 0

XML:

<sample>
    <test>
        <Cell1>John</Cell1>
        <Cell2>A</Cell2>
        <Cell4>xy</Cell4>
    </test>
    <test>
        <Cell1>John</Cell1>
        <Cell2>B</Cell2>
        <Cell6>10</Cell6>
    </test>
    <test>
        <Cell1>John,Jade</Cell1>
        <Cell2>A,Y</Cell2>
        <Cell4>1</Cell4>
    </test>
    <test>
        <Cell1>John,Jade</Cell1>
        <Cell2>A C,X</Cell2>
    </test>
    <test>
        <Cell1>John,Jade</Cell1>
        <Cell2>C D,Y</Cell2>
    </test>
    <test>
        <Cell1>John</Cell1>
        <Cell2>A B</Cell2>
        <Cell4>xy</Cell4>
    </test>
</sample>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output method="xml" encoding="UTF-8" indent="no"/>
    <xsl:template match="/">
        <xsl:apply-templates select="sample"/>
    </xsl:template>
    <xsl:template match="sample">
        <xsl:variable name="atomictest">
            <!--Store the test containing only one value in cell2-->
            <xsl:copy-of select="test[not(contains(Cell2,',')) or not(contains(Cell2,' '))]"/>
        </xsl:variable>
        <xsl:variable name="copy">
            <xsl:apply-templates select="test">
                <xsl:with-param name="atomictest" select="$atomictest"/>
            </xsl:apply-templates>
        </xsl:variable>
    </xsl:template>
    <xsl:template match="test">
        <xsl:param name="atomictest"/>
        <xsl:choose>
            <xsl:when test="contains(Cell2,',')">
                <xsl:variable name="Cell1">
                    <xsl:copy-of select="Cell1"/>
                </xsl:variable>
                <!-- tokenize cell2 based on comma -->
                <xsl:for-each select="tokenize(Cell2,',')">
                    <xsl:variable name="str">
                        <xsl:value-of select="."/>
                    </xsl:variable>
                    <xsl:variable name="pos">
                        <xsl:value-of select="position()"/>
                    </xsl:variable>
                    <xsl:choose>
                        <!-- If cell2 contains space -->
                        <xsl:when test="contains(.,' ')">
                            <!-- tokenize cell2 based on comma -->
                            <xsl:for-each select="tokenize(.,' ')">
                                <xsl:variable name="str">
                                    <xsl:value-of select="."/>
                                </xsl:variable>
                                <!-- if cell2 value not contained in the atomic collected -->
                                <xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
                                    <!--Store Cell2 value -->
                                    <xsl:variable name="Cell2">
                                        <xsl:value-of select="."/>
                                    </xsl:variable>
                                    <!-- tokenize cell1-->
                                    <xsl:for-each select="tokenize($Cell1/Cell1,',')">
                                        <xsl:if test="position()=$pos">
                                            <test>
                                                <Cell1>
                                                    <xsl:value-of select="."/>
                                                </Cell1>
                                                <Cell2>
                                                    <xsl:value-of select="$Cell2"/>
                                                </Cell2>
                                            </test>
                                        </xsl:if>
                                    </xsl:for-each>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <!-- if cell2 doesnot contains space -->
                            <xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
                                <xsl:variable name="Cell2">
                                    <xsl:value-of select="."/>
                                </xsl:variable>
                                <xsl:for-each select="tokenize($Cell1/Cell1,',')">
                                    <xsl:if test="position()=$pos">
                                        <test>
                                            <Cell1>
                                                <xsl:value-of select="."/>
                                            </Cell1>
                                            <Cell2>
                                                <xsl:value-of select="$Cell2"/>
                                            </Cell2>
                                        </test>
                                    </xsl:if>
                                </xsl:for-each>
                            </xsl:if>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="contains(Cell2,' ')">
                <xsl:variable name="Cell1">
                    <xsl:copy-of select="Cell1"/>
                </xsl:variable>
                <!-- tokenize cell2 based on space or comma -->
                <xsl:for-each select="tokenize(Cell2,' ')">
                    <xsl:variable name="str">
                        <xsl:value-of select="."/>
                    </xsl:variable>
                    <xsl:variable name="pos">
                        <xsl:value-of select="position()"/>
                    </xsl:variable>
                    <!-- if cell2 value not contained in the atomic rows collected -->
                    <xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
                        <xsl:if test="position()=$pos">
                            <test>
                                <Cell1>
                                    <xsl:value-of select="$Cell1"/>
                                </Cell1>
                                <Cell2>
                                    <xsl:value-of select="$str"/>
                                </Cell2>
                            </test>
                        </xsl:if>
                    </xsl:if>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <test>
                    <Cell1>
                        <xsl:value-of select="Cell1"/>
                    </Cell1>
                    <Cell2>
                        <xsl:value-of select="Cell2"/>
                    </Cell2>
                </test>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
  1. I have stored the cell2 that contains a single value in atomictest variable
  2. Check if Cell2 contains comma. if true tokenize Cell2 based on comma and check if the tokenized Cell2 value is there in atomic test -> if no then add Cell2 and Cell1 value to the output
  3. I would like to update the newly added Cell1 and Cell2 values in the output to the atomictest variable so that if I come through the same Cell2 value the next time I need to skip it. How to do this??

The output which I get:

<test>
    <Cell1>John</Cell1>
    <Cell2>A</Cell2>
</test>
<test>
    <Cell1>John</Cell1>
    <Cell2>B</Cell2>
</test>
<test>
    <Cell1>Jade</Cell1>
    <Cell2>Y</Cell2>
</test>
<test>
    <Cell1>John</Cell1>
    <Cell2>C</Cell2>
</test>
<test>
    <Cell1>Jade</Cell1>
    <Cell2>X</Cell2>
</test>
<test>
    <Cell1>John</Cell1>
    <Cell2>C</Cell2>
</test>
<test>
    <Cell1>John</Cell1>
    <Cell2>D</Cell2>
</test>
<test>
    <Cell1>Jade</Cell1>
    <Cell2>Y</Cell2>
</test>

Resulting output should look like the following:

<test>
        <Cell1>John</Cell1>
        <Cell2>A</Cell2>
    </test>
    <test>
        <Cell1>John</Cell1>
        <Cell2>B</Cell2>
    </test>
    <test>
        <Cell1>Jade</Cell1>
        <Cell2>Y</Cell2>
    </test>
    <test>
        <Cell1>John</Cell1>
        <Cell2>C</Cell2>
    </test>
    <test>
        <Cell1>Jade</Cell1>
        <Cell2>X</Cell2>
    </test>
    <test>
        <Cell1>John</Cell1>
        <Cell2>D</Cell2>
    </test>
  • 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-12T05:27:52+00:00Added an answer on June 12, 2026 at 5:27 am

    This XSLT 2.0 style-sheet…

    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:temp="http://stackoverflow.com/questions/12673307"
      exclude-result-prefixes="xsl temp">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*" />  
    
    <xsl:variable name="phase-1-output">
      <temp:tests>
        <xsl:apply-templates select="/*/test" mode="phase-1" />
      </temp:tests>
    </xsl:variable>
    
    <xsl:variable name="phase-2-output">
      <xsl:apply-templates select="$phase-1-output" mode="phase-2" />
    </xsl:variable>
    
    <xsl:template match="/">
     <xsl:copy-of select="$phase-2-output"/>
    </xsl:template>
    
    <xsl:template match="*" mode="phase-1" />
    
    <xsl:template match="test[Cell1!=''][Cell2!='']" mode="phase-1">
      <xsl:variable name="cell2" select="tokenize(Cell2,',')" />
      <xsl:for-each select="tokenize(Cell1,',')" >
        <xsl:variable name="cell1-pos" select="position()" />
        <xsl:variable name="cell1" select="." />
        <xsl:for-each select="tokenize($cell2[$cell1-pos],' ')">
          <temp:test>
            <temp:Cell1><xsl:value-of select="$cell1" /></temp:Cell1>
            <temp:Cell2><xsl:value-of select="." /></temp:Cell2>
          </temp:test>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="temp:tests" mode="phase-2">
      <xsl:for-each-group select="temp:test" group-by="concat(temp:Cell1,'|',temp:Cell2)">
        <test>
          <Cell1><xsl:value-of select="substring-before(current-grouping-key(),'|')" /></Cell1>
          <Cell2><xsl:value-of select="substring-after(current-grouping-key(),'|')" /></Cell2>
        </test>
      </xsl:for-each-group>
    </xsl:template>
    
    </xsl:stylesheet>
    

    …will transform this input…

    <sample>
        <test>
            <Cell1>John</Cell1>
            <Cell2>A</Cell2>
            <Cell4>xy</Cell4>
        </test>
        <test>
            <Cell1>John</Cell1>
            <Cell2>B</Cell2>
            <Cell6>10</Cell6>
        </test>
        <test>
            <Cell1>John,Jade</Cell1>
            <Cell2>A,Y</Cell2>
            <Cell4>1</Cell4>
        </test>
        <test>
            <Cell1>John,Jade</Cell1>
            <Cell2>A C,X</Cell2>
        </test>
        <test>
            <Cell1>John,Jade</Cell1>
            <Cell2>C D,Y</Cell2>
        </test>
        <test>
            <Cell1>John</Cell1>
            <Cell2>A B</Cell2>
            <Cell4>xy</Cell4>
        </test>
    </sample>
    

    …into…

    <test>
       <Cell1>John</Cell1>
       <Cell2>A</Cell2>
    </test>
    <test>
       <Cell1>John</Cell1>
       <Cell2>B</Cell2>
    </test>
    <test>
       <Cell1>Jade</Cell1>
       <Cell2>Y</Cell2>
    </test>
    <test>
       <Cell1>John</Cell1>
       <Cell2>C</Cell2>
    </test>
    <test>
       <Cell1>Jade</Cell1>
       <Cell2>X</Cell2>
    </test>
    <test>
       <Cell1>John</Cell1>
       <Cell2>D</Cell2>
    </test>
    

    Alternative solution

    Here is an alternative single phase solution. It is simpler, but less adaptable.

    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*" />  
    
    <xsl:template match="/">
      <xsl:for-each select="
         distinct-values(
           for $t in /*/test,
               $p1 in 1 to  count( tokenize($t/Cell1,',')),
               $cell1 in           tokenize($t/Cell1,',')[$p1],
               $cell2 in tokenize( tokenize($t/Cell2,',')[$p1], ' ') return
                   concat($cell1,'|',$cell2))">
        <test>
          <Cell1><xsl:value-of select="substring-before(.,'|')" /></Cell1>
          <Cell2><xsl:value-of select="substring-after( .,'|')" /></Cell2>
        </test>
      </xsl:for-each>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    Note

    Both solutions rely on the following assumptions:

    1. Both Cell1 and Cell2 have the same count of commas.
    2. Cell1 will never contain the pipe (‘|’) character.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a problem reading an XML-Feed Sample of the XML File: <f:feed xmlns:f=http://www.bbgo.de/feedxml/feed
I have a xml called sample.xml <?xml version=1.0?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget
A test sample of my xml file is shown below: test.xml <feed> <entry> <title>Link
I have a sample Xml code snippet <modification name=givenName operation=add xmlns=urn:oasis:names:tc:DSML:2:0:core> <value>Changed name</value> </modification>
I have this sample XML-RPC response: <struct> <member><name>post_id</name><value><string>131</string></value></member> <member><name>post_title</name><value><string>Test with secret password</string></value></member> <member><name>post_date</name><value><dateTime.iso8601>20080404T09:38:05</dateTime.iso8601></value></member> <member><name>post_thumbnail</name><value><string>http://localhost/~herop-kde/wordpress/wp-content/themes/twentyeleven/images/thumbnails/119.jpg</string></value></member>
I have my sample XML file: <?xml version=1.0 encoding=utf-8 ?> <contacts> <contact contactId=2> <firstName>Barney</firstName>
The following is the sample XML structure I'm working on: <command name=test> <parameter index=2>4000</parameter>
<?xml version=1.0 encoding=utf-8?> <Customers> <Customer Id=1> <Name>rtertr</Name> <DOB>2010-12-12T00:00:00</DOB> <EMail>werer@test.com</EMail> </Customer> <Customer Id=2> <Name>west</Name> <DOB>0001-01-01T00:00:00</DOB>
I wrote an XML RPC server in python and a simple Test Client for
XML sample ( original link ): <records> <record index=1> <property name=Username>Sven</property> <property name=Domain>infinity2</property> <property

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.