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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:13:54+00:00 2026-06-08T09:13:54+00:00

HI i am trying to use if in an xsl to transform an input

  • 0

HI i am trying to use if in an xsl to transform an input xml to other xml.
Could able to achieve to some part unable to get accurate output. Help would be glad.

Input Xml:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Music_Model>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>4</info>
    </No_of_used>
    <Music_info>
        <Value>1234566</Value>
        <Description>
            <Value>1</Value>
        </Description>
    </Music_info>
</Music_Model>
<Music_Model>
    <Electronic>Piano</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>2</info>
    </No_of_used>
</Music_Model>
<Music_Model>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <No_of_used>
        <info>3</info>
    </No_of_used>
    <Music_info>
        <Value>128888902</Value>
        <Description>
            <Value>2</Value>
        </Description>
    </Music_info>
</Music_Model>
<Music_Model>
    <Electronic>tabala</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>40</info>
    </No_of_used>
    <Music_info>
        <Value>1298932</Value>
    </Music_info>
</Music_Model>
</Music>

Note: In the above input xml. I was interested in “If input xml have NO Musci_info element and also having Music_info but NO Description then the output xml should use elements No_of_used and should display only its value. If Music_info element and its values are available the output should be normal. I could achieve the elimination but unable to get the acurate.

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Record>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1234566</music_info_value>
    <test_level>1</test_level>
</Record>
<Record>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <music_info_value>128888902</music_info_value>
    <test_level>2</test_level>
</Record>
<not_found>
    <product>2</product>
</not_found>
</Music>

Output Displaying with the code i worked:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Record>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1234566</music_info_value>
    <test_level>1</test_level>
</Record>
<Record>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <music_info_value>128888902</music_info_value>
    <test_level>2</test_level>
</Record>
<Record>
    <Electronic>tabala</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1298932</music_info_value>
    <test_level/>
</Record>
<not_found>
    <product>4</product>
</not_found>
<not_found>
    <product>3</product>
</not_found>
<not_found>
    <product>40</product>
</not_found>
 </Music>

Code I worked:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <Music>
        <xsl:for-each select="//Music_Model">
            <xsl:if test="//Music_Model/Music_info!=Music_info ">
                <Record>
                    <Electronic>
                        <xsl:value-of select="Electronic"/>
                    </Electronic>
                    <Beats>
                        <xsl:value-of select="Beats"/>
                    </Beats>
                    <music_info_value>
                        <xsl:value-of select="Music_info/Value"/>
                    </music_info_value>
                    <test_level>
                        <xsl:value-of select="Music_info/Description/Value"/>
                    </test_level>
                </Record>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="//Music_Model">
            <xsl:if test="//Music_Model/Music_info!=Music_info">
                <not_found>
                    <product>
                        <xsl:value-of select="No_of_used/info"/>
                    </product>
                </not_found>
            </xsl:if>
        </xsl:for-each>
    </Music>
</xsl:template>

  • 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-08T09:13:56+00:00Added an answer on June 8, 2026 at 9:13 am

    Here is a simpler push style style-sheet.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
     <Music>
       <xsl:apply-templates select="*/Music_Model[    Music_info/Description ]"/>
       <xsl:apply-templates select="*/Music_Model[not(Music_info/Description)]"/>
     </Music>
    </xsl:template>
    
    <xsl:template match="Music_Model">  
     <Record> 
      <xsl:copy-of select="Electronic|Beats"/> 
      <music_info_value><xsl:value-of select="Music_info/Value" /></music_info_value>
      <test_level><xsl:value-of select="Music_info/Description/Value" /></test_level>
     </Record>  
    </xsl:template>
    
    <xsl:template match="Music_Model[not(Music_info/Description)]">  
     <not_found>
       <product><xsl:value-of select="No_of_used/info" /></product>
     </not_found>
    </xsl:template>
    
    </xsl:stylesheet>
    

    This produces output as per your stated requirements, with the <not_found> nodes last.

    <Music>
      <Record>
        <Electronic>Guitar</Electronic>
        <Beats>Pad</Beats>
        <music_info_value>1234566</music_info_value>
        <test_level>1</test_level>
      </Record>
      <Record>
        <Electronic>Guitar123</Electronic>
        <Beats>Flute</Beats>
        <music_info_value>128888902</music_info_value>
        <test_level>2</test_level>
      </Record>
      <not_found>
        <product>2</product>
      </not_found>
      <not_found>
        <product>40</product>
      </not_found>
    </Music>
    

    Note

    Note that the tabala (product 40), is included in the not_found list, because as your rules stated, it does not have a Description.

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

Sidebar

Related Questions

I'm trying to use XSL-stylesheets in order to transform some generated XML-data to HTML-output.
I am trying to use XSL to transform an XML file and can't figure
I'm trying to use an XSL file to transform an XML file into an
I am trying to use Javascript to transform part of a XML file to
I'm trying to transform some XML-data to HTML with XSLT for my bachelor thesis.
I am trying to re-use a XSL template, and place other templates within this
I am trying to generate a PDF using XSL(XML-FO) to transform a generated XML
I'm trying to use XML files and XSL stylesheets instead of ordinary phtml templates
I am trying to use XSL to transform the following WCF call and place
I am trying to get some xpath from xsl variable using xsl ver 1.0

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.