col end tag not getting called - xslt-1.0

I have the below part of my XML Document. here when i run my xslt on this XML document, the col class ... inside colgroup is getting called, colgroup is getting called but end tag of col class is not getting called. please let me know how do i close col class... the last code contains my html output of xml.
<table frame="all" width="100%">
<tgroup cols="2">
<colspec colnum="1" colname="col1" colwidth="18%"/>
<colspec colnum="2" colname="col2" colwidth="82%"/>
<thead>
<row>
<entry namest="col1" nameend="col2">
<para>A timeline of British Virgin Islands company law</para>
</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<para>1884</para>
</entry>
<entry>
<para>Companies Act passed into law.</para>
</entry>
</row>
<row>
<entry>
<para>1984</para>
</entry>
<entry>
<para>International Business Companies Act passed into law.</para>
</entry>
</row>
<row>
<entry>
<para>2004</para>
</entry>
<entry>
<para>BVI Business Companies Act passed into law, coming into force on 1 January 2005.</para>
</entry>
</row>
<row>
<entry>
<para>2005</para>
</entry>
<entry>
<para>All three corporate statutes exist in parallel and it is possible to incorporate companies under any of them.</para>
</entry>
</row>
<row>
<entry>
<para>2006</para>
</entry>
<entry>
<para>Incorporation provisions in the International Business Companies Act and the Companies Act are repealed on 31 December 2005; the Acts remain in force but new companies may only be incorporated under the BVI Business Companies Act.</para>
</entry>
</row>
<row>
<entry>
<para>2007</para>
</entry>
<entry>
<para>International Business Companies Act repealed on 31 December 2006. Transitional provisions come into effect for companies incorporated under that act.</para>
</entry>
</row>
<row>
<entry>
<para>2009</para>
</entry>
<entry>
<para>Companies Act repealed on 31 December 2008. Transitional provisions come into effect for companies incorporated under that Act.</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
and below is my xslt.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ntw="Number2Word.uri"
exclude-result-prefixes="ntw">
<xsl:variable name="ThisDocument" select="document('')"/>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8"/>
<title>
<xsl:value-of select="chapter/title"/>
</title>
<link rel="stylesheet" href="er:#css" type="text/css"/>
</head>
<body>
<xsl:apply-templates/>
<hr/>
<section class="tr_footnotes">
<xsl:apply-templates select="//footnote"
mode="footnote"/>
</section>
</body>
</html>
</xsl:template>
<xsl:template match="chapter">
<section>
<div class="chapter">
<a name="BVI-CH-{#num}"/>
<xsl:variable name="cnum">
<xsl:choose>
<xsl:when test="starts-with(#num,'0')">
<xsl:value-of select="substring-after(#num,'0')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="#num"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div class="chapter-title">
<span class="chapter-num">
<xsl:value-of select="concat('Chapter ',$cnum,' ')"/>
</span>
<xsl:apply-templates select="title"/>
</div>
<xsl:apply-templates select="child::node()[not(self::title)]"/>
</div>
</section>
</xsl:template>
<xsl:template match="chapter/para">
<div class="para align-right">
<span class="format-smallcaps">Para</span>.
</div>
</xsl:template>
<!-- Index templates -->
<xsl:template name="toc" match="chapter/toc">
<div class="toc">
<xsl:call-template name="toc-part"/>
</div>
</xsl:template>
<xsl:template name="toc-part" match="chapter/toc/toc-part">
<div class="toc-part">
<xsl:call-template name="toc-div"/>
</div>
</xsl:template>
<xsl:template name="toc-div" match="chapter/toc/toc-part/toc-div">
<table class="toc-div">
<tbody>
<xsl:for-each select="current()/toc-part/toc-div/*">
<xsl:call-template name="toc-item"/>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template name="toc-item" match="chapter/toc/toc-part/toc-div/toc-item">
<xsl:variable name="tocpg">
<xsl:value-of select="concat('#P',current()/toc-pg/text())"/>
</xsl:variable>
<xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')"/>
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/#num"/>
</xsl:variable>
<xsl:variable name="itemlevel">
<xsl:value-of select="$ThisDocument//ntw:nums[#num=$chapternumber]/#word"/>
</xsl:variable>
<xsl:variable name="tocitemlevel">
<xsl:value-of select="concat('toc-item-', $itemlevel,'-level')"/>
</xsl:variable>
<table class="{$tocitemlevel}">
<tbody>
<tr>
<td class="toc-item-num">
<xsl:value-of select="current()/#num"/>
</td>
<td class="toc-title">
<xsl:value-of select="current()/toc-title"/>
</td>
<td class="toc-pg">
<a href="{$tocpgtag}">
<xsl:value-of select="current()/toc-pg"/>
</a>
</td>
</tr>
</tbody>
</table>
</xsl:template>
<!-- Index Templates Complete -->
<!-- Paragraph templates -->
<xsl:template name="section" match="section">
<!-- Variables-->
<xsl:variable name="classname">
<!--Get name attribute of current node -->
<xsl:value-of select="concat('section-',#level)"/>
</xsl:variable>
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/#num"/>
</xsl:variable>
<xsl:variable name="sectnum">
<xsl:number level="any" count="section" format="1"/>
</xsl:variable>
<!--Create a string variable by concat string method -->
<xsl:variable name="sectionname">
<xsl:value-of select="concat('CH-',$chapternumber,'-SEC-0', $sectnum)"/>
</xsl:variable>
<!-- Template Content -->
<div class="{$classname}">
<a name="{$sectionname}"> </a>
<div class="section-title">
<span class="section-num">
<xsl:value-of select="#num"/>
</span>
<xsl:apply-templates select="title"/>
</div>
<xsl:apply-templates select="child::node()[not(self::title)]"/>
</div>
</xsl:template>
<xsl:template name="para" match="section/para">
<div class="para">
<xsl:apply-templates select="phrase"/>
<span class="phrase">
<xsl:value-of select="current()/phrase"/>
</span>
<xsl:apply-templates select="child::node()[not(self::phrase)]"/>
</div>
</xsl:template>
<xsl:template name="phrase" match="phrase">
<xsl:variable name="phrase">
<xsl:value-of select="concat('P',text())"/>
</xsl:variable>
<xsl:variable name="newphrase" select="translate($phrase,'.','-')"/>
<a>
<xsl:attribute name="name"><xsl:value-of select="$newphrase"></xsl:value-of></xsl:attribute>
</a>
</xsl:template>
<!-- Table Templates -->
<xsl:template name="table" match="table">
<table style="frame-{current()/#frame} width-{translate(current()/#width,'%','')}">
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="tgroup">
<colgroup>
<xsl:apply-templates select=".//colspec"/>
</colgroup>
<xsl:apply-templates select="child::node()[not(self::colspec)]"/>
</xsl:template>
<xsl:template name="tbody" match="tgroup/tbody">
<tbody>
<xsl:for-each select="current()/row">
<xsl:call-template name="row"/>
</xsl:for-each>
</tbody>
</xsl:template>
<xsl:template name="thead" match="tgroup/thead">
<xsl:for-each select="current()/row"><thead>
<tr>
<xsl:for-each select="current()/entry">
<xsl:call-template name="headentry"/>
</xsl:for-each>
</tr>
</thead>
</xsl:for-each>
</xsl:template>
<xsl:template name="colspec" match="colspec">
<col class="colnum-{current()/#colnum} colname-{current()/#colname} colwidth-{translate(current()/#colwidth,'%','')}"></col>
</xsl:template>
<xsl:template name="row" match="tbody/row">
<tr>
<xsl:for-each select="current()/entry">
<xsl:call-template name="entry"/>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template name="entry" match="entry">
<xsl:variable name="count">
<xsl:value-of select="count(preceding-sibling::* | following-sibling::*)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$count < 2">
<xsl:if test="position()=1">
<td>
<div class="para align-center">
<xsl:value-of select="para[position()=1]"/>
</div>
</td>
<td>
<div class="para">
<xsl:value-of select="following-sibling::node()"/>
</div>
</td>
</xsl:if>
</xsl:when>
<xsl:when test="$count > 1">
<td>
<div class="para">
<xsl:apply-templates/>
</div>
</td>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="headentry">
<th>
<xsl:if test="translate(current()/#namest,'col','') != translate(current()/#nameend,'col','')">
<xsl:variable name="colspan">
<xsl:value-of select="translate(current()/#nameend,'col','') - translate(current()/#namest,'col','') + 1"/>
</xsl:variable>
<xsl:attribute name="colspan"><xsl:value-of select="$colspan"></xsl:value-of></xsl:attribute>
</xsl:if>
<div class="para">
<xsl:value-of select="current()/para/text()"/>
</div>
</th>
</xsl:template>
<!-- Table Templates complete -->
<!--List templates -->
<xsl:template name="orderedlist" match="orderedlist">
<ol class="orderedlist">
<xsl:apply-templates/>
</ol>
</xsl:template>
<xsl:template name="orderitem" match="orderlist/item">
<li class="item">
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template name="orderitempara" match="item/para">
<xsl:variable name="itemnumber">
<xsl:value-of select="parent::item[1]/#num"/>
</xsl:variable>
<li class="item">
<div class="para">
<span class="item-num">
<xsl:value-of select="parent::item[1]/#num"/>
</span>
<xsl:apply-templates/>
</div>
</li>
</xsl:template>
<!--List templates Complete -->
<!-- Paragraph templates Complete -->
<!-- Footnote Templates-->
<xsl:template match="footnote">
<sup>
<a>
<xsl:attribute name="name"><xsl:text>footnoteref</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="href"><xsl:text>#footnote</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="class"><xsl:text>tr_ftn</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:number level="any" count="footnote" format="1"/>
</a>
</sup>
</xsl:template>
<xsl:template match="footnote" mode="footnote">
<sup>
<li style="list-style-type:none;indent:0">
<a>
<xsl:attribute name="name"><xsl:text>footnote</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="href"><xsl:text>#footnoteref</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="class"><xsl:text>tr_ftn</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:number level="any" count="footnote" format="1"/>
</a>
<xsl:text> </xsl:text>
<xsl:apply-templates/>
</li>
</sup>
</xsl:template>
<xsl:template match="footnote/para/uri">
<xsl:variable name="url1">
<xsl:value-of select="translate(#href, '<','')" />
</xsl:variable>
<xsl:variable name="url2">
<xsl:value-of select="translate($url1, '>','')" />
</xsl:variable>
<a href="{$url2}">
<xsl:value-of select="." />
</a>
</xsl:template>
<!-- Footnote Templates Complete -->
<xsl:template match="content-style">
<xsl:choose>
<xsl:when test="#format='smallcaps'">
<xsl:value-of select="translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXZ','abcdefghijklmnopqrstuvwxyz')"/>
</xsl:when>
<xsl:when test="#format='superscript'">
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Namespace ntw-->
<ntw:nums num="01" word="first"/>
<ntw:nums num="02" word="second"/>
<ntw:nums num="03" word="third"/>
<ntw:nums num="04" word="forth"/>
<ntw:nums num="05" word="fifth"/>
<ntw:nums num="06" word="sixth"/>
<ntw:nums num="07" word="seventh"/>
<ntw:nums num="08" word="eighth"/>
<ntw:nums num="09" word="nighth"/>
<ntw:nums num="10" word="tenth"/>
<!-- Namespace ntw ends -->
</xsl:stylesheet>
HTML output of xml.
table style="frame-all width-100">
<colgroup>
<col class="colnum-1 colname-col1 colwidth-18"> //here / is missing in the end
<col class="colnum-2 colname-col2 colwidth-82">//here / is missing in the end
</colgroup>
Thanks

Welcome to Stack Overflow!
Your stylesheet does not specify an output method, but your output is recognizably HTML. So your stylesheet is using the HTML output method, and not the XML output method. In the HTML output method, elements which are always empty (like col) use an SGML-style end-tag, so the output you show is correct.
If you want XML output, add an xsl:output element to the stylesheet (as the first child of xsl:stylesheet) and specify method="xml" on it, thus:
<xsl:output method="xml"/>
(Note that if you are looking to create XHTML output, you will want to put your output elements in the XHTML namespace.)
When I add an output element to the stylesheet, the colgroup element in the output takes the following form, which appears to be what you're looking for:
<colgroup>
<col class="colnum-1 colname-col1 colwidth-18"/>
<col class="colnum-2 colname-col2 colwidth-82"/>
</colgroup>

Related

How to get dynamic columns and map Row Values to Column Header with XSLT 1.0 (second Question)

i got the following XML Data and need to create 2 Tables looking like this with only xsl 1.0
Ribbon Rezept
Medikament
Dosis
08:00
12:00
18:00
22:00
ASPIRIN
0-0-0-1
0
0
0
1
Ribbon Medikarte
Medikament
Dosis
08:00
12:00
16:00
20:00
22:00
ASPIRIN
0-0-0-1
0
0
0
1
PONSTAN
1-0-0-1
1
0
0
1
But the result with the below xsl ist not creating the expected tables
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl"?>
<DATA>
<RIBBON_REZEPT>
<SRO_INT_REZEPT_INFO>
<MRO_INT_REZEPT>
<MEDIKAMENT_M CR="1">ASPIRIN</MEDIKAMENT_M>
<DOSIS_M CR="1">0-0-0-1</DOSIS_M>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>08:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>12:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>18:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>1</DOSIS_N>
<ZEIT_T>22:00</ZEIT_T>
</EINZELDOSIS>
<EINHEIT_M CR="1">Stk</EINHEIT_M>
</MRO_INT_REZEPT>
</SRO_INT_REZEPT_INFO>
</RIBBON_REZEPT>
<RIBBON_MEDIKARTE>
<SRO_INT_REZEPT_INFO>
<MRO_INT_REZEPT>
<MEDIKAMENT_M CR="1">ASPIRIN</MEDIKAMENT_M>
<DOSIS_M CR="1">0-0-0-1</DOSIS_M>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>08:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>12:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>16:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>1</DOSIS_N>
<ZEIT_T>20:00</ZEIT_T>
</EINZELDOSIS>
<EINHEIT_M CR="1">Stk</EINHEIT_M>
</MRO_INT_REZEPT>
<MRO_INT_REZEPT>
<MEDIKAMENT_M CR="1">PONSTAN</MEDIKAMENT_M>
<DOSIS_M CR="1">1-0-0-1</DOSIS_M>
<EINZELDOSIS>
<DOSIS_N>1</DOSIS_N>
<ZEIT_T>08:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>12:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>0</DOSIS_N>
<ZEIT_T>16:00</ZEIT_T>
</EINZELDOSIS>
<EINZELDOSIS>
<DOSIS_N>1</DOSIS_N>
<ZEIT_T>22:00</ZEIT_T>
</EINZELDOSIS>
<EINHEIT_M CR="1">Stk</EINHEIT_M>
</MRO_INT_REZEPT>
</SRO_INT_REZEPT_INFO>
</RIBBON_MEDIKARTE>
</DATA>
From this Post i got a Solution, but as the Times are different within the Sub-Nodes (Ribbon Rezept and Medikarte), it does not work as expected :(
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" version="1.0">
<xsl:output encoding="ISO-8859-1" indent="yes" method="html"/>
<xsl:key match="EINZELDOSIS" name="data-by-time" use="ZEIT_T"/>
<xsl:key match="EINZELDOSIS" name="data-by-cell" use="concat(ZEIT_T, '|', generate-id(..))"/>
<xsl:template match="/">
<html>
<head charset="ISO-8859-1">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Medilist v2</title>
</head>
<body>
<p>Ribbon Rezept</p>
<xsl:call-template name="RIBBON">
<xsl:with-param name="medinode" select="/DATA/RIBBON_REZEPT/SRO_INT_REZEPT_INFO"/>
</xsl:call-template>
<p>Ribbon Medikarte</p>
<xsl:call-template name="RIBBON">
<xsl:with-param name="medinode" select="/DATA/RIBBON_MEDIKARTE/SRO_INT_REZEPT_INFO"/>
</xsl:call-template>
</body>
</html>
</xsl:template>
<xsl:template name="RIBBON">
<xsl:param name="medinode" />
<xsl:variable name="distinct-times-RTF">
<xsl:for-each select="$medinode/MRO_INT_REZEPT/EINZELDOSIS[count(. | key('data-by-time', ZEIT_T)[1]) = 1]">
<xsl:sort data-type="text" order="ascending" select="ZEIT_T"/>
<xsl:copy-of select="ZEIT_T"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="distinct-times" select="ext:node-set($distinct-times-RTF)/ZEIT_T"/>
<xsl:variable name="source_document" select="/"/>
<table border="1">
<thead>
<tr>
<th>Medi</th>
<th>Dosis</th>
<!-- a column header for each distinct time point -->
<xsl:for-each select="$distinct-times">
<th>
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="$medinode/MRO_INT_REZEPT">
<xsl:variable name="row-id" select="generate-id()"/>
<tr>
<td>
<xsl:value-of select="MEDIKAMENT_M"/>
</td>
<td>
<xsl:value-of select="DOSIS_M"/>
</td>
<!-- create a cell for each distinct time point
-->
<xsl:for-each select="$distinct-times">
<xsl:variable name="zeit" select="."/>
<td>
<!-- get matching data point -->
<!-- switch the context back to the source document -->
<xsl:for-each select="$source_document">
<xsl:value-of select="key('data-by-cell', concat($zeit, '|', $row-id))/DOSIS_N"/>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
i've put it as an example here on fiddle
Your XML has two branches - RIBBON_REZEPT and RIBBON_MEDIKARTE - with identical structure, apart from the name. In order to limit the first key to the current branch, you need to add the ID or the name of the branch to its value.
Instead of:
<xsl:key match="EINZELDOSIS" name="data-by-time" use="ZEIT_T"/>
define the key as:
<xsl:key match="EINZELDOSIS" name="data-by-time" use="concat(ZEIT_T, '|', name(../../..))" />
then call it as:
key('data-by-time', concat(ZEIT_T, '|', name($branch))
where $branch is either RIBBON_REZEPT or RIBBON_MEDIKARTE.
The other key is already limited to the current MRO_INT_REZEPT, so it can stay as it is.
With some streamlining, your stylesheet could look like this:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="html"/>
<xsl:key name="data-by-time" match="EINZELDOSIS" use="concat(ZEIT_T, '|', name(../../..))" />
<xsl:key name="data-by-cell" match="EINZELDOSIS" use="concat(ZEIT_T, '|', generate-id(..))" />
<xsl:variable name="source_document" select="/" />
<xsl:template match="/DATA">
<html>
<body>
<p>Ribbon Rezept</p>
<xsl:call-template name="create-table">
<xsl:with-param name="branch" select="RIBBON_REZEPT"/>
</xsl:call-template>
<p>Ribbon Medikarte</p>
<xsl:call-template name="create-table">
<xsl:with-param name="branch" select="RIBBON_MEDIKARTE"/>
</xsl:call-template>
</body>
</html>
</xsl:template>
<xsl:template name="create-table">
<xsl:param name="branch"/>
<xsl:variable name="recepts" select="$branch/SRO_INT_REZEPT_INFO/MRO_INT_REZEPT"/>
<xsl:variable name="doses" select="$recepts/EINZELDOSIS"/>
<!-- distinct times -->
<xsl:variable name="distinct-times-RTF">
<xsl:for-each select="$doses[count(. | key('data-by-time', concat(ZEIT_T, '|', name($branch)))[1]) = 1]">
<xsl:sort select="ZEIT_T" data-type="text" order="ascending"/>
<xsl:copy-of select="ZEIT_T"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="distinct-times" select="exsl:node-set($distinct-times-RTF)/ZEIT_T" />
<!-- table -->
<table border="1">
<thead>
<tr>
<th>Medikament</th>
<th>Dosis</th>
<!-- a column header for each distinct time -->
<xsl:for-each select="$distinct-times">
<th>
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="$recepts">
<xsl:variable name="recept-id" select="generate-id()" />
<tr>
<td>
<xsl:value-of select="MEDIKAMENT_M"/>
</td>
<td>
<xsl:value-of select="DOSIS_M"/>
</td>
<!-- create a cell for each distinct time -->
<xsl:for-each select="$distinct-times">
<xsl:variable name="time" select="." />
<td>
<!-- switch the context back to the source document -->
<xsl:for-each select="$source_document">
<!-- get data from the intersection of column (time) and row (recept) -->
<xsl:value-of select="key('data-by-cell', concat($time, '|', $recept-id))/DOSIS_N" />
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>

How to remain space in XSL

I have an XML of the following process
<p>
<contribution>
<authors><author>
<surname>Zhengxing</surname> <given-name>Chen</given-name>
</author> </authors>
</contribution>
</p>
But I need below format. Major problem is in space between surname and given-name. But after converting the file, space is gone.
<p>
<span class="contribution">
<span class="authors"><span class="author">
<span class="surname">Zhengxing</span> <span class="given-name">Chen</span>
</span> </span>
</span>
</p>
Thanks for advance.
Try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:element name="p"><xsl:apply-templates/></xsl:element>
</xsl:template>
<xsl:template match="contribution|authors|author|surname|given-name">
<xsl:element name="span">
<xsl:attribute name="class"><xsl:value-of select="name()"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Looking to insert div into html body with xsl 1.0

Pretty simple: What I'm looking for a simple XSL to change
<body>
...
</body>
To
<body>
...
<div>...</div>
</body>
<xsl:template match="body">
<xsl:apply-templates />
<div>...</div>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="curTagName" select="name()"/>
<xsl:element name="{$curTagName}">
<!-- Walk through the attributes -->
<xsl:apply-templates select="#*">
<xsl:sort select="name()"/>
</xsl:apply-templates>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:variable name="curAttName" select="name()"/>
<xsl:attribute name="{$curAttName}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>

XAML XSLT attributes fail to match

When I try to transform this XAML document
<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="User" NumberSubstitution.Substitution="AsCulture" FontFamily="Arial" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="12" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0">
<Table CellSpacing="1" Margin="0,0,0,0"><Table.Columns><TableColumn Width="264" /></Table.Columns><TableRowGroup><TableRow><TableCell Padding="0,0,0,0">
<Paragraph FontFamily="Times New Roman" FontSize="10.666666666666666" Margin="0,0,0,0">
<Span FontWeight="Bold"><Run>some text</Run></Span><Run> </Run>
<Span Foreground="#FF00681C"><Run>some more text</Run></Span>
</Paragraph>
</TableCell></TableRow></TableRowGroup></Table>
</Section>
using this XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="/Table"/>
<xsl:apply-templates select="/Paragraph"/>
<xsl:apply-templates select="/Run"/>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Table">
<table>
<xsl:apply-templates select="TableRowGroup"/>
</table>
</xsl:template>
<xsl:template match="TableRowGroup">
<xsl:apply-templates select="TableRow"/>
</xsl:template>
<xsl:template match="TableRow">
<tr>
<xsl:apply-templates select="TableCell"/>
</tr>
</xsl:template>
<xsl:template match="TableCell">
<td>
</td>
</xsl:template>
<xsl:template match="Paragraph">
<p>
<xsl:apply-templates select=""/>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="Run">
<span>
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="Span">
<span>
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
i get this incorrect result
<html>
<body>
some text some more text
</body>
I saw a few problems with your XSLT that would stop it working.
There is no namespace in the XSLT. There needs to be a namespace that
matches the one in the input XML, and you need to use this namespace
when matching elements.
Your first template matches "/", which matches the root node. I
assume you actually want to match the document element Section. The
apply-templates inside this are matching elements beginning with
"/", which makes them absolute paths, so they won't match anything.
I've taken a guess at what output XML you expect. The following XSLT stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
version="1.0"
exclude-result-prefixes="ns">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" method="html"/>
<xsl:template match="/ns:Section">
<html>
<body>
<xsl:apply-templates select="ns:Table"/>
</body>
</html>
</xsl:template>
<xsl:template match="ns:Table">
<table>
<xsl:apply-templates select="ns:TableRowGroup"/>
</table>
</xsl:template>
<xsl:template match="ns:TableRowGroup">
<xsl:apply-templates select="ns:TableRow"/>
</xsl:template>
<xsl:template match="ns:TableRow">
<tr>
<xsl:apply-templates select="ns:TableCell"/>
</tr>
</xsl:template>
<xsl:template match="ns:TableCell">
<td>
<xsl:apply-templates select="ns:Paragraph"/>
</td>
</xsl:template>
<xsl:template match="ns:Paragraph">
<p>
<xsl:apply-templates select="ns:Span"/>
</p>
</xsl:template>
<xsl:template match="ns:Run">
<span>
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="ns:Span">
<span>
<xsl:apply-templates select="ns:Run"/>
</span>
</xsl:template>
</xsl:stylesheet>
produces the following output when applied to your example input XML:
<html>
<body>
<table>
<tr>
<td>
<p><span><span>some text</span></span><span><span>some more text</span></span></p>
</td>
</tr>
</table>
</body>
</html>
which may need a little formatting...

need to replace special characters

Hi I have a part of my xml document as below. In this xsm document I have a special character as “ and &#x201D. I want them to be replaced by “ and ” in my HTML output using XSL.
I am searching for a template or any other alternative method to scan my entire XML document and replace where ever the special characters are found. Right now it is being replaced by some white boxes. Please let me know how do I do it.
<para>
<phrase>2.081</phrase> In creating a vehicle like a restricted purpose company, the British Virgin Islands legislature was clearly concerned that, if such vehicles became widely used, there might be a risk that persons dealing with British Virgin Islands companies generally may have become concerned about the ability of companies to engage in day-to-day transactions. Accordingly, it sought to address these concerns by two slightly different mechanisms. Firstly, all restricted purpose companies were required to include the designation “(SPV) Limited” in their name, to make sure they were readily identifiable (even without reviewing their constitutional documents). Secondly, restricted purpose companies were made subject to licence fees significantly higher than regular companies to limit their use to appropriate cases.</para>
<table frame="all" width="100%">
<title>Comparing restricted purpose companies</title>
<tgroup cols="3">
<colspec colnum="1" colname="col1" colwidth="33%"/>
<colspec colnum="2" colname="col2" colwidth="33%"/>
<colspec colnum="3" colname="col3" colwidth="33%"/>
<thead>
I also want a method to get the number of <colspec> elements below <tgroup cols=""> (attribute cols should be there). There are also <tgroup> elements without the cols attribute.
This is my current XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ntw="Number2Word.uri" exclude-result-prefixes="ntw">
<xsl:variable name="ThisDocument" select="document('')"/>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
<xsl:value-of select="chapter/title"/>
</title>
<link rel="stylesheet" href="er:#css" type="text/css" />
</head>
<body>
<xsl:apply-templates/>
<hr />
<section class="tr_footnotes">
<xsl:apply-templates select="//footnote" mode="footnote"/>
</section>
</body>
</html>
</xsl:template>
<xsl:template match="chapter">
<section>
<div class="chapter">
<a name="BVI-CH-{#num}" />
<xsl:variable name="cnum">
<xsl:choose>
<xsl:when test="starts-with(#num,'0')">
<xsl:value-of select="substring-after(#num,'0')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="#num"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable><div class="chapter-title"><span class="chapter-num">
<xsl:value-of select="concat('Chapter ',$cnum,' ')"/>
</span>
<xsl:apply-templates/>
</div></div>
</section>
</xsl:template>
<xsl:template match="chapter/para">
<div class="para align-right">
<span class="format-smallcaps">Para</span>.
</div>
</xsl:template>
<!-- Index templates -->
<xsl:template name="toc" match="chapter/toc">
<div class="toc">
<xsl:call-template name="toc-part"></xsl:call-template>
</div>
</xsl:template>
<xsl:template name="toc-part" match="chapter/toc/toc-part">
<div class="toc-part">
<xsl:call-template name="toc-div"></xsl:call-template>
</div>
</xsl:template>
<xsl:template name="toc-div" match="chapter/toc/toc-part/toc-div">
<table class="toc-div">
<tbody>
<xsl:for-each select="current()/toc-part/toc-div/*">
<xsl:call-template name="toc-item"></xsl:call-template>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template name="toc-item" match="chapter/toc/toc-part/toc-div/toc-item">
<xsl:variable name="tocpg" >
<xsl:value-of select="concat('#P',current()/toc-pg/text())"/>
</xsl:variable>
<xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')" />
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/#num"/>
</xsl:variable>
<xsl:variable name="itemlevel">
<xsl:value-of select="$ThisDocument//ntw:nums[#num=$chapternumber]/#word"></xsl:value-of>
</xsl:variable>
<xsl:variable name="tocitemlevel">
<xsl:value-of select="concat('toc-item-', $itemlevel,'-level')"></xsl:value-of>
</xsl:variable>
<table class="{$tocitemlevel}">
<tbody>
<tr>
<td class="toc-item-num">
<xsl:value-of select="current()/#num"/>
</td>
<td class="toc-title">
<xsl:value-of select="current()/toc-title"/>
</td>
<td class="toc-pg">
<a href="{$tocpgtag}">
<xsl:value-of select="current()/toc-pg"/>
</a>
</td>
</tr>
</tbody>
</table>
</xsl:template>
<!-- Index Templates Complete -->
<!-- Paragraph templates -->
<xsl:template name="section" match="section">
<!-- Variables-->
<xsl:variable name ="classname" >
<!--Get name attribute of current node -->
<xsl:value-of select="concat('section-',#level)"/>
</xsl:variable>
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/#num"/>
</xsl:variable>
<xsl:variable name="sectnum">
<xsl:number level="any" count="section" format="1"/>
</xsl:variable>
<!--Create a string variable by concat string method -->
<xsl:variable name="sectionname">
<xsl:value-of select="concat('CH-',$chapternumber,'-SEC-0', $sectnum)"/>
</xsl:variable>
<!-- Template Content -->
<div class="{$classname}">
<a name="{$sectionname}"> </a>
<div class="section-title">
<span class="section-num">
<xsl:value-of select="#num"/>
</span>
<xsl:apply-templates/>
</div>
</div>
</xsl:template>
<!--<xsl:template name="para" match="section/para">
<xsl:variable name="count2">
<xsl:value-of select="count[child::node()]"></xsl:value-of>
</xsl:variable>
<xsl:choose>
<xsl:when test="$count2 >=1">
<div class="para">
<span class="phrase">
<xsl:value-of select="current()/phrase"/>
</span>
<xsl:apply-templates/>
</div>
</xsl:when>
<xsl:when test="$count2 < 1">
<span class="phrase">
<xsl:value-of select="current()/phrase"/>
</span>
<xsl:apply-templates mode="nr"/>
</xsl:when>
</xsl:choose>
</xsl:template> -->
<xsl:template name="para" match="section/para">
<xsl:choose>
<xsl:when test="contains()">
<div class="para">
<span class="phrase">
<xsl:value-of select="current()/phrase"/>
</span>
<xsl:apply-templates/>
</div>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="phrase" match="phrase">
<xsl:variable name="phrase">
<xsl:value-of select="concat('P',text())"/>
</xsl:variable>
<xsl:variable name="newphrase" select="translate($phrase,'.','-')"/>
<a>
<xsl:attribute name="name">
<xsl:value-of select="$newphrase"></xsl:value-of>
</xsl:attribute>
</a>
</xsl:template>
<!-- Table Templates -->
<xsl:template name ="table" match="table">
<table style="frame-{current()/#frame} width-{translate(current()/#width,'%','')}"><colgroup></colgroup>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template name="tbody" match="tgroup/tbody">
<tbody>
<xsl:for-each select="current()/row">
<xsl:call-template name="row"></xsl:call-template>
</xsl:for-each>
</tbody>
</xsl:template>
<xsl:template name="thead" match="tgroup/thead">
<xsl:value-of select="name()"></xsl:value-of>
<xsl:for-each select="current()/row">
<xsl:value-of select="name()"></xsl:value-of>
<tr>
<xsl:for-each select="current()/entry">
<xsl:call-template name="headentry"></xsl:call-template>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template name="colspec" match="colspec" >
<col class="colnum-{current()/#colnum} colname-{current()/#colname} colwidth-{translate(current()/#colwidth,'%','')}"/>
</xsl:template>
<xsl:template name="row" match="tbody/row">
<tr>
<xsl:for-each select="current()/entry">
<xsl:call-template name="entry" ></xsl:call-template>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template name="entry" match="entry">
<xsl:variable name="count">
<xsl:value-of select="count(preceding-sibling::* | following-sibling::*)"></xsl:value-of>
</xsl:variable>
<xsl:choose>
<xsl:when test="$count < 2">
<xsl:if test="position()=1">
<td>
<div class="para align-center">
<xsl:value-of select="para[position()=1]"/>
</div>
</td>
<td>
<div class="para">
<xsl:value-of select="following-sibling::node()"></xsl:value-of>
</div>
</td>
</xsl:if>
</xsl:when>
<xsl:when test="$count > 1">
<td>
<div class="para">
<!--xsl:value-of select="current()"/-->
<xsl:apply-templates/>
</div>
</td>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="headentry" >
<th>
<xsl:if test="translate(current()/#namest,'col','') != translate(current()/#nameend,'col','')">
<xsl:variable name="colspan">
<xsl:value-of select="translate(current()/#nameend,'col','') - translate(current()/#namest,'col','') + 1" />
</xsl:variable>
<xsl:attribute name="colspan">
<xsl:value-of select="$colspan"></xsl:value-of>
</xsl:attribute>
</xsl:if>
<div class="para">
<xsl:value-of select="current()/para/text()"/>
</div>
</th>
</xsl:template>
<!-- Table Templates complete -->
<!--List templates -->
<xsl:template name="orderedlist" match="orderedlist">
<ol class="orderedlist">
<xsl:apply-templates/>
</ol>
</xsl:template>
<xsl:template name="orderitem" match="orderlist/item">
<li class="item">
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template name="orderitempara" match="item/para">
<xsl:variable name="itemnumber">
<xsl:value-of select="parent::item[1]/#num"/>
</xsl:variable><li class="item">
<div class="para">
<span class="item-num">
<xsl:value-of select="parent::item[1]/#num"/>
</span>
<xsl:apply-templates></xsl:apply-templates>
</div></li>
</xsl:template>
<!--List templates Complete -->
<!-- Paragraph templates Complete -->
<!-- Footnote Templates-->
<xsl:template match="footnote"><sup>
<a>
<xsl:attribute name="name"><xsl:text>footnoteref</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="href"><xsl:text>#footnote</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="class">
<xsl:text>tr_ftn</xsl:text><xsl:number level="any" count="footnote" format="1"/>
</xsl:attribute>
<xsl:number level="any" count="footnote" format="1"/>
</a></sup>
</xsl:template>
<xsl:template match="footnote" mode="footnote"><sup>
<li style="list-style-type:none;indent:0">
<a>
<xsl:attribute name="name"><xsl:text>footnote</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="href"><xsl:text>#footnoteref</xsl:text><xsl:number level="any" count="footnote" format="1"/></xsl:attribute>
<xsl:attribute name="class">
<xsl:text>tr_ftn</xsl:text><xsl:number level="any" count="footnote" format="1"/>
</xsl:attribute>
<xsl:number level="any" count="footnote" format="1"/>
</a>
<xsl:text> </xsl:text>
<xsl:apply-templates/>
</li></sup>
</xsl:template>
<xsl:template match="footnote/para/uri">
<a>
</a>
</xsl:template>
<!-- Footnote Templates Complete -->
<xsl:template match="content-style">
<xsl:choose>
<xsl:when test="#format='smallcaps'">
<xsl:value-of select="translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXZ','abcdefghijklmnopqrstuvwxyz')"/>
</xsl:when>
<xsl:when test="#format='superscript'">
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Namespace ntw-->
<ntw:nums num="01" word="first"></ntw:nums>
<ntw:nums num="02" word="second"></ntw:nums>
<ntw:nums num="03" word="third"></ntw:nums>
<ntw:nums num="04" word="forth"></ntw:nums>
<ntw:nums num="05" word="fifth"></ntw:nums>
<ntw:nums num="06" word="sixth"></ntw:nums>
<ntw:nums num="07" word="seventh"></ntw:nums>
<ntw:nums num="08" word="eighth"></ntw:nums>
<ntw:nums num="09" word="nighth"></ntw:nums>
<ntw:nums num="10" word="tenth"></ntw:nums>
<!-- Namespace ntw ends -->
</xsl:stylesheet>
Thanks
As I am not exactly sure on which place you want to count al the <colspec> elements here an explanation of count():
You can use count(node-set) (replace node-set with your desired node-set to count) to count all the nodes from where you start it. For example:
Count all <colspec> elements in XML:
<xsl:value-of select="count(//colspec)" />
Count all <colspec> elements from the path you are now:
<xsl:value-of select="count(colspec)" />
Count all <colspec> elements from the path you are now below element <tgroup>:
<xsl:value-of select="count(tgroup/colspec)" />
Your question about the " is not clear to me. Please keep in mind that HTML and XML have some special characters which you never can display as is in source code. See next:
HTML special characters: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Character_entity_references_in_HTML
XML special characters: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML
Hope it helps you out!