Can somebody help me with the following problem, here is inputxml, xslt i'm using and expected output . actually i know it is because of unique generateid not getting generated this xslt failing to generate desired output but i don't know where that code should be inserted.

InputXML

<?xml version="1.0" encoding="UTF-8"?> 
<item id="N65538" text="catalog"> 
  <item id="N65541" text="cd"> 
    <item id="N65543" text="title"> 
      <item id="N65544" img1="VAL" text="Empire Burlesque"/> 
    </item> 
    <item id="N65546" text="artist"> 
      <item id="N65547" img1="VAL" text="Bob Dylan"/> 
    </item> 
    <item id="N65549" text="country"> 
      <item id="N65550" img1="VAL" text="USA"/> 
    </item> 
    <item id="N65552" text="company"> 
      <item id="N65553" img1="VAL" text="Columbia"/> 
    </item> 
    <item id="N65555" text="price"> 
      <item id="N65556" img1="VAL" text="10.90"/> 
    </item> 
    <item id="N65558" text="year"> 
      <item id="N65559" img1="VAL" text="1985"/> 
    </item> 
  </item> 
  <item id="N65562" text="cd"> 
    <item id="N65564" text="title"> 
      <item id="N65565" img1="VAL" text="Hide your heart"/> 
    </item> 
    <item id="N65567" text="artist"> 
      <item id="N65568" img1="VAL" text="Bonnie Tyler"/> 
    </item> 
    <item id="N65570" text="country"> 
      <item id="N65571" img1="VAL" text="UK"/> 
    </item> 
    <item id="N65573" text="company"> 
      <item id="N65574" img1="VAL" text="CBS Records"/> 
    </item> 
    <item id="N65576" text="price"> 
      <item id="N65577" img1="VAL" text="9.90"/> 
    </item> 
    <item id="N65579" text="year"> 
      <item id="N65580" img1="VAL" text="1988"/> 
    </item> 
  </item> 
</item>

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:call-template name="dispatch"> 
     <xsl:with-param name="nodes" select="node()"/> 
   </xsl:call-template> 
 </xsl:template> 
 
 <xsl:template name="dispatch"> 
   <xsl:param name="nodes"/> 
   <xsl:choose> 
     <xsl:when test="text()"> 
       <xsl:call-template name="apply" > 
         <xsl:with-param name="select" select="node()" /> 
       </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
       <xsl:call-template name="apply" /> 
     </xsl:otherwise> 
   </xsl:choose> 
 </xsl:template>   
 
 <xsl:template name="apply"> 
   <xsl:param name="select" select="node()" /> 
   <xsl:for-each select="$select"> 
     <xsl:if test='local-name() !=""'> 
       <xsl:variable name="ename">  
         <xsl:for-each select="@*"> 
           <xsl:if test='name()="img1"'> 
             <xsl:text><xsl:value-of select="." /></xsl:text>  
           </xsl:if> 
         </xsl:for-each>  
       </xsl:variable>  
       <xsl:variable name="aname">  
         <xsl:for-each select="@*"> 
           <xsl:if test='name()="img"'> 
             <xsl:text><xsl:value-of select="." /></xsl:text>  
           </xsl:if> 
         </xsl:for-each>  
       </xsl:variable>  
 
       <xsl:for-each select="@*"> 
         <xsl:variable name="tname">  
           <xsl:text><xsl:value-of select="." /></xsl:text>  
         </xsl:variable>  
         <xsl:choose> 
           <xsl:when test='name() ="text" and normalize-space($ename) = "VAL" and normalize-space($aname) != "ATTR"'> 
             <xsl:element name="{$tname}">  
               <xsl:for-each select="$select"> 
                 <xsl:call-template name="dispatch"/> 
               </xsl:for-each> 
             </xsl:element>  
           </xsl:when> 
           <xsl:when test='name() ="text" and normalize-space($ename) = "VAL" '> 
             <xsl:value-of select="$tname" /> 
           </xsl:when> 
           <xsl:when test='name() ="text" and normalize-space($aname) = "ATTR"'> 
             <xsl:attribute name="id"><xsl:value-of select="$aname" /></xsl:attribute> 
           </xsl:when> 
         </xsl:choose> 
       </xsl:for-each> 
     </xsl:if> 
   </xsl:for-each> 
 </xsl:template> 
 </xsl:stylesheet>

EXPECTED OUTPUT:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog> 
  <cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
  </cd> 
  <cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
  </cd> 
</catalog>

Firstly, this XML is not well-formed. Use any root element. Suppose u added <doc>at the beginning and </doc> at last then..... use .

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
        <xsl:for-each select=".//*[@text='catalog']">
            <xsl:element name="{@text}">
                <xsl:for-each select="./*">
                    <xsl:element name="{@text}">
                        <xsl:for-each select="./*">
                            <xsl:element name="{@text}"><xsl:value-of select="./*/@text"/></xsl:element>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>   

</xsl:stylesheet>

u'll surely get the expected o/p.

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.