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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:37:33+00:00 2026-06-18T02:37:33+00:00

I have two XML documents I need to merge. <!– A.xml –> <cm:Process> <cm:Other

  • 0

I have two XML documents I need to merge.

<!-- A.xml -->
<cm:Process>
    <cm:Other />
    <cm:Elements />
    <cm:Request>
        <!-- stuff -->
    </cm:Request>
    <cm:ElementCouldBeHereToo />
    <cm:Request>
        <!-- stuff -->
    </cm:Request>
</cm:Process>

<!-- B.xml -->
<gateway-orders>
    <response>
        <status />
    </response>
    <response>
        <status />
    </response>
</gateway-orders>

The first is the original XML. The requests has been pulled out and sent to a system and the next is the responses. Now I need to merge these two and match request N with response N so I can pull in some info from the responses. The XSL work on A.xml and get B.xml as a parameter. To begin with I’m just trying to create a copy of the correct response in B.xml inside the request in A.xml.

The problem I have is that I thought I could use position(), but realized that won’t work since the cm:Request elements are mixed with other elements. Is there another way I can use to match up these somehow?

This is what I tried:

<xsl:import href="identity-transform.xsl" />

<xsl:param name="responses" />


<xsl:template match="cm:Request">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
        <xsl:apply-templates select="$responses/*[1]/*[position()]" />
    </xsl:copy>
</xsl:template>

There are two problems here.

  1. First of all the position() won’t match up. Is there a way to get the number/position of cm:Request elements you’re at rather than the number/position of all sibling elements? So that the first cm:Request always gives 1 irregardless of if it has any elements in front of it.
  2. Secondly I for some reason get a copy of all responses inside each request. If I change position() with for example 1, I only get a copy of the first response in each request. What am I doing wrong here?

Hoping someone knows what I should do here, cause I’m a bit blank right now and my Google-fu is failing me 😛

So to sum up, how can I match up the nth element with name blah with the nth child in a parameter node-set?

  • 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-18T02:37:34+00:00Added an answer on June 18, 2026 at 2:37 am

    How’s this:

    <xsl:template match="cm:Request">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
            <xsl:variable name="reqNum" select="count(preceding-sibling::cm:Request | .)" />
            <xsl:apply-templates select="$responses/*[1]/response[$reqNum]" />
        </xsl:copy>
    </xsl:template>
    

    To explain this:

    2.Secondly I for some reason get a copy of all responses inside each request. If I change position() with for example 1, I only get a copy of the first response in each request. What am I doing wrong here?

    In this case, position() is actually behaving differently from how you think it is. If you did this in that template:

    <xsl:variable name="pos" select="position()" />
    

    Then $pos would have the position of the current cm:Request in relation to all of its siblings, but here:

    $responses/*[1]/*[position()]
    

    position() is being evaluated in the context $responses/*[1]/*. So for the first <response> this evaluates to $responses/*[1]/*[1] and for the second response, this evaluates to $responses/*[1]/*[2], hence both are always selected.

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

Sidebar

Related Questions

I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I have two documents that I need to merge, that happen in a way
I have two xml documents with about 1000 elements in each of them and
I have two major xml elements: courses and CRNs <courses> <course credits=3 courseNum=COMP1950 name=Intermediate
I have two XML files that I need to parse into one record. The
Using xslt/xpath, I need to be able to select the two elements which have
I need to perform a comparison between two XML documents. I've been looking at
I have two array I need to merge, and using the Union (|) operator
I need to process XML documents of varying formats into records in a MySQL
I have two documents both are similar but I need to find an elegant

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.