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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:11:43+00:00 2026-06-07T00:11:43+00:00

I have one XML request which I need to modify (to XML) and then

  • 0

I have one XML request which I need to modify (to XML) and then send it further. I have no prior knowledge of XSLT.
Say I have

<Combined>
<Profile>
  <Fullname>John Doe</Fullname>
  <OtherData>
    <Birthdate>1996</Birthdate>
    <FavoriteBooks>
      <Book>
        <id>1</id>
        <description>Libre1</description>
      </Book>
      <Book>
        <id>2</id>
        <description>Libre2</description>
      </Book>
      <Book>
        <id>3</id>
        <description></description>
      </Book>
      <Book>
        <id>4</id>
        <description>Libre4</description>
      </Book>
    </FavoriteBooks>
  </OtherData>
</Profile>
<LoadedData>
  <NewBirthdate>1998</NewBirthdate>
  <BooksUpdate>
    <Book id="1">
      <BookText>Book1</BookText>
    </Book>
    <Book id="2">
      <BookText>Book2</BookText>
    </Book>
    <Book id="3">
      <BookText>Book3</BookText>
    </Book>
    <Book id="4">
      <BookText>Book4</BookText>
    </Book>
    <Book id="5">
      <BookText>Book5</BookText>
    </Book>
  </BooksUpdate>
</LoadedData>

And want to get

<Profile>
<Fullname>John Doe</Fullname>
<OtherData>
  <Birthdate>1998</Birthdate>
  <FavoriteBooks>
    <Book>
      <id>1</id>
      <description>Libre1Book1</description>
    </Book>
    <Book>
      <id>2</id>
      <description>Libre2Book2</description>
    </Book>
    <Book>
      <id>3</id>
      <description>empty</description>
    </Book>
    <Book>
      <id>4</id>
      <description>Libre4Book4</description>
    </Book>
    <Book>
      <id>5</id>
      <description>new Book5</description>
    </Book>
  </FavoriteBooks>
</OtherData>

I did a pretty pathetic attempt, which obviously does not work.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <Profile>
    <xsl:apply-templates select="Combined/Profile/Fullname" />
  </Profile>
  <Otherdata>
    <Birthdate>
      <xsl:apply-templates select="Combined/LoadedData/NewBirthdate"/>
    </Birthdate>
    <FavoriteBooks>
      <xsl:for-each select="/Combined/Profile/OtherData/FavoriteBooks/Book">
        <Book>
          <id>
            <xsl:value-of select="id"/>
          </id>
          <description>
            <xsl:value-of select="description"/>
            <xsl:apply-templates select="/Combined/LoadedData/BooksUpdate/Book[@id='']" />
          </description>
        </Book>
      </xsl:for-each>
    </FavoriteBooks>
  </Otherdata>
</xsl:template>
</xsl:stylesheet>

How can I get closer to what I want to get? Could you also advise me some book to jump start, because w3schools tutorials are useless 🙁

  • 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-07T00:11:44+00:00Added an answer on June 7, 2026 at 12:11 am

    How’s this?

    According to the logic you described, the description would not be “empty” but rather “Book3” (empty string merged with “Book3”).

    <!-- root and static content -->
    <xsl:template match="/">
        <xsl:apply-templates select='Combined/Profile' />
    </xsl:template>
    
    <!-- identity/copy, with some tweaks -->
    <xsl:template match='node()|@*'>
    
        <!-- copy node -->
        <xsl:copy>
    
            <!-- add in its attributes -->
            <xsl:apply-templates select='@*' />
    
            <!-- now either apply same treatment to child nodes, or something special -->
            <xsl:choose>
    
                <!-- use updated birthdate -->
                <xsl:when test='name() = "Birthdate"'>
                    <xsl:value-of select='/Combined/LoadedData/NewBirthdate' />
                </xsl:when>
    
                <!-- merge book descriptions -->
                <xsl:when test='name() = "description"'>
                    <xsl:value-of select='concat(., /Combined/LoadedData/BooksUpdate/Book[@id = current()/../id]/BookText)' />
                </xsl:when>
    
                <!-- or just keep recursing -->
                <xsl:otherwise>
                    <xsl:apply-templates select='node()' />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    
        <!-- if we've done all books, add in any in the loaded data but not the original data -->
        <xsl:if test='name() = "Book" and not(count(following-sibling::Book))'>
            <xsl:variable name='orig_book_ids'>
                <xsl:for-each select='../Book'>
                    <xsl:value-of select='concat("-",id,"-")' />
                </xsl:for-each>
            </xsl:variable>
            <xsl:apply-templates select='/Combined/LoadedData/BooksUpdate/Book[not(contains($orig_book_ids, concat("-",@id,"-")))]' mode='new_books' />
        </xsl:if>
    </xsl:template>
    
    <!-- new books -->
    <xsl:template match='Book' mode='new_books'>
        <Book>
            <id><xsl:value-of select='@id' /></id>
            <description>new <xsl:value-of select='BookText' /></description>
        </Book>
    </xsl:template>
    

    You can run it at this XMLPlayground session (see output source).

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

Sidebar

Related Questions

Let's say I have two strings: one is XML data and the other is
I have a .xslt that translates xml from one form to another (I'm not
I have an XML variable with only one element in it. I need to
I have 3 user controls, each one of which will need to pull data
I have two request mappings in a Spring MVC 3 application, one which takes
i have one xml like, Declare @xmldata xml; set @xmldata =' <Customers> <Id>1</Id> <Name>foo</Name>
I have one XML with 20 fields in my case based on type we
I have one .net windows application I'm trying to read .xml file from c#
Hey I have downloaded one of the xml editors from some site. But don't
I have one problem , I want to get some data from XML file

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.