[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XSLT data fetching from multiple places in XML file

Subject: XSLT data fetching from multiple places in XML file
From: Glenn Thomas Hvidsten <gth@xxxxxxxxx>
Date: Mon, 11 Apr 2005 10:32:25 +0200
xslt data
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.