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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:30:34+00:00 2026-05-23T17:30:34+00:00

i have to duplicate the xml payload into as many xml payloads based on

  • 0

i have to duplicate the xml payload into as many xml payloads based on a specific id, e.g., userid

<ns2:Details xmlns:ns2="ns">
  <ns2:var1>AA0511201143</ns2:var1>
  <ns2:var2>PARCEL</ns2:var2>
  <ns2:var3>04/04/2011</ns2:var3>
  <ns2:var4>Organization</ns2:var4>
  <ns2:UserId>46</ns2:UserId>
  <ns2:UserId>237</ns2:UserId>
</ns2:Details>

i need the output as

<ns2:Details>
  <ns2:var1>AA0511201143</ns2:var1>
  <ns2:var2>PARCEL</ns2:var2>
  <ns2:var3>04/04/2011</ns2:var3>
  <ns2:var4>Organization</ns2:var4>
  <ns2:UserId>46</ns2:UserId>
</ns2:Details>
<ns2:Details>
  <ns2:var1>AA0511201143</ns2:var1>
  <ns2:var2>PARCEL</ns2:var2>
  <ns2:var3>04/04/2011</ns2:var3>
  <ns2:var4>Organization</ns2:var4>
  <ns2:UserId>237</ns2:UserId>
</ns2:Details>

is this possible


Update: The below answer that was given is working fine, but there’s a small catch I failed to mention. If the userid is the same and it’s repeating, then the same xml payload should be displayed. For this I tried the following to get the unique elements of userid

<xsl:param name="userId" select="ns0:UserId[generate-id(.)=generate-id(key('k', ns0:UserId)[1])]"/>

but this is not working and also tried using above

..[generate-id(.)=generate-id(key('k', ns0:UserId)[1])] 

at template level also it is not working

Am I missing something?

Update :
i made a small modification to the above code, instead of working at xsl:param, i have used it at xsl:apply-template

before modification (provided as answer to me)
<xsl:apply-templates select=”//ns2:Details/ns2:UserId”/>
after modification
<xsl:apply-templates select=”//ns2:Details/ns2:UserId[generate-id(.)=generate-id(key(‘myUserId’, .)[1])]”/>

my mistake i was using ns2:userid instead of “.”

full xsl code
—

<xsl:output method="xml" indent="yes"/> <xsl:key name="k" match="ns2:UserId" use="text()"/> <xsl:key name="myUserId" match="ns2:UserId" use="."/> <xsl:template match="/"> <ns2:Root> <xsl:apply-templates select="//ns2:Details/ns2:UserId[generate-id(.)=generate-id(key('myUserId', .)[1])]"/> </ns2:Root> </xsl:template>

<xsl:template match="//ns2:Details"> <xsl:param name="userId" select="ns2:UserId"/> <ns2:Details> <xsl:copy-of select="key('k', $userId)[1]"/> <!-- displays UserId values--> <xsl:copy-of select="./*[name() != 'ns2:UserId']"/> <!-- displays other values--> </ns2:Details> </xsl:template>

<xsl:template match="ns2:UserId"> <xsl:apply-templates select=".."> <xsl:with-param name="userId" select="."/> </xsl:apply-templates> </xsl:template>

Please, validate it. this too is working for me…

  • 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-05-23T17:30:34+00:00Added an answer on May 23, 2026 at 5:30 pm

    Supposed XML:

    <ns2:Details xmlns:ns2="ns2">
      <ns2:var1>AA0511201143</ns2:var1>
      <ns2:var2>PARCEL</ns2:var2>
      <ns2:var3>04/04/2011</ns2:var3>
      <ns2:var4>Organization</ns2:var4>
      <ns2:UserId>46</ns2:UserId>
      <ns2:UserId>237</ns2:UserId>
      <ns2:UserId>46</ns2:UserId>
    </ns2:Details>
    

    XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ns2="ns2"
    >
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:key name="k" match="ns2:UserId" use="text()"/>
    
      <xsl:template match="/">
        <root>
          <xsl:apply-templates select="//ns2:Details/ns2:UserId[not(node() = preceding-sibling::node())]"/>
        </root>
      </xsl:template>
    
      <xsl:template match="//ns2:Details">
        <xsl:param name="userId" select="ns2:UserId"/>
    
        <ns2:Details>
          <xsl:copy-of select="key('k', $userId)[not(node() = preceding-sibling::node())]"/>
          <xsl:copy-of select="./*[name() != 'ns2:UserId']"/>
        </ns2:Details>
      </xsl:template>
    
      <xsl:template match="ns2:UserId">
        <xsl:apply-templates select="..">
          <xsl:with-param name="userId" select="."/>
        </xsl:apply-templates>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Output XML:

    <?xml version="1.0" encoding="utf-8"?>
    <root xmlns:ns2="ns2">
      <ns2:Details>
        <ns2:UserId>46</ns2:UserId>
        <ns2:var1>AA0511201143</ns2:var1>
        <ns2:var2>PARCEL</ns2:var2>
        <ns2:var3>04/04/2011</ns2:var3>
        <ns2:var4>Organization</ns2:var4>
      </ns2:Details>
      <ns2:Details>
        <ns2:UserId>237</ns2:UserId>
        <ns2:var1>AA0511201143</ns2:var1>
        <ns2:var2>PARCEL</ns2:var2>
        <ns2:var3>04/04/2011</ns2:var3>
        <ns2:var4>Organization</ns2:var4>
      </ns2:Details>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file containing seed data that I'm trying to load into
I have many many XML files that often contain nodes mutiple times (each time
Possible Duplicate: Parse XML Elements with LINQ I have an XML document that looks
Possible Duplicate: Best XML Parser for PHP I have an XML code like as
Possible Duplicate: Silverlight XML editor / syntax highlighting Hello, I have some XML in
Possible Duplicate: JAVA SAX parser split calls to characters() I have an XML file
Possible Duplicate: Android: How to get values in under specific xml tags Hi guys,
Possible Duplicate: What Java XML library do you recommend (to replace dom4j)? I have
Possible Duplicate: Should I use Elements or Attributes in XML? I have never been
Possible Duplicate: Need help in structuring things in XML file have a structure like

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.