XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 257
Number of posts: 456

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
XSLT transform XML document to display two columns from right to left Posted by melgert on 7 Feb 2011 at 2:37 PM
Hello,

I am trying to create XSLT output that will style XML to the following criteria. I have been stuck on this for several days please help.

1. Only display a field and table row if a value is present in the XML
2. Display only two column table
3. Display the data from left to right only if a value is present

Please see sample XML, XSLT, current output table and desired output table.

Thanks in advance,

Matthew

Current XML - Employees.xml
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Employees.xslt"?>
<Employees>
  <Person>
    <LastName>Smith</LastName>
    <FirstName>John</FirstName>
    <MiddleName>David</MiddleName>
    <Generation>Jr.</Generation>
    <Address>101 Oak</Address>
    <City>Peoria </City>
    <State>AZ </State>
    <Zip>85345 </Zip>
  </Person>
  <Person>
    <LastName>Johnson</LastName>
    <FirstName>Mark</FirstName>
    <MiddleName/>
    <Generation>Sr.</Generation>
    <Address>105 Blue</Address>
    <City>New Albany </City>
    <State>IN </State>
    <Zip>70005 </Zip>
  </Person>
  <Person>
    <LastName>Walker</LastName>
    <FirstName>Jimmy</FirstName>
    <MiddleName>Frank </MiddleName>
    <Generation/>
    <Address/>
    <City/>
    <State>CA </State>
    <Zip>90052 </Zip>
  </Person>
</Employees>


Current - Employees.xslt
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet  [
  <!ENTITY nbsp   "&#160;">
  <!ENTITY copy   "&#169;">
  <!ENTITY reg    "&#174;">
  <!ENTITY trade  "&#8482;">
  <!ENTITY mdash  "&#8212;">
  <!ENTITY ldquo  "&#8220;">
  <!ENTITY rdquo  "&#8221;">
  <!ENTITY pound  "&#163;">
  <!ENTITY yen    "&#165;">
  <!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output method="html" encoding="utf-16"/>
  <xsl:template match="/">


    <xsl:for-each select="Employees/Person">

      <table border="1" style="font-size:12pt; width:50%;">
        <td>
          <p style="text-align:center; font-size:14pt; font-weight:bold; background-color:#404060; color:white;">
            (<xsl:value-of select="position()"/>&nbsp;of&nbsp;<xsl:value-of select="count(//Person)"/>&nbsp;records found)
          </p>
        </td>
      </table>
      <table border="1" style="font-size:12pt; width:50%;">
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">First Name:</span>
            <xsl:value-of select="FirstName"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Last Name:</span>
            <xsl:value-of select="LastName"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">Middle Name:</span>
            <xsl:value-of select="MiddleName"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Generation:</span>
            <xsl:value-of select="Generation"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">Address:</span>
            <xsl:value-of select="Address"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">City:</span>
            <xsl:value-of select="City"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">State:</span>
            <xsl:value-of select="State"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Zip:</span>
            <xsl:value-of select="Zip"/>
          </td>
        </tr>
      </table>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>



Current table output
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>John
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Smith
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Middle Name:David
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Generation:Jr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>Address:101 Oak
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>Peoria
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>AZ
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>85345
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Mark
        </td>
        <td>
            <span style="font-weight: bold;">Last Name: </span>Johnson
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>
        </td>
        <td>
            <span style="font-weight: bold;">Generation: </span>Sr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>105 Blue
        </td>
        <td>
            <span style="font-weight: bold;">City: </span>New Albany
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>IN
        </td>
        <td>
            <span style="font-weight: bold;">Zip: </span>70005
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Jimmy
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Walker
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Frank
        </td>
        <td>
            <span style="font-weight: bold;">Generation: </span>
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>
        </td>
        <td>
            <span style="font-weight: bold;">City: </span>
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>CA
        </td>
        <td>
            <span style="font-weight: bold;">Zip: </span>90052
        </td>
    </tr>
</table>


Desired table output
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>John
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Smith
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>David
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Jr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>101 Oak
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>Peoria
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>AZ
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>85345
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Mark
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Johnson
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Sr.
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>105 Blue
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>New Albany
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>IN
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>70005
        </td>
        <td valign="top" width="50%">
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Jimmy
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Walker
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Frank
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>CA
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>90052
        </td>
        <td valign="top" width="50%">
        </td>
    </tr>
</table>






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.