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>
Here is a simpler push style style-sheet.
This produces output as per your stated requirements, with the <not_found> nodes last.
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.