A Dynamic XSL Transformation
page 3 of 4
by Michelle Beall
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28260/ 79

XSL

Once the XML document has been standardized, the trans.xslt file performs the transformation of the standardized XML document into a HTML table.  The first time the z:row template is applied the table header row is created using the names of the expected attributes.  The table begins with two static columns, Type and Name, followed by a series of columns for each dynamic attribute field, and finally another static column for the Modified date.  For each z:row element a new table row is generated and each cell created. 

Type Name Dynamic Fields Modified
... data ...
... data ...

The XML document had been modified to add missing attributes with empty string values to the z:row elements.  For the dynamic attributes a test is performed to ensure that a nobreakspace is displayed when the attribute has no value:

<xsl:if test="not(normalize-space(.))">
    <xsl:value-of select="'&#160;'"/>
</xsl:if>

The assumption was that the standard data fields returned by the Sharepoint web service would always have data.

The complete XSLT follows and was saved as trans.xslt in the project.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:rs="urn:schemas-microsoft-com:rowset"
      xmlns:z="#RowsetSchema"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="user"
      exclude-result-prefixes="msxsl user">
 <xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>  
 
 <xsl:template match="rs:data"> 
  <!-- Document List -->
  <table cellspacing="0" cellpadding="0">
   <xsl:apply-templates select="z:row" />
  </table> 
 </xsl:template>
 
 <xsl:template match="z:row">
  <xsl:if test="position() = '1'">
   <!-- Table Header Row -->
   <tr>
    <!-- Fixed Column "Type" -->
    <td>
     <xsl:attribute name="class">ms-vh2</xsl:attribute>
     <xsl:attribute name="nowrap" />
     <xsl:text>  </xsl:text>
     Type
    </td>
    <!-- Fixed Column "Name" -->
    <td>
     <xsl:attribute name="class">ms-vh2</xsl:attribute>
     <xsl:attribute name="nowrap" />
     <xsl:text>  </xsl:text>
     Name 
    </td>
    <!-- Variable Columns -->
    <xsl:for-each select="./@*">
     <xsl:choose>
      <xsl:when test="name() = 'ows_Last_x0020_Modified'" />
      <xsl:when test="name() = 'ows_ID'" />
      <xsl:when test="name() = 'ows_owshiddenversion'" />
      <xsl:when test="name() = 'ows_FSObjType'" />
      <xsl:when test="name() = 'ows_FileLeafRef'" />
      <xsl:when test="name() = 'ows_Modified'" />
      <xsl:when test="name() = 'ows_FileRef'" />
      <xsl:when test="name() = 'ows_Editor'" />
      <xsl:when test="name() = 'ows_DocIcon'" />
      <xsl:otherwise>
       <td>
        <xsl:attribute name="class">ms-vh2</xsl:attribute>
        <xsl:attribute name="nowrap" />
        <xsl:text>  </xsl:text>
        <xsl:value-of select="substring-after(name(),'_')" />
       </td>
      </xsl:otherwise>
     </xsl:choose>   
    </xsl:for-each>
    <!-- Fixed Column "Modified" -->
    <td>
     <xsl:attribute name="class">ms-vh2</xsl:attribute>
     <xsl:attribute name="nowrap" />
     <xsl:text>  </xsl:text>
     Modified
    </td>  
   </tr>
  </xsl:if> 
   
  <!-- Table Detail Rows -->
  <tr>
   <!-- Fixed Column "Type" -->
   <td>
    <xsl:attribute name="class">ms-vb2</xsl:attribute> 
    <xsl:attribute name="nowrap" />
    <img>
     <xsl:attribute name="src">http://TopLevelDomain/_layouts/images/ic
      <xsl:value-of select="@ows_DocIcon"/>.gif
     </xsl:attribute>
     <xsl:attribute name="align">texttop</xsl:attribute>
     <xsl:attribute name="border">0</xsl:attribute>
    </img>
   </td>
   <!-- Fixed Column "Name" -->
   <td>
    <xsl:attribute name="class">ms-vb2</xsl:attribute> 
    <xsl:attribute name="nowrap" />
    <a> 
     <xsl:attribute name="href">http://TopLevelDomain/
      <xsl:value-of select="substring-after(@ows_FileRef,'#')"/>
     </xsl:attribute>    
     <xsl:value-of select="substring(substring-after(@ows_FileLeafRef,'#'), 1, 
                             string-length(
                               substring-after(@ows_FileLeafRef,'#'))-4)"/>
    </a>
   </td>
   <!-- Variable Columns -->
   <xsl:for-each select="@*">
    <xsl:choose>
     <xsl:when test="name()= 'ows_Last_x0020_Modified'" />
     <xsl:when test="name()= 'ows_ID'" />
     <xsl:when test="name()= 'ows_owshiddenversion'" />
     <xsl:when test="name()= 'ows_FSObjType'" />
     <xsl:when test="name()= 'ows_FileLeafRef'" />
     <xsl:when test="name()= 'ows_Modified'" />
     <xsl:when test="name()= 'ows_FileRef'" />
     <xsl:when test="name()= 'ows_Editor'" />
     <xsl:when test="name()= 'ows_DocIcon'" />
     <xsl:otherwise>
      <td>
       <xsl:attribute name="class">ms-vb2</xsl:attribute> 
       <xsl:if test="not(normalize-space(.))">
        <xsl:value-of select="'&#160;'"/>
       </xsl:if>
       <xsl:value-of select="." />
      </td>
     </xsl:otherwise>
    </xsl:choose>    
   </xsl:for-each>
   <!-- Fixed Column "Modified" -->
   <td>
    <xsl:attribute name="class">ms-vb2</xsl:attribute> 
    <xsl:attribute name="nowrap" />
    <xsl:value-of select="@ows_Modified"/>
   </td> 
  </tr> 
   </xsl:template>
 </xsl:stylesheet>


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-06-16 12:53:30 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search