My action in controller:
#RequestMapping(value = "/")
public String get(Model model){
String xml="<items>" +
"<item><name>James</name></item>"+
"<item><name>Luck</name></item>"+
"<item><name>Luck</name></item>"+
"</items>";
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new InputSource(new StringReader(xml)));
model.addAttribute("items",document.getDocumentElement());
return "item";
}
My xslt file:
<?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" omit-xml-declaration="yes" />
<xsl:template match="/">
<html>
<head>
<title>RSS22 Au format HTML</title>
</head>
<body>
<h1>Affichage de la liste des articles stockés</h1>
<div>
<table>
<thead>
<th>name</th>
</thead>
<tbody>
<xsl:for-each select="items/item">
<tr>
<td><xsl:value-of select="name"/> </td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
the problem is that the generated view contains plain text
Related
From the following xml data:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="kandidaten.xsl" type="text/xsl"?>
<auswertung>
<kandidat>
<name>Peter</name>
<punkte>67</punkte>
</kandidat>
<kandidat>
<name>Karl</name>
<punkte>87</punkte>
</kandidat>
<kandidat>
<name>Anita</name>
<punkte>36</punkte>
</kandidat>
<kandidat>
<name>Rosi</name>
<punkte>67</punkte>
</kandidat>
<kandidat>
<name>Heiner</name>
<punkte>50</punkte>
</kandidat>
<kandidat>
<name>Paul</name>
<punkte>45</punkte>
</kandidat>
</auswertung>
I want to create the following output with xslt:
To this end I wrote the following xslt code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="auswertung">
<html>
<head>
<title>Kandidaten</title>
</head>
<body>
<h1>Kandidaten und ihre Punkte</h1>
<hr/>
<table>
<xsl:apply-templates select="kandidat">
<xsl:sort select="punkte" order="descending"></xsl:sort>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="kandidat">
<tr>
<td>
<xsl:value-of select="name/text()"/>
</td>
<!-- second columns of table is supposed to contain the -->
<!-- points ("punkte") of the respective candidate ("kandidat") -->
<td>
<!-- second column contains a paragraph whose background-color is blue -->
<p bgcolor="#0000ff">
<xsl:call-template name="create_empty_space">
<xsl:with-param name="count" select="punkte/text()"/>
<xsl:with-param name="number" select="punkte/text()"/>
</xsl:call-template>
</p>
</td>
</tr>
</xsl:template>
<xsl:template name="create_empty_space">
<xsl:param name="count"/>
<xsl:param name="number"/>
<!-- print number (points achieved by candidate) in the middle of the beam -->
<xsl:if test="$count = round($number div 2)">
<font color="#ffff00">
<xsl:value-of select="$number"/>
</font>
</xsl:if>
<xsl:if test="$count > 0">
<!-- amount of empty space created is supposed to depend on the number of -->
<!--points, therefore function "create_empty_space" is called recursively-->
<!-- depending on the number of points -->
<!--... example: if points==20, function is called 20 times -->
<xsl:text> </xsl:text>
<xsl:call-template name="create_empty_space">
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="number" select="$number"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When I run the code, all beams have the same size, though.
How can I make the length of the beams correspond to the number of points of the respective candidate?
Since you're using xml-stylesheet in your XML input, I doubt you're really using a 2.0 processor.
Like Martin suggested, the first items width should be 100% and the width of the remaining items should be computed based on the first (max).
Here's a modified version of your XSLT...
XSLT 1.0
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<!--This is easier in XSLT 2.0:
<xsl:variable name="maxPoints" select="max(/*/kandidat/punkte)"/>
-->
<xsl:variable name="maxPoints">
<xsl:for-each select="/*/kandidat">
<xsl:sort select="punkte" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="punkte"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="auswertung">
<html>
<head>
<title>Kandidaten</title>
</head>
<body>
<h1>Kandidaten und ihre Punkte</h1>
<hr/>
<table style="width: 100%">
<xsl:apply-templates select="kandidat">
<xsl:sort select="punkte" order="descending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="kandidat">
<tr>
<td style="width: 1%">
<xsl:value-of select="name"/>
</td>
<!-- second columns of table is supposed to contain the -->
<!-- points ("punkte") of the respective candidate ("kandidat") -->
<td>
<!-- second column contains a paragraph whose background-color is blue -->
<div style="text-align: center;width: {round(punkte div $maxPoints * 100)}%;color: #ffff00;background-color: #0000ff;">
<xsl:value-of select="punkte"/>
</div>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Output
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Kandidaten</title>
</head>
<body>
<h1>Kandidaten und ihre Punkte</h1>
<hr>
<table style="width: 100%">
<tr>
<td style="width: 1%">Karl</td>
<td>
<div style="text-align: center;width: 100%;color: #ffff00;background-color: #0000ff;">87</div>
</td>
</tr>
<tr>
<td style="width: 1%">Peter</td>
<td>
<div style="text-align: center;width: 77%;color: #ffff00;background-color: #0000ff;">67</div>
</td>
</tr>
<tr>
<td style="width: 1%">Rosi</td>
<td>
<div style="text-align: center;width: 77%;color: #ffff00;background-color: #0000ff;">67</div>
</td>
</tr>
<tr>
<td style="width: 1%">Heiner</td>
<td>
<div style="text-align: center;width: 57%;color: #ffff00;background-color: #0000ff;">50</div>
</td>
</tr>
<tr>
<td style="width: 1%">Paul</td>
<td>
<div style="text-align: center;width: 52%;color: #ffff00;background-color: #0000ff;">45</div>
</td>
</tr>
<tr>
<td style="width: 1%">Anita</td>
<td>
<div style="text-align: center;width: 41%;color: #ffff00;background-color: #0000ff;">36</div>
</td>
</tr>
</table>
</body>
</html>
EDIT: You could also clean up your stylesheet and output a little by using CSS classes...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<!--This is easier in XSLT 2.0:
<xsl:variable name="maxPoints" select="max(/*/kandidat/punkte)"/>
-->
<xsl:variable name="maxPoints">
<xsl:for-each select="/*/kandidat">
<xsl:sort select="punkte" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="punkte"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="auswertung">
<html>
<head>
<title>Kandidaten</title>
<style type="text/css">
.name {
width: 10%;
}
.punkte {
text-align: center;
color: #ffff00;
background-color: #0000ff;
}
table.kandidat {
width: 100%;
}
</style>
</head>
<body>
<h1>Kandidaten und ihre Punkte</h1>
<hr/>
<table class="kandidat">
<xsl:apply-templates select="kandidat">
<xsl:sort select="punkte" order="descending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="kandidat">
<tr>
<td class="name">
<xsl:value-of select="name"/>
</td>
<td>
<div style="width: {round(punkte div $maxPoints * 100)}%;"
class="punkte">
<xsl:value-of select="punkte"/>
</div>
</td>
</tr>
</xsl:template>
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.
I have a XSLT file which displays a XML file into a HTML page. There are three tables, I want the data which is greater than -2 under the maxtemp column to have a green text-color. I tried the <xsl:when>, but it does not work. Can anybody kindly help me with this?
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="weatherdata.xsl"?>
<weatherdata>
<stationdata weatherdate="2015-1-1" location="Calgary">
<maxtemp>1.1°C</maxtemp>
<mintemp>-6.1°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-1-2" location="Calgary">
<maxtemp>-3.4°C</maxtemp>
<mintemp>-18.2°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>5cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-1-3" location="Calgary">
<maxtemp>-18.1°C</maxtemp>
<mintemp>-21.1°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>1.6cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-01" location="Charlottetown">
<maxtemp>-3.5°C</maxtemp>
<mintemp>-15°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0.4cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-02" location="Charlottetown">
<maxtemp>-1°C</maxtemp>
<mintemp>-13.2°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0.6cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-03" location="Charlottetown">
<maxtemp>-11.8°C</maxtemp>
<mintemp>-16.1°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-01" location="Ottawa">
<maxtemp>-3°C</maxtemp>
<mintemp>-8.1°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0.2cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-02" location="Ottawa">
<maxtemp>-3.8°C</maxtemp>
<mintemp>-15.8°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>0cm</totalsnow>
</stationdata>
<stationdata weatherdate="2015-01-03" location="Ottawa">
<maxtemp>-9.6°C</maxtemp>
<mintemp>-15.5°C</mintemp>
<totalrain>0 mm</totalrain>
<totalsnow>18cm</totalsnow>
</stationdata>
</weatherdata>
This is my XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="thead">
<tr>
<th>Data</th>
<th>Maximum Temperature(°C)</th>
<th>Minimum Temperature(°C)</th>
<th>Total Rain(mm)</th>
<th>Total Snow(cm)</th>
</tr>
</xsl:variable>
<xsl:template match="weatherdata">
<html align="center">
<head>
<title>weather data</title>
<style type="text/css">
table, th, td {
border: 1px solid black;
}
th{
background-color:green;
}
</style>
</head>
<body>
<ul align="left" id="top">
<li>Calgary Weather</li>
<li>Charlottetown Weather</li>
<li>Ottawa Weather</li>
</ul>
<h1 id="Calgary">Calgary – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Calgary']"/>
</table>
Back To Top
<h1 id="Charlottetown">Charlottetown – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Charlottetown']"/>
</table>
Back To Top
<h1 id="Ottawa">Ottawa – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Ottawa']"/>
</table>
Back To Top
</body>
</html>
</xsl:template>
<xsl:template match="stationdata">
<tr>
<td><xsl:value-of select="#weatherdate"/></td>
<xsl:choose>
<xsl:when test="maxtemp >'10'">
<td color="yellow">
<xsl:value-of select="maxtemp"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="maxtemp"/></td>
</xsl:otherwise>
</xsl:choose>
<td> <xsl:value-of select="mintemp"/></td>
<td><xsl:value-of select="totalrain"/></td>
<td><xsl:value-of select="totalsnow"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
I fixed your XML above because it wasn't well-formed. Concerning the problem of the comparison of maxtemp:
<xsl:when test="maxtemp >'10'">
did compare -4.2°C, a string, to -2.0, a number. Which always evaluates to false. So remove the °C first and then compare the numbers. I also added a class to the CSS part to color it green.
<xsl:when test="substring-before(maxtemp, '°C') > -2.0">
<td class="Warmer">
...
Here is the complete XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="thead">
<tr>
<th>Data</th>
<th>Maximum Temperature(°C)</th>
<th>Minimum Temperature(°C)</th>
<th>Total Rain(mm)</th>
<th>Total Snow(cm)</th>
</tr>
</xsl:variable>
<xsl:template match="weatherdata">
<html align="center">
<head>
<title>weather data</title>
<style type="text/css">
table, th, td {
border: 1px solid black;
}
th{
background-color:green;
}
.Warmer { background-color: GreenYellow; }
</style>
</head>
<body>
<ul align="left" id="top">
<li>Calgary Weather</li>
<li>Charlottetown Weather</li>
<li>Ottawa Weather</li>
</ul>
<h1 id="Calgary">Calgary – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Calgary']"/>
</table>
Back To Top
<h1 id="Charlottetown">Charlottetown – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Charlottetown']"/>
</table>
Back To Top
<h1 id="Ottawa">Ottawa – Meteorological Data (2015)</h1>
<table border="1" align="center">
<xsl:copy-of select="$thead"/>
<xsl:apply-templates select="stationdata[#location='Ottawa']"/>
</table>
Back To Top
</body>
</html>
</xsl:template>
<xsl:template match="stationdata">
<tr>
<td><xsl:value-of select="#weatherdate"/></td>
<xsl:choose>
<xsl:when test="substring-before(maxtemp, '°C') > -2.0">
<td class="Warmer">
<xsl:value-of select="maxtemp"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="maxtemp"/></td>
</xsl:otherwise>
</xsl:choose>
<td> <xsl:value-of select="mintemp"/></td>
<td><xsl:value-of select="totalrain"/></td>
<td><xsl:value-of select="totalsnow"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
I want to output the names, domain and usage of a database where its version is equal to 20i. I have created a stylesheet that does this - when I load my xml, it detects that I have an element matching my requirements in the table, but doesn't actually show them. No idea what I'm doing wrong!
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
<Inventory>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<DatabaseName>
<GlobalName>Ricardo</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20i"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<WebserverName>
<GlobalName>Jim</GlobalName>
<Function>distribution</Function>
<Domain>jim1235.com</Domain>
<Administrator EmailAlias="rkarvani" Extension="134237">Richard Karvani</Administrator>
<Administrator EmailAlias="stones" Extension="222237">Steve Jones</Administrator>
<Attributes Type="Production" Version="20i"/>
<Comments>
...
</Comments>
<Usage>
1200
</Usage>
</WebserverName>
</Inventory>
XSLT Document:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>My Server List</h1>
<h2>Database Servers</h2>
<xsl:apply-templates select="Inventory/DatabaseName" />
<h2>Webservers</h2>
<xsl:apply-templates select="Inventory/WebserverName" />
</body>
</html>
</xsl:template>
<xsl:template match="DatabaseName">
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:for-each select="Attributes[#Version='20i']">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="WebserverName">
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:for-each select="Attributes[#Version='20i']">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
I think you want to put the condition about that version into the apply-templates and move the table creation to the another template:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>My Server List</h1>
<h2>Database Servers</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:apply-templates select="Inventory/DatabaseName[Attributes[#Version='20i']]" />
</table>
<h2>Webservers</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:apply-templates select="Inventory/WebserverName[Attributes[#Version='20i']]" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="DatabaseName">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:template>
<xsl:template match="WebserverName">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Okay, it's been a tough hour or so... I'm having difficulties generating table cells with different widths. I'm using XML/XSLT to spit out my HTML, so basically the widths are stored in XML format:
<size>
<width1>5</width1>
<width2>4</width2>
<width3>7</width3>
</size>
Using XSLT's attribute-set I should have a table row and cells with 5px, 4px, 7px widths respectively. However, the trouble with this is that attribute-set needs to be a child of <xsl:stylesheet> for it to work. I CAN'T do this: (forgive the missing px)
<tr>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width1"/></xsl:attribute>
</xsl:attribute-set>
</td>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width2"/></xsl:attribute>
</xsl:attribute-set>
</td>
<td>
<xsl:attribute-set name="style">
<xsl:attribute name="width"><xsl:value-of select="size/width3"/></xsl:attribute>
</xsl:attribute-set>
</td>
</tr>
Is there any way to generate html tag using XML data to style them?
Instead of xsl:attribute-set you need to add an xsl:attribute inside your <td> element:
<xsl:template match="size">
<tr>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width1"/>
</xsl:attribute>
</td>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width2"/>
</xsl:attribute>
</td>
<td>
<xsl:attribute name="width">
<xsl:value-of select="./width3"/>
</xsl:attribute>
</td>
</tr>
</xsl:template>
More XSLT style:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="size">
<table>
<tr>
<xsl:apply-templates/>
</tr>
</table>
</xsl:template>
<xsl:template match="size/*">
<td style="width:{.}px;">
<!-- Do stuff -->
</td>
</xsl:template>
</xsl:stylesheet>
Applied to your sample result will be:
<table>
<tr>
<td style="width:5px;"></td>
<td style="width:4px;"></td>
<td style="width:7px;"></td>
</tr>
</table>