XML data file
<?xml version="1.0" encoding="utf-8"?>
<page>
<tab dim="70">
<tab dim="50">
alpha
</tab>
<tab dim="50">
alpha
</tab>
</tab>
<tab dim="30">
gama
</tab>
</page>
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="xml" version="1.0"
encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<title>Title</title>
<link type="text/css" href="/css/framework.css" rel="stylesheet"/>
</head>
<body>
<div id="page-base">
<xsl:for-each select="//tab">
<div class="wrapper tab">
<xsl:attribute name="style">
width:<xsl:value-of select="@dim" />%;
min-width:<xsl:value-of select="@dim" />%;
max-width:<xsl:value-of select="@dim" />%;
</xsl:attribute>
<xsl:value-of select="." />
</div>
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Ouput
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/css/framework.css" type="text/css">
</head>
<body>
<div id="page-base">
<div class="wrapper tab" style=" width:70%; min-width:70%; max-width:70%; ">
alpha alpha
</div>
<div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
<div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
<div class="wrapper tab" style=" width:30%; min-width:30%; max-width:30%; "> gama </div>
</div>
</body>
Desired output
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/css/framework.css" type="text/css">
</head>
<body>
<div id="page-base">
<div class="wrapper tab" style=" width:70%; min-width:70%; max-width:70%; ">
<div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
<div class="wrapper tab" style=" width:50%; min-width:50%; max-width:50%; "> alpha </div>
</div>
<div class="wrapper tab" style=" width:30%; min-width:30%; max-width:30%; "> gama </div>
</div>
</body>
</html>
The key is not to use
<xsl:for-each>at all. Use template matching instead.Result: (also see http://xsltransform.net/gWmuiK6)
To get rid of the white-space in the output, all you need to do is to add another template:
There are specific cases where
<xsl:for-each>is the right thing to use. This is not one of them.Whenever you feel inclined to use
<xsl:for-each>you should stop and think. In the vast majority of cases<xsl:apply-templates>is what you really want.Use
<xsl:for-each>only if you find a compelling reason against<xsl:apply-templates>. If you can’t think of one such reason, then there probably is none.To output nodes that appear in the input without changing them, add the identity template:
To mute nodes that you do not want in the output, add an empty template: