SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    ::==:: Bonzo_CS's Avatar
    Join Date
    Dec 2003
    Location
    (Cardiff Wales)
    Posts
    747
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading tag names. Be warned (Newbie)

    Hi All,

    As the title suggests I am new to XML.

    Basically I am trying to structure data from an XML file into a table. I have managed to structure data in a way that it presents well, but I need the XSL to generate a standard format to formulate different data from XML.

    The XML data I am using for example:

    <Desktop>
    <Title>Desktop Computers</Title>
    <Model>
    <Name>Dell 3400</Name>
    <CPU>AMD 3200mhz</CPU>
    <RAM>1024mb</RAM>
    <HardDisk>80gb</HardDisk>
    <DVD>52 x DVD RW</DVD>
    <Monitor>TFT 17"" - Dell</Monitor>
    <Mouse>USB/Wireless</Mouse>
    <Keyboard>USB/Wireless</Keyboard>
    <Software>Microsoft Windows XP</Software>
    <Price>799.00</Price>
    </Model>
    <Model>
    <Name>Dell 4400</Name>
    <CPU>AMD 3400mhz</CPU>
    <RAM>1024mb</RAM>
    <HardDisk>120gb</HardDisk>
    <DVD>52 x DVD RW</DVD>
    <Monitor>TFT 17"" - Dell</Monitor>
    <Mouse>USB/Wireless</Mouse>
    <Keyboard>USB/Wireless</Keyboard>
    <Software>Microsoft Windows XP, Microsoft Office XP</Software>
    <Price>849.00</Price>
    </Model>
    The XSL I have used is:

    <stylesheet>
    <body>
    <h1>
    <xsl:value-of select="Title"/>
    </h1>
    <table>
    <tbody>
    <tr>
    <th>Name</th>
    <th>CPU</th>
    <th>RAM</th>
    <th>HardDisk</th>
    <th>DVD</th>
    <th>Monitor</th>
    <th>Mouse</th>
    <th>Keyboard</th>
    <th>Software</th>
    <th>Price</th>
    </tr>
    <tr>
    <xsl:apply-templates select="Model"/>
    </tr>
    </tbody>
    </table>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="Model/Name">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/CPU">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/RAM">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/HardDisk">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/DVD">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/Monitor">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/Mouse">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/Keyboard">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/Software">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model/Price">
    <td>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    <xsl:template match="Model">
    <tr>
    <xsl:apply-templates/>
    </tr>
    </xsl:template>
    </xsl:stylesheet>

    Basically I have manually entered the data for the <th> tags, but I was wondering if there was a way to read the tag names in XML so I can extract these from the XML file to use at the <th> tags? If not is there a way to extract attributed info I could use?

    Thanks for your time guys.

  2. #2
    SitePoint Addict markchivs's Avatar
    Join Date
    Oct 2004
    Location
    Malvern Hills, UK
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you guarantee that all the header data you want to go into the <th> tags will definately exist in the first instance of <model /> in your XML data?

    Or for example could the <monitor> node not exist for a particular <model /> node - lol u get what im asking



  3. #3
    ::==:: Bonzo_CS's Avatar
    Join Date
    Dec 2003
    Location
    (Cardiff Wales)
    Posts
    747
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I get what u mean exactly. I realised that problem when I was generating the code after posting the above. As a result I have changed the heads to read standard tags now for the <th> such as specification ( only showing that which exists ).

    Cheers for pointing that out though, think it was one of my many newbie mistakes

    I have overcome the problem above now as a result of that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •