文本在没有循环的情况下重复

时间:2013-09-02 15:23:19

标签: xslt-1.0

我有以下xml。

XML Document

以及下面的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">&lt;!DOCTYPE html&gt;</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">&lt;br/&gt;</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>

当我在我的xml上运行这个xslt时。有内容重复,虽然我没有使用任何循环。我真的很困惑,无法理解我哪里出错了。请告诉我。

您可以使用本文搜索“本法的制定是为了确保经济纠纷得到公正和及时的仲裁,以保护”

由于

1 个答案:

答案 0 :(得分:1)

因为你没有明确的部分和章节模板,所以元素的默认模板是触发,这相当于对元素子元素的apply-templates调用。

您有部分/章节/标题的显式模板,其中包含针对以下部分元素的apply-templates指令。由于这些节元素是章节的子节点,这意味着您要处理每个节的两次。结果就是您看到的重复。

您的XSLT的相关位稍微更惯用的表述可能看起来更像这样:

首先,生成'section'和'div'元素,将“输出”中的章节包装在“章节”的模板中。

<xsl:template match="part/chapter">
  <section class="tr_chapter">
    <div class="cha_app chapter">
      <xsl:apply-templates/>
    </div>
  </section>
</xsl:template>

现在,部分/章节/标题的模板不需要对其后面的部分负责,或者对于副标题的部分负责。 (我也改变了你的测试,看似更合理 - 但你可能有理由像你一样制定它。)

<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>

现在字幕处理简单,不需要在复杂的选择中跳过。

<xsl:template match="part/chapter/subtitle">
  <div class="chapter-subtitle">
    <xsl:apply-templates/>
  </div>
</xsl:template>
相关问题