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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:38+00:00 2026-06-07T20:24:38+00:00

I am trying to get the ageCode attribute from the xml below and with

  • 0

I am trying to get the ageCode attribute from the xml below and with an xslt, put <Adults>3</Adults> if the ageCode = 10 or <Children>0</Children> if the `ageCode = 8. Here is my sample xml:

<Root>
    <Reservations>
        <Reservation>
            <Rooms>
                <Room>
                    <Guests>
                        <Guest ageCode="10" count="3" />
                        <Guest ageCode="8" count="0" />
                    </Guests>
                </Room>
            </Rooms>
        </Reservation>
    </Reservations>
</Root>

I am trying to tranform it into:

<Reservation>
    <Rooms>
        <Room>
            <Adults>3</Adults>
            <Children>0</Children>
        </Room>
    </Rooms>
</Reservation>

with the follwing XSLT, but I can’t get it to work (I am completely new to XSLT):

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Reservation">
  <xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Root/Reservations/Reservation/Rooms/Room/Guests">
 <xsl:for-each select="Guest">
  <xsl:if test="@ageCode = '10'">
    <xsl:element name="Adults">
      <xsl:value-of select="@count"/>
    </xsl:element>
  </xsl:if>
  <xsl:if test="@ageCode = '8'">
    <xsl:element name="Children">
      <xsl:value-of select="@count"/>
    </xsl:element>
  </xsl:if>
  </xsl:for-each>
  </xsl:template>
  </xsl:stylesheet>

How does XSLT work? By this I mean does it go from top to bottom applying templates? I ask because you said I needed 3 templates and with each one applied, a new transformation was made.

What does the first and second template actually do? Is there a way you could show the XML after the first is applied, then the second? Am I understanding XSLT correctly here?

What do you mean by applies deeper templates?

Would a tool like MapForce be useful if I have to create many XSLT’s?

  • 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-07T20:24:40+00:00Added an answer on June 7, 2026 at 8:24 pm

    You need 3 templates: one to discard the top 2 elements, one to match everything and just output itself and recurse, and one to do your guests -> adults transformation.

    The first one is

    <xsl:template match="Root|Reservations">
        <xsl:apply-templates/>
    </xsl:template>
    

    It outputs nothing and applies deeper templates.

    The second is

    <xsl:template match="*">
      <xsl:element name="{name(.)}">
      <xsl:apply-templates />
      </xsl:element>
    </xsl:template>
    

    It outputs itself (that is, an element with the current name) and applies deeper templates.

    The third is what you have, but just match “Guests” instead of the full path:

    <xsl:template match="Guests">
     <xsl:for-each select="Guest">
      <xsl:if test="@ageCode = '10'">
        <Adults>
          <xsl:value-of select="@count"/>
        </Adults>
      </xsl:if>
      <xsl:if test="@ageCode = '8'">
        <Children>
          <xsl:value-of select="@count"/>
        </Children>
      </xsl:if>
      </xsl:for-each>
    </xsl:template>
    

    BTW, you don’t need to use xsl:element with a constant name, just use a literal.

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

Sidebar

Related Questions

in my app i am trying to get an xml file from an URL.
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I am trying get content from the link https://....com:8455 where Server's certificate is not
When I'm trying get method from remote webservice it gives me error. My code
I am using cakephp. I am trying get data from facebook using file_get_contents. I
When trying get in memcache client, getting the below excepton. Caused by: java.io.IOException: com.fet.myclass.webservice.data.DataList
I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
Trying to get a value from a object isn't working out. class Asset <
Trying to get into PostgreSQL. I'm drawing an function that will check credentials Here
I am trying get the values using SP.The query is below. create proc [dbo].[GetOrdersByUserID11]

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.