Reputation: 834
i've the below xml.
and the below 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:output method="html"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>
<html>
<head>
<title>
<xsl:value-of select="concat('Appendix ', substring-before(substring-after(part/chapter/title, ' '),' '))"></xsl:value-of>
</title>
<link rel="stylesheet" href="C:\Documents and Settings\u0138039\Desktop\XML COnversion\Book 5\Book-5(original)\assets\main.css" type="text/css"/>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="part/chapter/title">
<xsl:variable name="check">
<xsl:value-of select="string-length(substring-before(substring-after(.,' '),' '))"/>
</xsl:variable>
<section class="tr_chapter">
<div class="cha_app chapter">
<xsl:choose>
<xsl:when test="$check=1">
<a name="{concat('CHA_APP_0', substring-before(substring-after(., ' '),' '))}" class="tr_toc_anchor"> </a>
</xsl:when>
<xsl:otherwise>
<a name="{concat('CHA_APP_', substring-before(substring-after(., ' '),' '))}" class="tr_toc_anchor"> </a>
</xsl:otherwise>
</xsl:choose>
<div class="chapter-title">
<xsl:value-of select="concat('Appendix ', substring-before(substring-after(., ' '),' '))"></xsl:value-of>
</div>
<div class="chapter-subtitle">
<xsl:value-of select="substring-after(substring-after(.,' '),' ')"></xsl:value-of>
</div>
<div class="chapter-subtitle">
<xsl:value-of select="following-sibling::subtitle"/>
</div>
<xsl:apply-templates select="following-sibling::section"/>
</div>
</section>
</xsl:template>
<xsl:template match="section">
<div class="section-sect1">
<a name="{concat('APP_01-SEC-',substring-before(substring-after(./title,' '), ' '))}">
</a>
<div class="section-title">
<xsl:value-of select="concat('Chapter ',substring-before(substring-after(./title,' '),' '),' ')"/>
<xsl:text disable-output-escaping="yes"><br/></xsl:text>
<xsl:value-of select="substring-after(substring-after(./title,' '),' ')"/>
</div>
<xsl:apply-templates select="child::node()[not(self::title)]"/>
</div>
</xsl:template>
<xsl:template match="para">
<div class="para">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template name="orderedlist" match="orderedlist">
<ol class="eng-orderedlist orderedlist">
<xsl:apply-templates/>
</ol>
</xsl:template>
<xsl:template name="orderitem" match="item">
<li class="item">
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template name="orderitempara" match="item/para">
<xsl:choose>
<xsl:when test="parent::item/@num">
<div class="para">
<xsl:choose>
<xsl:when test="position()=1">
<span class="item-num">
<xsl:if test="position()=1">
<xsl:value-of select="parent::item[1]/@num"/>
</xsl:if>
</span>
<xsl:text> </xsl:text>
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</div> </xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<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:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',@font-style)"/>
</xsl:variable>
<span class="{$fontStyle}">
<xsl:apply-templates/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when i'm running this xslt on my xml. there is content repeating, though i've not used any loops. i'm really confused and unable to understand where i'm going wrong. please let me know.
you can use this text to search "This Law is formulated in order to ensure that economic disputes shall be impartially and promptly arbitrated, to protect"
Thanks
Upvotes: 0
Views: 136
Reputation: 25034
Because you have no explicit templates for part and chapter, the default template for elements is firing, which amounts to a call to apply-templates on the element's children.
You do have an explicit template for part/chapter/title, which includes an apply-templates instruction for the following section elements. Since those section elements are children of chapter, this means you're handling each section twice. The result is the duplication you see.
A slightly more idiomatic formulation of the relevant bit of your XSLT might look more like this:
First, generate the 'section' and 'div' elements that wrap a chapter in the output in a template for 'chapter'.
<xsl:template match="part/chapter">
<section class="tr_chapter">
<div class="cha_app chapter">
<xsl:apply-templates/>
</div>
</section>
</xsl:template>
Now the template for part/chapter/title doesn't need to be responsible for the sections which follow it, or for that matter for the subtitle. (I've also changed your test, to something that seems more plausible -- but you may have reasons for formulating it as you did.)
<xsl:template match="part/chapter/title">
<xsl:choose>
<xsl:when test="not(normalize-space(.))">
<a name="{concat('CHA_APP_0',
substring-before(substring-after(., ' '),' '))}"
class="tr_toc_anchor"> </a>
</xsl:when>
<xsl:otherwise>
<a name="{concat('CHA_APP_',
substring-before(
substring-after(., ' '),' '))}"
class="tr_toc_anchor"> </a>
</xsl:otherwise>
</xsl:choose>
<div class="chapter-title">
<xsl:value-of select="concat('Appendix ',
substring-before(
substring-after(., ' '),' '))"/>
</div>
<div class="chapter-subtitle">
<xsl:value-of select="substring-after(
substring-after(.,' '),' ')"/>
</div>
</xsl:template>
Now subtitles are handled simply and don't need to be skipped in complicated selects.
<xsl:template match="part/chapter/subtitle">
<div class="chapter-subtitle">
<xsl:apply-templates/>
</div>
</xsl:template>
Upvotes: 1