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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:30:03+00:00 2026-05-26T21:30:03+00:00

For input XML like: <FlightOptions> <item> <Fares> <item> <FareClass>A</FareClass> <Fare>100</Fare> <FareType>E</FareType> <Seats>5</Seats> </item> <item>

  • 0

For input XML like:

<FlightOptions>
  <item>
     <Fares>
       <item>
         <FareClass>A</FareClass>
         <Fare>100</Fare>
         <FareType>E</FareType>
         <Seats>5</Seats>
       </item>
       <item>
         <FareClass>B</FareClass>
         <Fare>200</Fare>
         <FareType>E</FareType>
         <Seats>10</Seats>
       </item>
       <item>
         <FareClass>C</FareClass>
         <Fare>250</Fare>
         <FareType>E</FareType>
         <Seats>20</Seats>
       </item>
       <item>
         <FareClass>N</FareClass>
         <Fare>100</Fare>
         <FareType>F</FareType>
         <Seats>5</Seats>
       </item>
       <item>
         <FareClass>M</FareClass>
         <Fare>200</Fare>
         <FareType>F</FareType>
         <Seats>50</Seats>
       </item>
       <item>
         <FareClass>O</FareClass>
         <Fare>300</Fare>
         <FareType>F</FareType>
         <Seats>20</Seats>
       </item>
     </Fares>
     <Flight>
         <FlightNumber>YY232</FlightNumber>
         <Origin>JFK</Origin>
         <Destination>LHR</Destination>
         <DepTime>1300</DepTime>
         <ArrTime>2000</ArrTime>
     </Flight>
    </item>
</FlightOptions>

For the above XML, I need to preserve only a few Fares/item nodes where I first group by FareType (E and F) and keep the items starting from the cheapest Fare, but stopping if the Seats is >= 9. Eg., since A has only 5 seats, I need to pick the next highest fare B, but not C. Also, if Seats >= 9, I need to cap it to 9.

I am able to do the group and sort, but unable to walk the fares and apply the logic to pick the fares as long as Seats <= 9 for that FareType. The other complication is that the output XML must reorder the Fares/items such that FareType E nodes come first in descending order of Fare, followed by N node (if present in the source), and then the other FareType F nodes in descending order of Fare.

Output XML would be:

<FlightOptions>
  <item>
     <Fares>
       <item>
         <FareClass>B</FareClass>
         <Fare>200</Fare>
         <FareType>E</FareType>
         <Seats>9</Seats>
       </item>
       <item>
         <FareClass>A</FareClass>
         <Fare>100</Fare>
         <FareType>E</FareType>
         <Seats>5</Seats>
       </item>
       <item>
         <FareClass>N</FareClass>
         <Fare>100</Fare>
         <FareType>F</FareType>
         <Seats>5</Seats>
       </item>
       <item>
         <FareClass>M</FareClass>
         <Fare>200</Fare>
         <FareType>F</FareType>
         <Seats>9</Seats>
       </item>
     </Fares>
     <Flight>
         <FlightNumber>YY232</FlightNumber>
         <Origin>JFK</Origin>
         <Destination>LHR</Destination>
         <DepTime>1300</Deptime>
         <ArrTime>2000</ArrTime>
     </Flight>
    </item>
<FlightOptions>

Have been trying to readup on Muenchian grouping examples but I am having trouble understanding how to apply along with an identity transform (since I have to keep the Flight structure along with the Fares/item nodes).

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-05-26T21:30:04+00:00Added an answer on May 26, 2026 at 9:30 pm

    You could get away with not using grouping in this instance, as really all you are doing are sorting, and just excluding some elements.

    So, when matching the Fares element, you sort the item elements, like so

         <xsl:apply-templates select="item">
            <xsl:sort select="FareType"/>
            <xsl:sort select="Fare"/>
         </xsl:apply-templates>
    

    Next, when you match each such item element, you can just test for whether any previous element with the same FareType is already greater than 9. If there are no such nodes, then because you have sorted by Fare, you know to include the node.

    <xsl:template match="Fares/item">
        <xsl:if test="not(preceding-sibling::item[FareType=current()/FareType][Seats > 9])">
           <!-- copy node -->
        </xsl:if>
    

    Here is the full XSLT…

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
       <xsl:template match="Fares">
          <xsl:copy>
             <xsl:apply-templates select="item">
                <xsl:sort select="FareType"/>
                <xsl:sort select="Fare"/>
             </xsl:apply-templates>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="Fares/item">
          <xsl:if test="not(preceding-sibling::item[FareType=current()/FareType][Seats > 9])">
             <xsl:call-template name="identity"/>
          </xsl:if>
       </xsl:template>
    
       <xsl:template match="Seats[. > 9]">
          <xsl:copy>9</xsl:copy>
       </xsl:template>
    
       <xsl:template match="@*|node()">
          <xsl:call-template name="identity"/>
       </xsl:template>
    
       <xsl:template name="identity">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output:

    <FlightOptions>
       <item>
          <Fares>
             <item>
                <FareClass>A</FareClass>
                <Fare>100</Fare>
                <FareType>E</FareType>
                <Seats>5</Seats>
             </item>
             <item>
                <FareClass>B</FareClass>
                <Fare>200</Fare>
                <FareType>E</FareType>
                <Seats>9</Seats>
             </item>
             <item>
                <FareClass>N</FareClass>
                <Fare>100</Fare>
                <FareType>F</FareType>
                <Seats>5</Seats>
             </item>
             <item>
                <FareClass>M</FareClass>
                <Fare>200</Fare>
                <FareType>F</FareType>
                <Seats>9</Seats>
             </item>
          </Fares>
          <Flight>
             <FlightNumber>YY232</FlightNumber>
             <Origin>JFK</Origin>
             <Destination>LHR</Destination>
             <DepTime>1300</DepTime>
             <ArrTime>2000</ArrTime>
          </Flight>
       </item>
    </FlightOptions>
    

    Do also note the template match for Seats to limit then to 9.

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

Sidebar

Related Questions

The input XML tag must be validated for a pattern which is like this:
I have this use case of an xml file with input like Input: <abc
I have written xml file which contains html tags as element like <component> <input
My sample input XML is: <root> <a> <b>item</b> <b>item1</b> <b>item2</b> <b>item3</b> <b>item4</b> </a> </root>
I have the following input XML: <root age=1> <description>some text</description> <section> <item name=a> <uuid>1</uuid>
I have an input XML document something like this: <text> <p> Download the software
I have a input XML like this : <in_xml> <company> <project> ProjNo1 ProjNo2 ProjNo3
I have a stored proc that takes an input of xml value like this:
I have a question. My input XML looks like <?xml version=1.0 encoding=UTF-8?> <Text> <Message>this
We have in input an XML like following: <R> <MT N=folder V=Folder1\Subfolder1 /> <MT

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.