[xsl] XSLT data fetching from multiple places in XML file

Subject: [xsl] XSLT data fetching from multiple places in XML file
From: Glenn Thomas Hvidsten <gth@xxxxxxxxx>
Date: Mon, 11 Apr 2005 10:32:25 +0200
Hi,

I've got an XML file with data about a user. I want this data to be presented in a table in this order:

ID : Name : Posts : Username : Password : Joined : Age

My problem is that the data is not stored in exactly this way. The ID, Name and Age are stored in the top-level object tag, Username and Password in an object tag that is a child of the first tag, and Posts and Joined are sub-objects of the second tag.
How can I fetch data from all around the XML-file to get it presented the way I want?
The attached XSLT file does not work as I want it too.


This data is of course just example data constructed to have the same structure and presentation as other data I'm supposed to transform in the same way. That way modifying the XML and presenting data in another way is out of the question.


XML and XSLT below:



***** XML FILE *****


<?xml version="1.0" encoding="ISO-8859-1" ?>

<content>
  <title>Heading</title>
  <objects>

    <object type="info">
      <data name="id">
        <long>1</long>
      </data>
      <data name="name">
        <string>John Doe</string>
      </data>
      <data name="age">
        <long>25</long>
      </data>
      <object type="user">
        <data name="username">
          <string>johnd</string>
        </data>
        <data name="password">
          <string>3%g7@&%Ao1</string>
        </data>
        <object type="meta">
          <data name="name">
            <string>joined</string>
          </data>
          <data name="value">
            <string>2005-04-11</string>
          </data>
         </object>
        <object type="meta">
          <data name="name">
            <string>posts</string>
          </data>
          <data name="value">
            <string>17</string>
          </data>
         </object>
      </object>
    </object>

    <object type="info">
      ...
      ...
    </object>

  </objects>
</content>


***** XSLT FILE *****



<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:fn="http://www.w3.org/2004/07/xpath-functions"; xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes";>

  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="report">

    <html>
      <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
      <body>
        <h1><xsl:value-of select="title"/></h1>

        <table cellpadding="1" border="1">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Posts</th>
            <th>Username</th>
            <th>Password</th>
            <th>Joined</th>
            <th>Age</th>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="objects/object[@type='info']"/>
        </tbody>
        </table>
      </body>
    </html>

</xsl:template>

  <xsl:template match="object[@type='info']">
    <tr>
      <td>
        <xsl:value-of select="data[@name='id']"/>
      </td>
      <td>
        <xsl:value-of select="data[@name='name']"/>
      </td>
      <xsl:apply-templates select="object[@type='user']"/>
      <td>
        <xsl:value-of select="data[@name='age']"/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="object[@type='user']">
    <xsl:apply-templates select="object[@type='meta']"/>
    <td>
      <xsl:value-of select="data[@name='username']"/>
    </td>
    <td>
      <xsl:value-of select="data[@name='password']"/>
    </td>
    <xsl:apply-templates select="object[@type='meta']"/>
  </xsl:template>

  <xsl:template match="object[@type='meta']">
    <td>
      <xsl:value-of select="data[@name='value']"/>
    </td>
  </xsl:template>
	
</xsl:stylesheet>



--

Glenn Thomas Hvidsten

Current Thread