943,152 Members | Top Members by Rank

Ad:
Jan 20th, 2010
0

XSLT problem

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <item id="N65538" text="catalog">
  3. <item id="N65541" text="cd">
  4. <item id="N65543" text="title">
  5. <item id="N65544" img1="VAL" text="Empire Burlesque"/>
  6. </item>
  7. <item id="N65546" text="artist">
  8. <item id="N65547" img1="VAL" text="Bob Dylan"/>
  9. </item>
  10. <item id="N65549" text="country">
  11. <item id="N65550" img1="VAL" text="USA"/>
  12. </item>
  13. <item id="N65552" text="company">
  14. <item id="N65553" img1="VAL" text="Columbia"/>
  15. </item>
  16. <item id="N65555" text="price">
  17. <item id="N65556" img1="VAL" text="10.90"/>
  18. </item>
  19. <item id="N65558" text="year">
  20. <item id="N65559" img1="VAL" text="1985"/>
  21. </item>
  22. </item>
  23. <item id="N65562" text="cd">
  24. <item id="N65564" text="title">
  25. <item id="N65565" img1="VAL" text="Hide your heart"/>
  26. </item>
  27. <item id="N65567" text="artist">
  28. <item id="N65568" img1="VAL" text="Bonnie Tyler"/>
  29. </item>
  30. <item id="N65570" text="country">
  31. <item id="N65571" img1="VAL" text="UK"/>
  32. </item>
  33. <item id="N65573" text="company">
  34. <item id="N65574" img1="VAL" text="CBS Records"/>
  35. </item>
  36. <item id="N65576" text="price">
  37. <item id="N65577" img1="VAL" text="9.90"/>
  38. </item>
  39. <item id="N65579" text="year">
  40. <item id="N65580" img1="VAL" text="1988"/>
  41. </item>
  42. </item>
  43. </item>
XSLT:
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  4. <xsl:template match="/">
  5. <xsl:call-template name="dispatch">
  6. <xsl:with-param name="nodes" select="node()"/>
  7. </xsl:call-template>
  8. </xsl:template>
  9.  
  10. <xsl:template name="dispatch">
  11. <xsl:param name="nodes"/>
  12. <xsl:choose>
  13. <xsl:when test="text()">
  14. <xsl:call-template name="apply" >
  15. <xsl:with-param name="select" select="node()" />
  16. </xsl:call-template>
  17. </xsl:when>
  18. <xsl:otherwise>
  19. <xsl:call-template name="apply" />
  20. </xsl:otherwise>
  21. </xsl:choose>
  22. </xsl:template>
  23.  
  24. <xsl:template name="apply">
  25. <xsl:param name="select" select="node()" />
  26. <xsl:for-each select="$select">
  27. <xsl:if test='local-name() !=""'>
  28. <xsl:variable name="ename">
  29. <xsl:for-each select="@*">
  30. <xsl:if test='name()="img1"'>
  31. <xsl:text><xsl:value-of select="." /></xsl:text>
  32. </xsl:if>
  33. </xsl:for-each>
  34. </xsl:variable>
  35. <xsl:variable name="aname">
  36. <xsl:for-each select="@*">
  37. <xsl:if test='name()="img"'>
  38. <xsl:text><xsl:value-of select="." /></xsl:text>
  39. </xsl:if>
  40. </xsl:for-each>
  41. </xsl:variable>
  42.  
  43. <xsl:for-each select="@*">
  44. <xsl:variable name="tname">
  45. <xsl:text><xsl:value-of select="." /></xsl:text>
  46. </xsl:variable>
  47. <xsl:choose>
  48. <xsl:when test='name() ="text" and normalize-space($ename) = "VAL" and normalize-space($aname) != "ATTR"'>
  49. <xsl:element name="{$tname}">
  50. <xsl:for-each select="$select">
  51. <xsl:call-template name="dispatch"/>
  52. </xsl:for-each>
  53. </xsl:element>
  54. </xsl:when>
  55. <xsl:when test='name() ="text" and normalize-space($ename) = "VAL" '>
  56. <xsl:value-of select="$tname" />
  57. </xsl:when>
  58. <xsl:when test='name() ="text" and normalize-space($aname) = "ATTR"'>
  59. <xsl:attribute name="id"><xsl:value-of select="$aname" /></xsl:attribute>
  60. </xsl:when>
  61. </xsl:choose>
  62. </xsl:for-each>
  63. </xsl:if>
  64. </xsl:for-each>
  65. </xsl:template>
  66. </xsl:stylesheet>
EXPECTED OUTPUT:
XML Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <catalog>
  3. <cd>
  4. <title>Empire Burlesque</title>
  5. <artist>Bob Dylan</artist>
  6. <country>USA</country>
  7. <company>Columbia</company>
  8. <price>10.90</price>
  9. <year>1985</year>
  10. </cd>
  11. <cd>
  12. <title>Hide your heart</title>
  13. <artist>Bonnie Tyler</artist>
  14. <country>UK</country>
  15. <company>CBS Records</company>
  16. <price>9.90</price>
  17. <year>1988</year>
  18. </cd>
  19. </catalog>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prashantc13 is offline Offline
1 posts
since Jan 2010
Feb 8th, 2010
0

Solved :)

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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
varun0703 is offline Offline
14 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: Merging XML
Next Thread in XML, XSLT and XPATH Forum Timeline: How to increment a variable in XSLT





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC