Problems with XML transformation with XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scripts08
    New Member
    • Mar 2008
    • 2

    #1

    Problems with XML transformation with XSLT

    Hi,

    I am new to XSLT. Having problems creating an HTML table (specifically the headers) from an XML file like the following:

    <group>
    <categorylist >
    <category>car </category>
    <description>be ntly</description>
    </categorylist>

    <categorylist >
    <category>car </category>
    <description>bm w</description>
    </categorylist>

    <categorylist >
    <category>compu ter</category>
    <description>de ll</description>
    </categorylist>
    </group>

    So, I need to create a table where the first header (TH) should be 'car' with 2 TRs for 'bently' and 'bmw'. The next header should be 'computer' with only one TR for 'dell'. So, for each categorylist, I need to create a header for all categories with the same value and TRs for their corresponding 'description'.

    Any help would be greatly appreciated.

    Thanks.
  • saran3b2
    New Member
    • Aug 2007
    • 19

    #2
    hi declare the variable for category and description
    like this

    <xsl:key name="keyCatego ry" match="category list" use="category"/>
    <xsl:key name="keyDescri ption" match="category list" use="descriptio n"/>


    and apply ur xslt coding like these


    <xsl:for-each select="$group" >
    <xsl:variable name="category" select="categor y"/>
    <xsl:variable name="descripti on" select="descrip tion"/>
    <xsl:if test="generate-id(.)=generate-id($group[category=$categ ory])">
    <tr>
    <th><xsl:valu e-of select="categor y"/></th>
    </tr>
    <tr>
    <td><xsl:valu e-of select="descrip tion"/></td>
    </tr>

    </xsl:if>

    </xsl:for-each>


    ok
    -------------------------------------------------------------------------------------------------------


    Originally posted by scripts08
    Hi,

    I am new to XSLT. Having problems creating an HTML table (specifically the headers) from an XML file like the following:

    <group>
    <categorylist >
    <category>car </category>
    <description>be ntly</description>
    </categorylist>

    <categorylist >
    <category>car </category>
    <description>bm w</description>
    </categorylist>

    <categorylist >
    <category>compu ter</category>
    <description>de ll</description>
    </categorylist>
    </group>

    So, I need to create a table where the first header (TH) should be 'car' with 2 TRs for 'bently' and 'bmw'. The next header should be 'computer' with only one TR for 'dell'. So, for each categorylist, I need to create a header for all categories with the same value and TRs for their corresponding 'description'.

    Any help would be greatly appreciated.

    Thanks.

    Comment

    • scripts08
      New Member
      • Mar 2008
      • 2

      #3
      Thank you very much for your response. For some reason, its not working for me. I am not sure why. Any idea? I checked several times to make sure I don't have any typo or anything but still no luck.



      Originally posted by saran3b2
      hi declare the variable for category and description
      like this

      <xsl:key name="keyCatego ry" match="category list" use="category"/>
      <xsl:key name="keyDescri ption" match="category list" use="descriptio n"/>


      and apply ur xslt coding like these


      <xsl:for-each select="$group" >
      <xsl:variable name="category" select="categor y"/>
      <xsl:variable name="descripti on" select="descrip tion"/>
      <xsl:if test="generate-id(.)=generate-id($group[category=$categ ory])">
      <tr>
      <th><xsl:valu e-of select="categor y"/></th>
      </tr>
      <tr>
      <td><xsl:valu e-of select="descrip tion"/></td>
      </tr>

      </xsl:if>

      </xsl:for-each>


      ok
      -------------------------------------------------------------------------------------------------------

      Comment

      • saran3b2
        New Member
        • Aug 2007
        • 19

        #4
        hi,

        here ouput is the clear. Please verify in ur xml and xsl coding.


        <?xml version="1.0" encoding="UTF-8"?>
        <?xml-stylesheet type="text/xsl" href="group.xsl "?>
        <group>
        <categorylist >
        <category>car </category>
        <description>be ntly</description>
        </categorylist>

        <categorylist >
        <category>car </category>
        <description>bm w</description>
        </categorylist>

        <categorylist >
        <category>compu ter</category>
        <description>de ll</description>
        </categorylist>
        </group>

        Simply copy and paste this coding for out put
        ------------------------------------------------------

        group.xsl

        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:styleshe et version="2.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
        <xsl:variable name="group" select="//categorylist"/>
        <!-- Define keys used to group elements -->
        <xsl:key name="keyCatego ry" match="category list" use="category"/>
        <xsl:key name="keyDescri ption" match="category list" use="descriptio n"/>
        <xsl:template match="/">
        <html>
        <head>
        <title>Cengag e Production Report</title>
        </head>
        <body>
        <center>******* *************** *************** ***</center>
        <center>
        <font size="6" color="brown">P reMedia Global P. Ltd.</font>
        </center>
        <center>******* *************** *************** ***</center>
        <center>
        <font size="4" color="blue">Ce ngage Production Report</font>
        </center>
        <br/>
        <table width="100%" frame="box" border="2" align="center">
        <tbody>
        <xsl:apply-templates/>
        </tbody>
        </table>
        </body>
        </html>
        </xsl:template>



        <xsl:template match="group" name="Showprodu ctionsInTeam">

        <xsl:for-each select="$group" >
        <xsl:variable name="category" select="categor y"/>
        <xsl:variable name="descripti on" select="descrip tion"/>
        <xsl:if test="generate-id(.)=generate-id($group[category=$categ ory])">
        <tr>
        <th><xsl:valu e-of select="categor y"/></th>
        </tr>
        </xsl:if>
        <tr>
        <td><xsl:valu e-of select="descrip tion"/></td>
        </tr>
        </xsl:for-each>
        </xsl:template>
        </xsl:stylesheet>

        Reply me if ouput clear

        Regards
        vinoth K



        Originally posted by scripts08
        Thank you very much for your response. For some reason, its not working for me. I am not sure why. Any idea? I checked several times to make sure I don't have any typo or anything but still no luck.

        Comment

        • saran3b2
          New Member
          • Aug 2007
          • 19

          #5
          hi,

          Please ignore the previous mail.

          here is ur xml

          <?xml version="1.0" encoding="UTF-8"?>
          <?xml-stylesheet type="text/xsl" href="group.xsl "?>
          <group>
          <categorylist >
          <category>car </category>
          <description>be ntly</description>
          </categorylist>

          <categorylist >
          <category>car </category>
          <description>bm w</description>
          </categorylist>

          <categorylist >
          <category>compu ter</category>
          <description>de ll</description>
          </categorylist>
          </group>


          Simply copy and paste this coding for out put
          ------------------------------------------------------

          group.xsl

          <?xml version="1.0" encoding="UTF-8"?>
          <xsl:styleshe et version="2.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
          <xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
          <xsl:variable name="group" select="//categorylist"/>
          <!-- Define keys used to group elements -->
          <xsl:key name="keyCatego ry" match="category list" use="category"/>
          <xsl:key name="keyDescri ption" match="category list" use="descriptio n"/>
          <xsl:template match="/">
          <html>
          <head>
          <title>Report </title>
          </head>
          <body>
          <table width="100%" frame="box" border="2" align="center">
          <tbody>
          <xsl:apply-templates/>
          </tbody>
          </table>
          </body>
          </html>
          </xsl:template>



          <xsl:template match="group" name="Showprodu ctionsInTeam">

          <xsl:for-each select="$group" >
          <xsl:variable name="category" select="categor y"/>
          <xsl:variable name="descripti on" select="descrip tion"/>
          <xsl:if test="generate-id(.)=generate-id($group[category=$categ ory])">
          <tr>
          <th><xsl:valu e-of select="categor y"/></th>
          </tr>
          </xsl:if>
          <tr>
          <td><xsl:valu e-of select="descrip tion"/></td>
          </tr>
          </xsl:for-each>
          </xsl:template>
          </xsl:stylesheet>

          Comment

          Working...