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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:52:30+00:00 2026-06-15T03:52:30+00:00

i am beginner in XSLT and i am using it to transform XML to

  • 0

i am beginner in XSLT and i am using it to transform XML to XML

This is the source XML i receive

Source XML:

<Response>
    <Pax>
        <Id>1</Id>
    </Pax>
    <Pax>
        <Id>2</Id>
    </Pax>
    <Travelers>
        <Traveler>
            <Name>ABC</Name>
        </Traveler>
        <Traveler>
            <Name>XYZ</Name>
        </Traveler>
    </Travelers>
</Response>

I have written below XSLT

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Response">
        <xsl:element name="Root">
                <xsl:apply-templates select="Travelers/Traveler"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Traveler">
          <xsl:element name="Person">
             <xsl:element name="PId">
                   <xsl:value-of select="//Pax/Id[position()]" />
             </xsl:element>
             <xsl:element name="Name">
                   <xsl:value-of select="Name" />
             </xsl:element>
          </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output:

<Root>
  <Person>
    <PId>1</PId>
    <Name>ABC</Name>
  </Person>
  <Person>
    <PId>1</PId>
    <Name>XYZ</Name>
  </Person>
</Root>

I would like to generate below XML output

Expected Output:

<Root>
  <Person>
    <PId>1</PId>
    <Name>ABC</Name>
  </Person>
  <Person>
    <PId>2</PId>
    <Name>XYZ</Name>
  </Person>
</Root>

As shown in above XML the only issue is with PId, it should have value 2.

Please help. 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-15T03:52:32+00:00Added an answer on June 15, 2026 at 3:52 am

    Here’s a relatively simple solution.

    When this XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output omit-xml-declaration="yes" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:template match="/*">
        <Root>
          <xsl:apply-templates select="Pax" />
        </Root>
      </xsl:template>
    
      <xsl:template match="Pax">
        <xsl:variable name="vPosition" select="position()" />
        <Person>
          <PId>
            <xsl:value-of select="Id" />
          </PId>
          <Name>
            <xsl:value-of select="/*/Travelers/*[$vPosition]/Name" />
          </Name>
        </Person>
      </xsl:template>
    </xsl:stylesheet>
    

    …is applied to the original XML:

    <Response>
      <Pax>
        <Id>1</Id>
      </Pax>
      <Pax>
        <Id>2</Id>
      </Pax>
      <Travelers>
        <Traveler>
          <Name>ABC</Name>
        </Traveler>
        <Traveler>
          <Name>XYZ</Name>
        </Traveler>
      </Travelers>
    </Response>
    

    …the wanted result is produced:

    <Root>
      <Person>
        <PId>1</PId>
        <Name>ABC</Name>
      </Person>
      <Person>
        <PId>2</PId>
        <Name>XYZ</Name>
      </Person>
    </Root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner in XSLT 1.0. I am using XSLT to transform XML to
I am a beginner in XSLT. Below is source XML which i receive. Request
I am a beginner to XSLT. My Source XML is as below: <Passengers> <Passenger
I am beginner in XSLT. My Source XML is as below <Options> <Option> <Data>Data1</Data>
I am beginner in XSLT. My Source XML: <Request> <Documents> <Doc Type=A>123</Doc> <Doc Type=C>345</Doc>
I have JDK6 and using JAXP for the transformation. I am beginner to XSLT.
I am a beginner and want to learn XSLT. I came upon an issue
I'm a beginner at XSL FO. I have a very simple XML document :
Beginner here: In this answer to my question of how to insert data into
Beginner at Django here, I've been trying to fix this for a long time

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.