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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:13:47+00:00 2026-06-15T08:13:47+00:00

I am beginner in XSLT 1.0. I am using XSLT to transform XML to

  • 0

I am beginner in XSLT 1.0. I am using XSLT to transform XML to XML.
I would like to get first 5 unique Airline from the received XML.

Source XML:

<Response>
    <Flights>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>AB</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>CD</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>EF</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>EF</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>EF</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>EF</Airline>
                </Segment>
            </Segments>
        </Flight>
        <Flight>
            <Segments>
                <Segment>
                    <Airline>SD</Airline>
                </Segment>
            </Segments>
        </Flight>
    </Flights>
    <OtherRecommens>
        <RecommFlight>
            <Airline>XY</Airline>
        </RecommFlight>
        <RecommFlight>
            <Airline>ZZ</Airline>
        </RecommFlight>
        <RecommFlight>
            <Airline>XY</Airline>
        </RecommFlight>
        <RecommFlight>
            <Airline>AB</Airline>
        </RecommFlight>
    </OtherRecommens>
</Response>

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:variable name="Airlines" select="//Airline"/>
            <xsl:for-each select="$Airlines">
                <xsl:if test="not(preceding::Airline[.=current()/text()])">
                    <xsl:element name="SpecificAirline">
                        <xsl:value-of select="text()"/>
                    </xsl:element>
                </xsl:if>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

By applying above XSLT i get the below output:

Output:

<Root>
  <SpecificAirline>AB</SpecificAirline>
  <SpecificAirline>CD</SpecificAirline>
  <SpecificAirline>EF</SpecificAirline>
  <SpecificAirline>SD</SpecificAirline>
  <SpecificAirline>XY</SpecificAirline>
  <SpecificAirline>ZZ</SpecificAirline>
</Root>

As per my requirement i need to get only first five airlines

Expected Output:

<Root>
  <SpecificAirline>AB</SpecificAirline>
  <SpecificAirline>CD</SpecificAirline>
  <SpecificAirline>EF</SpecificAirline>
  <SpecificAirline>SD</SpecificAirline>
  <SpecificAirline>XY</SpecificAirline>
</Root>

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-15T08:13:49+00:00Added an answer on June 15, 2026 at 8:13 am

    I have used a grouping described here: how to apply group by on xslt elements

    And array index limitation 5 >= position() described here: http://www.w3schools.com/xpath/xpath_functions.asp#context

    <?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:key name="airlinetext" match="Airline" use="text()" />
    
        <xsl:template match="Response">
            <xsl:element name="Root">
                <xsl:apply-templates select="(//Airline[generate-id(.)=generate-id(key('airlinetext',text())[1])])[5&gt;=position()]"/>
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="Airline">
                    <xsl:element name="SpecificAirline">
                        <xsl:value-of select="text()"/>
                    </xsl:element>
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 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 and i am using it to transform XML to
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 am a beginner in XSLT. Below is source XML which i receive. Request
I have JDK6 and using JAXP for the transformation. I am beginner to XSLT.
Python beginner here. I am using the matplotlib library to make graphs from tab
First, I should say that I am a beginner in terms of XSLT. Although
I am a beginner and want to learn XSLT. I came upon an issue
Beginner here trying to get a pipeline working in bash. If somebody can see

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.