Reputation: 11
I don't quite understand how to get an example seen below to transform into a table format seen below.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<Form name="Sample Form">
<Prop title="Name">
<PropList type="list">
<Item value="Joe"></Item>
<Item value="Anna"></Item>
<Item value="Mark"></Item></PropList></Prop>
<Prop title="ID">
<PropList type="list">
<Item value="123"></Item>
<Item value="789"></Item>
<Item value="345"></Item></PropList></Prop>
<Prop title="Code">
<PropList type="list">
<Item value="WFUO"></Item>
<Item value="SOP"></Item>
<Item value="ASAP"></Item></PropList></Prop></Form>
wanted output: sorry I'm unable to post an image
Joe 123 WFUO
Anna 789 SOP
Mark 345 ASAP
I am only able to get it to output all values to one row or one row. Please let me know if you can help.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head><title>Sample Form</title></head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>ID</th>
<th>Code</th>
</tr>
<xsl:apply-templates select='Form/Prop/PropList/Item'/>
</table>
</body></html>
</xsl:template>
<xsl:template match="Prop">
<tr>
<td><xsl:value-of select="@value"/></td>
<xsl:apply-templates select='Form/Prop/PropList/Item'/>
</tr>
</xsl:template>
<xsl:template match="Item">
<tr>
<td><xsl:value-of select="@value"/></td>
<xsl:apply-templates select='Form/Prop/PropList/Item'/>
</tr>
</xsl:template>
</xsl:stylesheet>
New updated code with some help from fellow members:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="PropKey" match="Prop" use="@title" />
<xsl:template match="Form">
<html>
<head><title>Sample Form</title></head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>ID</th>
<th>Code</th>
</tr>
<xsl:apply-templates select='Prop'/>
</table>
</body></html>
</xsl:template>
<xsl:template match="Prop">
<xsl:variable name="thisGroup" select="key('PropKey', @title)"> </xsl:variable>
<xsl:value-of select="$thisGroup"/>
<xsl:if test="generate-id() = generate-id($thisGroup[1])">
<xsl:apply-templates select="PropList"/>
</xsl:if>
</xsl:template>
<xsl:template match="PropList">
<td>
<xsl:value-of select="Item/@value"/>
</td>
</xsl:template>
Upvotes: 1
Views: 671
Reputation: 116982
The output that you show us requires pivoting the table. A simpler approach would display the table in its existing orientation:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Form">
<html>
<head><title>Sample Form</title></head>
<body>
<table border="1">
<xsl:apply-templates select='Prop'/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Prop">
<tr>
<th><xsl:value-of select="@title"/></th>
<xsl:apply-templates select='PropList/Item'/>
</tr>
</xsl:template>
<xsl:template match="Item">
<td><xsl:value-of select="@value"/></td>
</xsl:template>
</xsl:stylesheet>
Result (rendered)
As I said, if you want to change the orientation of the table, you must work harder at it:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Form">
<html>
<head><title>Sample Form</title></head>
<body>
<table border="1">
<!-- header -->
<xsl:for-each select="Prop">
<th><xsl:value-of select="@title"/></th>
</xsl:for-each>
<!-- body -->
<xsl:for-each select="Prop[1]/PropList/Item">
<xsl:variable name="i" select="position()"/>
<tr>
<xsl:for-each select="/Form/Prop/PropList">
<td><xsl:value-of select="Item[$i]/@value"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Result (rendered)
Upvotes: 1