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

Subject: Re: [xsl] XSLT data fetching from multiple places in XML file
From: Glenn Thomas Hvidsten <gth@xxxxxxxxx>
Date: Mon, 11 Apr 2005 16:58:20 +0200
Thanks a lot. Using some of this syntax I've gotten it to work :)
I have, however, run into another problem.

<applic>
  <type>Foobar</type>
  <model model="39C">
    <version version="39CCz" />
    <version version="39CSe" />
  </model>
  <model model="39D" />
</applic>

I want this to display the version-attributes if they exist. If they don't, I want the model-attribute to show. This example would then output:
39CCz, 39CSe, 39D


I'm using this XSLT:

  <xsl:choose>
    <xsl:when test="applic">
      <td><xsl:apply-templates select="applic/model[@model]"/></td>
    </xsl:when>
    <xsl:otherwise>
      <td>&#xA0;</td>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="applic/model[@model]">
  <xsl:choose>
    <xsl:when test="version">
      <xsl:apply-templates select="version[@version]"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="@model"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="version[@version]">
  <xsl:value-of select="@version"/>
</xsl:template>

And this gives the following (somewhat correct) result:

39CCz39CSe39D

How can I make a comma and a space between all the different versions and models?


Glenn Thomas Hvidsten



omprakash.v@xxxxxxxxxxxxx wrote:

Hi,


You have 2 meta tags one holding the joining date and other the number of
posts held.
In your calls to the 'meta' template you need to specify
which one of the 2 you would like to invoke.

This could be done by modifying the way you apply your meta template  as
follows:

  <xsl:template match="object[@type='user']">
     <xsl:apply-templates select="object[@type='meta' and
normalize-space(data[@name = 'name']) = 'posts']"/>
     <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' and
normalize-space(data[@name = 'name']) = 'joined']"/>
   </xsl:template>


Cheers, Omprakash.V















Glenn Thomas Hvidsten To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx <gth@xxxxxxxx cc: (bcc: omprakash.v/Polaris) o> Subject: [xsl] XSLT data fetching from multiple places in XML file 04/11/2005 02:02 PM Please respond to xsl-list




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






This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.


Visit Us at http://www.polaris.co.in

Current Thread