Jump to content

[solved] Create Html Table Iteratively


Recommended Posts

Hello,I'm just starting with XSLT so maybe someone is able to help me with my topic :)There is an XML file with the following structure:

<TestCaseData>	<TestCase>		<TestCaseId>XXX</TestCaseId>		<Name>Name</Name>		<FolderYn>Y</FolderYn>		<CustomProperties>			<CustomProperty>				<Alias>Test Case ID</Alias>				<Name>Text01</Name>			</CustomProperty>			<CustomProperty>				<Alias>Preconditions</Alias>				<Name>Text02</Name>			</CustomProperty>			<CustomProperty>				<Alias>Type of Test</Alias>				<Name>List01</Name>			</CustomProperty>		</CustomProperties>	</TestCase>	<TestCase>		[...]	</TestCase></TestCaseData>

The most interesting element for me is "FolderYn" which can contain either "Y" or "N" as I would like to format my HTML output depending on the value of this element. The final output shall behave like this:If FolderYn is set to "Y", then format the content of <Name> as a heading (<h3>). If FolderYn is set to "N", then create a table and insert the content of <Name> for each <TestCase> as a new row until FolderYn is set to "Y" (which represents a new chapter).Or in HTML:

<h3>Name1</h3> <!-- If FolderYn=Y --><table>	<tr>		<th>Column 1</th>		<th>Column 2</th>	</tr>	<!-- Repeat for each TestCase as long as FolderYn=N and next TestCase/FolderYn=N -->	<tr>		<td>Name</td>		<td>Text01</td>	</tr></table><h3>Name2</h3> <!-- If FolderYn=Y --><h3>Name3</h3> <!-- If FolderYn=Y --><h3>Name4</h3> <!-- If FolderYn=Y --><table>	<tr>		<th>Column 1</th>		<th>Column 2</th>	</tr>	<!-- Repeat for each TestCase as long as FolderYn=N and next TestCase/FolderYn=N -->	<tr>		<td>Name</td>		<td>Text01</td>	</tr></table>

My major problem is: I tried to solve this with several if-statements but unfortunately it's not possible that e.g. the closing </table> tag is outside of the if-statement where the opening <table> has been defined.Oh my god, describing my problem is really hard. I hope anybody will understand my posting :) If you have further questions, I'll be happy answering them.Robert

Edited by MrRobse
Link to post
Share on other sites
  • 2 weeks later...

A user from another forum helped me and here is a solution that's working.

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><!-- Main template --><xsl:template match="/">	<body>		<xsl:apply-templates/>	</body>	</xsl:template>		<!-- Template for TestCase elements -->	<!-- Possible variations:				FolderYn=Y => Folder				FolderYn=N => Test case	-->	<xsl:template match="TestCase">		<xsl:choose>			<xsl:when test="FolderYn='Y'">				<xsl:apply-templates select="." mode="IsFolder"/>			</xsl:when>			<xsl:when test="FolderYn='N' and preceding-sibling::TestCase[1]/FolderYn != 'N'">				<table>					<tr>						<th>Test Case ID</th>						<th>Title</th>						<th>Priority</th>					</tr>					<xsl:apply-templates select="." mode="IsTestCase"/>				</table>			</xsl:when>		</xsl:choose>			</xsl:template>		<!-- Folder template -->	<xsl:template match="TestCase" mode="IsFolder">		<xsl:choose>			<xsl:when test="string-length(IndentLevel) div 3 = 1">				<div id="heading2"><xsl:value-of select="Name"/></div>			</xsl:when>						<xsl:when test="string-length(IndentLevel) div 3 = 2">				<div id="heading3"><xsl:value-of select="Name"/></div>			</xsl:when>			<xsl:otherwise>				<div id="heading4"><xsl:value-of select="Name"/></div>			</xsl:otherwise>		</xsl:choose>	</xsl:template>	<!-- Test case template -->	<xsl:template match="TestCase" mode="IsTestCase">		<!-- Insert test case data as table row -->		<tr>			<td><xsl:value-of select="Text01"/></td>			<td><xsl:value-of select="Name"/></td>			<td><xsl:value-of select="TestCasePriorityName"/></td>		</tr>		<!-- Check next TestCase node and call test case template -->		<xsl:if test="following-sibling::TestCase[1]/FolderYn != 'Y'">			<xsl:apply-templates select="following-sibling::TestCase[FolderYn='N'][1]" mode="IsTestCase"/>		</xsl:if>	</xsl:template></xsl:stylesheet>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...