ASP.NET: How to convert schema to Excel xsl - asp.net

I have the following xml schema:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0">
<xs:element name="PercentTimeReport">
<xs:complexType>
<xs:sequence>
<xs:element name="ParamStartDate" type="xs:dateTime" />
<xs:element name="ParamEndDate" type="xs:dateTime" />
<xs:element name="ParamQuarterInt" type="xs:unsignedByte" />
<xs:element name="ParamProjID" nillable="true" />
<xs:element name="ParamStaffID" nillable="true" />
<xs:element name="ParamPercentRange" type="xs:unsignedByte" />
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:unsignedShort" />
<xs:element name="EmployeeName" type="xs:string" />
<xs:element name="StaffID" type="xs:unsignedShort" />
<xs:element name="Status" type="xs:string" />
<xs:element name="Date" type="xs:dateTime" />
<xs:element name="Department" type="xs:string" />
<xs:element name="DepartmentCode" type="xs:string" />
<xs:element name="Project" type="xs:string" />
<xs:element name="ProjectID" type="xs:unsignedByte" />
<xs:element name="Hours" type="xs:unsignedByte" />
<xs:element name="HoursPerWeek" type="xs:decimal" />
<xs:element name="PercentTime" type="xs:decimal" />
<xs:element name="ActualContact" type="xs:boolean" />
<xs:element name="Body" type="xs:string" />
<xs:element name="Issue" type="xs:string" />
<xs:element name="Activity" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsd:schema>
and I'd like to generate an excel file that only shows the Items table in an ASP.NET 2.0 Web application. I really don't understand this code that I found (see below), or XSLT enough to get the output format that I need. Does anyone know XSLT, that could tell me how to modify the XSLT below to:
1) Hide the elements other than Items (e.g. ParamStartDate, ParamEndDate, etc.).
2) Output the table with the nested "Items" complex element.
Currently, the xsl below produces each of the elements as column headers, and the Items cell contains all the items on one row. I essentially need to go one level deeper.
<xsl:template match="/">
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<xsl:template match="/*">
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select="local-name(/*/*)"/>
</xsl:attribute>
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="*[position() = 1]/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates/>
</Table>
</Worksheet>
</xsl:template>
<xsl:template match="/*/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="/*/*/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:template>
Desired Excel Output (Only show Items complexType as Table):
ID EmployeeName StaffID .... Issue Activity
1 John Smith 231 .... text text
2 Kate Henderson 101 .... text2 text3
. ....
. ....
N ....
Current Output:
ParamStartDate ParamEndDate .... ParamPercentRange Items
'01/01/2010' '04/01/2010' .... 6 '1John Smith231...texttext....'
As always, any help greatly appreciated.
Thank You
P.S. Alejandro, here's the output with your changes:
<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="PercentTimeReport">
<Table x:FullColumns="1" x:FullRows="1">
<Row />
</Table>
</Worksheet>
</Workbook>
Here is a sample of my original output:
<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
<Worksheet ss:Name="PercentTimeReport">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<Cell><Data ss:Type="String">ParamStartDate</Data></Cell><Cell><Data ss:Type="String">ParamEndDate</Data></Cell><Cell><Data ss:Type="String">ParamQuarterInt</Data></Cell><Cell><Data ss:Type="String">ParamPercentRange</Data></Cell><Cell><Data ss:Type="String">Items</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">2010-07-01T00:00:00</Data></Cell><Cell><Data ss:Type="String">2010-09-30T00:00:00</Data></Cell>....</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

Inside the template that match /* your context (following the schema you've privided) is PercentTimeReport root element. It seems that you want to iterate trougth child elements of PercentTimeReport/Items/Item.
So change:
<xsl:for-each select="*[position() = 1]/*">
For:
<xsl:for-each select="Items/Item[1]/*">
And this:
<xsl:apply-templates/>
For:
<xsl:apply-templates select="Items/Item"/>
And this patterns:
<xsl:template match="/*/*">
<xsl:template match="/*/*/*">
For:
<xsl:template match="Item">
<xsl:template match="Item/*">
Edit: This input (following schema)
<PercentTimeReport>
<ParamStartDate>ParamStartDate</ParamStartDate>
<ParamEndDate>ParamEndDate</ParamEndDate>
<ParamQuarterInt>ParamQuarterInt</ParamQuarterInt>
<ParamProjID>ParamProjID</ParamProjID>
<ParamStaffID>ParamStaffID</ParamStaffID>
<ParamPercentRange>ParamPercentRange</ParamPercentRange>
<Items>
<Item>
<ID>ID1</ID>
<EmployeeName>EmployeeName1</EmployeeName>
<StaffID>StaffID1</StaffID>
<Status>Status1</Status>
<Date>Date1</Date>
<Department>Department1</Department>
<DepartmentCode>DepartmentCode1</DepartmentCode>
<Project>Project1</Project>
<ProjectID>ProjectID1</ProjectID>
<Hours>Hours1</Hours>
<HoursPerWeek>HoursPerWeek1</HoursPerWeek>
<PercentTime>PercentTime1</PercentTime>
<ActualContact>ActualContact1</ActualContact>
<Body>Body1</Body>
<Issue>Issue1</Issue>
<Activity>Activity1</Activity>
</Item>
<Item>
<ID>ID2</ID>
<EmployeeName>EmployeeName2</EmployeeName>
<StaffID>StaffID2</StaffID>
<Status>Status2</Status>
<Date>Date2</Date>
<Department>Department2</Department>
<DepartmentCode>DepartmentCode2</DepartmentCode>
<Project>Project2</Project>
<ProjectID>ProjectID2</ProjectID>
<Hours>Hours2</Hours>
<HoursPerWeek>HoursPerWeek2</HoursPerWeek>
<PercentTime>PercentTime2</PercentTime>
<ActualContact>ActualContact2</ActualContact>
<Body>Body2</Body>
<Issue>Issue2</Issue>
<Activity>Activity2</Activity>
</Item>
<Item>
<ID>ID3</ID>
<EmployeeName>EmployeeName3</EmployeeName>
<StaffID>StaffID3</StaffID>
<Status>Status3</Status>
<Date>Date3</Date>
<Department>Department3</Department>
<DepartmentCode>DepartmentCode3</DepartmentCode>
<Project>Project3</Project>
<ProjectID>ProjectID3</ProjectID>
<Hours>Hours3</Hours>
<HoursPerWeek>HoursPerWeek3</HoursPerWeek>
<PercentTime>PercentTime3</PercentTime>
<ActualContact>ActualContact3</ActualContact>
<Body>Body3</Body>
<Issue>Issue3</Issue>
<Activity>Activity3</Activity>
</Item>
<Item>
<ID>ID4</ID>
<EmployeeName>EmployeeName4</EmployeeName>
<StaffID>StaffID4</StaffID>
<Status>Status4</Status>
<Date>Date4</Date>
<Department>Department4</Department>
<DepartmentCode>DepartmentCode4</DepartmentCode>
<Project>Project4</Project>
<ProjectID>ProjectID4</ProjectID>
<Hours>Hours4</Hours>
<HoursPerWeek>HoursPerWeek4</HoursPerWeek>
<PercentTime>PercentTime4</PercentTime>
<ActualContact>ActualContact4</ActualContact>
<Body>Body4</Body>
<Issue>Issue4</Issue>
<Activity>Activity4</Activity>
</Item>
</Items>
</PercentTimeReport>
With this stylesheet (Yours plus my changes)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:template match="/">
<Workbook>
<xsl:apply-templates/>
</Workbook>
</xsl:template>
<xsl:template match="/*">
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select="local-name(/*/*)"/>
</xsl:attribute>
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="Items/Item[1]/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates select="Items/Item"/>
</Table>
</Worksheet>
</xsl:template>
<xsl:template match="Item">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>
<xsl:template match="Item/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:template>
</xsl:stylesheet>
Output:
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="ParamStartDate">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<Cell>
<Data ss:Type="String">ID</Data>
</Cell>
<Cell>
<Data ss:Type="String">EmployeeName</Data>
</Cell>
<Cell>
<Data ss:Type="String">StaffID</Data>
</Cell>
<Cell>
<Data ss:Type="String">Status</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date</Data>
</Cell>
<Cell>
<Data ss:Type="String">Department</Data>
</Cell>
<Cell>
<Data ss:Type="String">DepartmentCode</Data>
</Cell>
<Cell>
<Data ss:Type="String">Project</Data>
</Cell>
<Cell>
<Data ss:Type="String">ProjectID</Data>
</Cell>
<Cell>
<Data ss:Type="String">Hours</Data>
</Cell>
<Cell>
<Data ss:Type="String">HoursPerWeek</Data>
</Cell>
<Cell>
<Data ss:Type="String">PercentTime</Data>
</Cell>
<Cell>
<Data ss:Type="String">ActualContact</Data>
</Cell>
<Cell>
<Data ss:Type="String">Body</Data>
</Cell>
<Cell>
<Data ss:Type="String">Issue</Data>
</Cell>
<Cell>
<Data ss:Type="String">Activity</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">ID1</Data>
</Cell>
<Cell>
<Data ss:Type="String">EmployeeName1</Data>
</Cell>
<Cell>
<Data ss:Type="String">StaffID1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Status1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Department1</Data>
</Cell>
<Cell>
<Data ss:Type="String">DepartmentCode1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Project1</Data>
</Cell>
<Cell>
<Data ss:Type="String">ProjectID1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Hours1</Data>
</Cell>
<Cell>
<Data ss:Type="String">HoursPerWeek1</Data>
</Cell>
<Cell>
<Data ss:Type="String">PercentTime1</Data>
</Cell>
<Cell>
<Data ss:Type="String">ActualContact1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Body1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Issue1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Activity1</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">ID2</Data>
</Cell>
<Cell>
<Data ss:Type="String">EmployeeName2</Data>
</Cell>
<Cell>
<Data ss:Type="String">StaffID2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Status2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Department2</Data>
</Cell>
<Cell>
<Data ss:Type="String">DepartmentCode2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Project2</Data>
</Cell>
<Cell>
<Data ss:Type="String">ProjectID2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Hours2</Data>
</Cell>
<Cell>
<Data ss:Type="String">HoursPerWeek2</Data>
</Cell>
<Cell>
<Data ss:Type="String">PercentTime2</Data>
</Cell>
<Cell>
<Data ss:Type="String">ActualContact2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Body2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Issue2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Activity2</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">ID3</Data>
</Cell>
<Cell>
<Data ss:Type="String">EmployeeName3</Data>
</Cell>
<Cell>
<Data ss:Type="String">StaffID3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Status3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Department3</Data>
</Cell>
<Cell>
<Data ss:Type="String">DepartmentCode3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Project3</Data>
</Cell>
<Cell>
<Data ss:Type="String">ProjectID3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Hours3</Data>
</Cell>
<Cell>
<Data ss:Type="String">HoursPerWeek3</Data>
</Cell>
<Cell>
<Data ss:Type="String">PercentTime3</Data>
</Cell>
<Cell>
<Data ss:Type="String">ActualContact3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Body3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Issue3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Activity3</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">ID4</Data>
</Cell>
<Cell>
<Data ss:Type="String">EmployeeName4</Data>
</Cell>
<Cell>
<Data ss:Type="String">StaffID4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Status4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Date4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Department4</Data>
</Cell>
<Cell>
<Data ss:Type="String">DepartmentCode4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Project4</Data>
</Cell>
<Cell>
<Data ss:Type="String">ProjectID4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Hours4</Data>
</Cell>
<Cell>
<Data ss:Type="String">HoursPerWeek4</Data>
</Cell>
<Cell>
<Data ss:Type="String">PercentTime4</Data>
</Cell>
<Cell>
<Data ss:Type="String">ActualContact4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Body4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Issue4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Activity4</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

Related

Displaying items from a specific tag via attributes

basically what i want to do right now is to display items from a specific tag. I am planning to differentiate the tags with unique attribute. The problem i facing right now is that i doesnt seem to be working as it is still displaying items from another tag. Please help.
This is the XML content
<group name="Personal Particulars">
<field is_admin_field="N" required="Y">
<question_title>Name</question_title>
<type>TextField</type>
<db_field_name>name</db_field_name>
<db_field_length>250</db_field_length>
<db_field_type>string</db_field_type>
<additional_comment/>
</field>
<field is_admin_field="N" required="Y">
<question_title>NRIC/FIN</question_title>
<type>TextField</type>
<db_field_name>nricfin</db_field_name>
<db_field_length>20</db_field_length>
<db_field_type>string</db_field_type>
<additional_comment/>
</field>
<field is_admin_field="N" required="Y">
<question_title>Contact No.</question_title>
<type>TextField</type>
<db_field_name>contact_no</db_field_name>
<db_field_length>20</db_field_length>
<db_field_type>string</db_field_type>
<additional_comment/>
</field>
</group>
<group name="Housing Type">
<field is_admin_field="N" required="Y">
<question_title>Which of the housing type best describes your residential?</question_title>
<type>List</type>
<db_field_name>which_of_the_housing_type_best_describes_your_residential</db_field_name>
<options>
<item score="0">3 - 5 room HDB</item>
<item score="0">Executive Condominium </item>
<item score="0">Landed 1 Floor</item>
<item score="0">Landed 2 Floor</item>
<item score="0">Landed 3 Floor</item>
<item score="0">Landed 4 Floor</item>
<item score="0">Landed 5 Floor</item>
</options>
<db_field_length>22</db_field_length>
<additional_comment/>
</field>
</group>
This is the XSLT
<div style="border:1px solid black">
<div style="border:1px solid black">
<xsl:variable name="name" select="/form/fieldset/group/#name"/>
<xsl:if test="$name='Personal Particulars'">
<xsl:text>Personal Particulars</xsl:text>
<xsl:for-each select="form/fieldset/group/field">
<div>
<xsl:value-of select="question_title"/>
</div>
<xsl:variable name="type" select="type"/>
<xsl:if test="$type='TextField'">
<xsl:element name="input">
<xsl:attribute name="type">textbox</xsl:attribute>
<xsl:attribute name="maxlength">5</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:if>
</div>
<div style="border:1px solid black">
<xsl:variable name="name" select="/form/fieldset/group/#name"/>
<xsl:if test="$name='Housing Type'">
<xsl:text>Housing Type</xsl:text>
<xsl:for-each select="form/fieldset/group/field">
<div>
<xsl:value-of select="form/fieldset/group/question_title"/>
</div>
<xsl:variable name="type" select="type"/>
<xsl:if test="$type='TextField'">
<xsl:element name="input">
<xsl:attribute name="type">textbox</xsl:attribute>
<xsl:attribute name="maxlength">5</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:if>
</div>
</div>
Try this. You will have to adjust the XPath in for-each loops to match the actual XML structure-
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<xsl:for-each select="item">
<div style="font-size:150%;margin-top:5%;">
<xsl:value-of select="title"/>
</div>
<div style="font-size:150%;">
<xsl:value-of select="description"/>
</div>
<xsl:variable name="category" select="category"/>
<xsl:if test= "$category = 'Text' " >
<xsl:for-each select="./options/need">
<div>
<xsl:element name="input">
<xsl:attribute name="type">checkbox</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="need"/>
</xsl:attribute>
</xsl:element>
<label>
<xsl:value-of select="."/>
</label>
</div>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
OUTPUT-
Having a variable like <xsl:variable name="category" select="category"/> will give you the ability to put the conditional <xsl:if test= "$category = 'Text' " > anywhere you want within the nested for-each loops
As per the follow-up comment, adding how to display radio buttons-
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">something</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="need"/>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<label>
<xsl:value-of select="."/>
</label>

XSL stylesheet template not generating proper visual elements

When viewing the XML file (congress.xml) in my browser, the drawCells template in election.xsl doesn't seem to be generating the proper visual elements. There is supposed to be a bar graph generated but it is not.
Open congress.xml in a browser. Open elections.xsl in a text editor.
The files are...
congress.xml
election.xsl
candidates.xml
vwstyles.css
vwlogo.png
Here is a google drive link to the all the files...
https://drive.google.com/folderview?id=0B9o30hEqwvyDc2Y3MktHNDQydnc&usp=sharing
Here is an image of what is supposed to be happening...
vw.jpg
If you think you can see why this is not happening by looking at the election.xsl stylesheet itelself, here it is...
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
doctype-system="about:legacy-compat"
encoding="UTF-8"
indent="yes" />
<xsl:variable name="candidateInfo" select="document('candidates.xml')/candidates/candidate[#candidateID]" />
<xsl:template match="/">
<html>
<head>
<title>Minnesota Congressional Election Results</title>
<link href="vwstyles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrap">
<header>
<img src="vwlogo.png" alt="Voter Web" />
</header>
<h1>Minnesota Congressional Election Results</h1>
<section id="votingResults">
<xsl:apply-templates select="congressResults/district" />
</section>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="district">
<h2>District <xsl:value-of select="#dNumber" /></h2>
<table class="electionTable">
<thead>
<tr>
<th>Candidate</th>
<th>Votes</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="candidates/candidate" />
</tbody>
</table>
</xsl:template>
<xsl:template match="candidate">
<tr>
<xsl:variable name="candidateVotes" select="sum(votes)" />
<xsl:variable name="totalVotes" select="sum(..//votes)" />
<xsl:variable name="candidatePercent" select="($candidateVotes) div ($totalVotes)" />
<xsl:variable name="candidateName" select="$candidateInfo[#candidateID=current()/#candidateID]/name" />
<xsl:variable name="candidateParty" select="$candidateInfo[#candidateID=current()/#candidateID]/party" />
<th>
<xsl:value-of select="$candidateName" />
(<xsl:value-of select="$candidateParty" />)
</th>
<th>
<xsl:value-of select="format-number($candidateVotes, '###,##0')" />
(<xsl:value-of select="format-number($candidatePercent, '#0.0%')" />)
</th>
<td>
<xsl:call-template name="drawCells">
<xsl:with-param name="cellCount" select="round(100 * $candidatePercent)"/>
<xsl:with-param name="party" select="$candidateParty" />
</xsl:call-template>
</td>
</tr>
</xsl:template>
<xsl:template name="drawCells">
<xsl:param name="cellCount" />
<xsl:param name="party" />
<xsl:if test="$cellCount > 0">
<td class="{$party}"></td>
<xsl:call-template name="drawCells">
<xsl:with-param name="cellCount" select="$cellCount - 1" />
<xsl:with-param name="party" select="$party" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
You wrap the call to drawCells in an own td element, therefore nesting the tds:
<td>
<xsl:call-template name="drawCells">...</<xsl:call-template>
</td>
Instead simply do
<xsl:call-template name="drawCells">...</<xsl:call-template>
At least this fixes the output in Firefox.

how to set margin dynamically on XSLT

I'm trying to achieve a tree view of an xml file using XSLT and css.
I have nested children in xml and I'm iterating through them to display content of each entry.
Each child is a logger with message entries. I would like to have each child table indented according to ancestor count which i get correctly. however trying to set the table margin dynamically margin-left="$margin*10" doesnot work.
This is my XSLT / xml
<xsl:template match="operationLogger">
<xsl:variable name="margin" select="count(ancestor::*) * 10"/>
<table class="normalTable" cols="4">
<xsl:attribute name="margin-left">
<xsl:value-of select="$margin"/>
</xsl:attribute>
<tr class="errorsCollectionTitle">
<td colspan="4">
<xsl:value-of select="#name"/>
</td>
</tr>
<xsl:if test="operationLogEntry">
<xsl:apply-templates select="operationLogEntry"/>
</xsl:if>
<xsl:if test="operationLogger">
<xsl:apply-templates select="operationLogger"/>
</xsl:if>
</table>
</xsl:template>
<xsl:template match="operationLogEntry">
<tr>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">rowData</xsl:when>
<xsl:otherwise>rowAlternatingData</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<td>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="#level = 'Error'">errorImage</xsl:when>
<xsl:when test="#level = 'Warning'">warningImage</xsl:when>
<xsl:when test="#level = 'Info'">informationImage</xsl:when>
<xsl:when test="#level = 'Debug'">informationImage</xsl:when>
</xsl:choose>
</xsl:attribute>
</td>
<td class="timeStamp">
<xsl:value-of select="#timeStamp"/>
</td>
<td class="source">
<xsl:value-of select="../loggerSource/#computer" />
/
<xsl:value-of select="../loggerSource/#user" />
</td>
<td class="message">
<div>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="#level = 'Error'">errorColor</xsl:when>
<xsl:when test="#level = 'Warning'">warningColor</xsl:when>
<xsl:when test="#level = 'Info'">informationColor</xsl:when>
<xsl:when test="#level = 'Debug'">informationColor</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="#message"/>
</div>
</td>
</tr>
</xsl:template>
<operationLogger name="">
<loggerSource domain="user" computer="computer" user="account" />
<operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Adding 1 to transactions list" />
<operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Executing 1 transactions..." />
<operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Executed 1 transactions successfully!" />
<operationLogger name="TransactionOperationCollection.Execute()">
<loggerSource domain="user" computer="computer" user="account" />
<operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Commiting transaction 0:Transaction" />
<operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Committed transaction 0:Transaction" />
<operationLogger name="TransactionOperation.Commit()">
<operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Setting Auditonly" />
<operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Succesfully changed Audit Only" />
</operationLogger>
</operationLogger>
</operationLogger>
and I tried looping from 1 to 'number of ancestors' and adding span or div. I tried pushing the content of the first row. nothing seems to work for me.
Any help would be greatly appreciated!
There is no such thing as a margin-left attribute. You can use a style attribute for this, though:
<xsl:attribute name="style">
<xsl:value-of select="concat('margin-left: ', $margin, 'px;')"/>
</xsl:attribute>
But using inline styles is rarely a good thing. I suggest you place your tables in a ul/li hierarchy. This way, you can put the margins in your CSS file/<style> section rather than calculating them dynamically:
<xsl:template match="/">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="operationLogger">
<li>
<table class="normalTable" cols="4">
<tr class="errorsCollectionTitle">
<td colspan="4">
<xsl:value-of select="#name"/>
</td>
</tr>
<xsl:apply-templates select="operationLogEntry"/>
</table>
<xsl:if test="operationLogger">
<ul>
<xsl:apply-templates select="operationLogger"/>
</ul>
</xsl:if>
</li>
</xsl:template>

how to make an xsl:value-of bold

I have the following XSL code:
<td class="info center">
<div class="infoBoldBlue"><xsl:value-of select="office" /></div>
<xsl:value-of select="Address/Address1" /><br />
<xsl:if test="Address/Address2 != ''">
<xsl:value-of select="Address/Address2" />
<br />
</xsl:if>
<xsl:value-of select="Address/City" />, <xsl:value-of select="Address/State" />
<xsl:text> </xsl:text>
<xsl:value-of select="Address/zip" /><br />
<xsl:value-of select="Address/phone" />
</td>
I am trying to make the <xsl:value-of select="Address/Address2" /> bold. How can I do that?
I tried adding a span and it didn't work.
Just use a strong tag around it as follows:
<td class="info center">
<div class="infoBoldBlue"><xsl:value-of select="office" /></div>
<xsl:value-of select="Address/Address1" /><br />
<xsl:if test="Address/Address2 != ''">
<strong>
<xsl:value-of select="Address/Address2" />
</strong>
<br />
</xsl:if>
<xsl:value-of select="Address/City" />, <xsl:value-of select="Address/State" />
<xsl:text> </xsl:text>
<xsl:value-of select="Address/zip" /><br />
<xsl:value-of select="Address/phone" />
</td>

ZK Grid Columns change width when button pressed

I placed ZK grid with certain proportions of columns as follows:
<grid model="#load(vm.rowList)">
<template name="model">
<row>
<cell width="7px">
<label value="#load(each.number)"/>
</cell>
<cell width="55px">
<vbox>
<button label="Up" width="100%"
onClick="#command('up',row=each)" />
<button label="Down" width="100%"
onClick="#command('down',row=each)" />
</vbox>
</cell>
<cell width="100%">
<label value="#load(each.text)"/>
</cell>
</row>
</template>
</grid>
It is displayed fine until i press any button the the width of each column becomes 1/3 of the grid.
I don't understand the reason. Suggest please how to keep the width constant.
You can try!!! Use de colums by to size every cell.
<grid model="#load(vm.rowList)">
<columns sizable="true">
<column width="20%"/>
<column width="20%"/>
<column width="20%"/>
</columns>
<template name="model">
<row>
<cell>
<label value="#load(each.number)"/>
</cell>
<cell>
<vbox>
<button label="Up" width="100%"
onClick="#command('up',row=each)" />
<button label="Down" width="100%"
onClick="#command('down',row=each)" />
</vbox>
</cell>
<cell>
<label value="#load(each.text)"/>
</cell>
</row>
</template>
</grid>

Resources