模板虽然输入一次,但调用了两次

时间:2014-02-27 08:01:01

标签: xml xslt xslt-2.0

我有以下XML文档。

<?xml version="1.0" encoding="UTF-8"?>
<toa>
<title>TABLE OF PRC LEGISLATION</title>
<subtitle>All references are to paragraph number</subtitle>
<toa-section>
<toa-div level="div1">
<title/>
<toa-entry>
<primary-entry><entry-name>Administrative Penalties Law</entry-name></primary-entry>
<secondary-entry><entry-name>Art 8</entry-name><pgs>14.026</pgs></secondary-entry>
<secondary-entry><entry-name>Art 20</entry-name><pgs>14.026</pgs></secondary-entry>
<secondary-entry><entry-name>Art 41</entry-name><pgs>14.026</pgs></secondary-entry>
<secondary-entry><entry-name>Art 44</entry-name><pgs>14.026</pgs></secondary-entry>
<secondary-entry><entry-name>Art 45</entry-name><pgs>14.026</pgs></secondary-entry>
<secondary-entry><entry-name>Art 51</entry-name><pgs>14.026</pgs></secondary-entry>
</toa-entry>
<toa-entry>
<primary-entry><entry-name>Civil Procedure Law</entry-name></primary-entry>
<secondary-entry><entry-name>Ch 19</entry-name><pgs>14.002, 14.006, 14.007, 14.012</pgs></secondary-entry>
<secondary-entry><entry-name>Art 40</entry-name><pgs>14.013</pgs></secondary-entry>
<secondary-entry><entry-name>Art 76</entry-name><pgs>14.016</pgs></secondary-entry>
<secondary-entry><entry-name>Art 92</entry-name><pgs>14.025</pgs></secondary-entry>
<secondary-entry><entry-name>Art 93</entry-name><pgs>14.025</pgs></secondary-entry>
<secondary-entry><entry-name>Art 94</entry-name><pgs>14.025</pgs></secondary-entry>
<secondary-entry><entry-name>Art 112</entry-name><pgs>14.014</pgs></secondary-entry>
<secondary-entry><entry-name>Art 199</entry-name><pgs>14.006, 14.007, 14.008</pgs></secondary-entry>
<secondary-entry><entry-name>Art 206</entry-name><pgs>14.006</pgs></secondary-entry>
</toa-entry>
</toa-div>
</toa-section>
</toa>

当我应用下面的XSL时。数据重复两次。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;&#xD;</xsl:text>
        <html>
            <head>
                <xsl:text disable-output-escaping="yes"><![CDATA[</meta>]]></xsl:text>
                <title>TABLE OF LEGISLATION</title>
                <link rel="stylesheet" href="C:\Users\u0138039\Desktop\Proview\HK\In Progress\Company Law-Insolvency 2014 Edition_xml\XSLT\main.css" type="text/css" /><xsl:text disable-output-escaping="yes"><![CDATA[</link>]]></xsl:text>
            </head>
            <body>
                <section class="tr_toa">
                    <xsl:call-template name="toa"></xsl:call-template>
                </section>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="toa">
        <div class="toa">
            <a name="CLI_TOL_02"> </a>
            <xsl:apply-templates />
        </div>
    </xsl:template>
    <xsl:template match="toa/title">
        <div class="toa-title">
            <xsl:value-of select="."/><xsl:text disable-output-escaping="yes"><![CDATA[<br />]]></xsl:text>

        </div>
        <div class="para align-center">

            <span class="font-style-bold">
                        <xsl:value-of select="following-sibling::subtitle[1]"/>
            </span>
</div>
<xsl:text disable-output-escaping="yes"><![CDATA[<br /><br />]]></xsl:text>
        <xsl:apply-templates select="//toa-section"/>
    </xsl:template>

    <xsl:template match="toa-section">
        <div class="toa-section">
            <xsl:for-each select="current()/toa-div">
                <xsl:call-template name="toa-div" />
            </xsl:for-each>
        </div>
    </xsl:template>
    <xsl:template match="toa-div" name="toa-div">
        <xsl:variable name="divClass" select="concat('toa-div level-', current()/@level)"></xsl:variable>
        <div class="{$divClass}">
            <!--<div class="toa-div-title">
                <xsl:variable name="fontStyle">
                    <xsl:value-of select="concat('font-style-',title/content-style/@font-style)"/>
                </xsl:variable>
                <span class="{$fontStyle}">
                    <xsl:value-of select="current()/title/content-style/text()"/>
                </span>
            </div>-->
            <xsl:apply-templates select="toa-entry" />
        </div>
    </xsl:template>

    <xsl:template match="toa-entry">
        <xsl:choose>
            <xsl:when test="not(preceding-sibling::toa-entry[1]/primary-entry/secondary-entry/node()) and position() != 1">

            </xsl:when>
            <xsl:otherwise>
                <table class="toa-entry">
                    <tbody>
                        <xsl:apply-templates select="primary-entry" />
                        <xsl:if test="not(current()/primary-entry/secondary-entry/node())">
                            <xsl:apply-templates select="following-sibling::toa-entry[1]" mode="next"/>
                        </xsl:if>
                    </tbody>
                </table>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="toa-entry" mode="next">
        <xsl:apply-templates  select="primary-entry"/>
        <xsl:if test="not(current()/primary-entry/secondary-entry/node())">
            <xsl:apply-templates select="following-sibling::toa-entry[1]" mode="next"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="primary-entry">
        <tr class="primary-entry">
            <td class="entry-name">
             <xsl:value-of select="current()/entry-name/text()"/>
                    </td>

                  <xsl:if test="current()/pgs/node()">
            <xsl:apply-templates select="pgs"/>
        </xsl:if>
        </tr>

        <xsl:if test="following-sibling::secondary-entry/node()">
            <xsl:for-each select="following-sibling::secondary-entry">
                <tr class="secondary-entry">
                    <td class="entry-name">
                        <xsl:value-of select="current()/entry-name/text()"/>
                    </td>

                  <xsl:if test="current()/pgs/node()">
            <xsl:apply-templates select="pgs"/>
        </xsl:if>

                </tr>
            </xsl:for-each>

        </xsl:if>

    </xsl:template>


<xsl:template match="pgs">
    <td class="pgs">
       <xsl:analyze-string select="." regex="[^,\s]+">
            <xsl:matching-substring>
                <xsl:variable name="range" select="tokenize(.,'—')"/>
                <xsl:variable name="pg" select="tokenize(.,'/')"/>
                <xsl:choose>
                    <xsl:when test="contains($pg[3],'—')">
                        <xsl:variable name="range-pg" as="item()*">
                            <xsl:for-each select="$range">
                                <xsl:sequence select="tokenize(.,'/')"/>
                            </xsl:for-each>
                        </xsl:variable>
                        <xsl:for-each select="xs:integer($range-pg[3]) to xs:integer($range-pg[6])">
                            <a href="er:#CLI_CH_{
                                if (string(number($range-pg[1]))!='NaN') then 
                                format-number(number($range-pg[1]),'00') 
                                else 
                                $range-pg[1]}/P{string-join($range-pg[position()=(1,2)],'/')}/{.}">
                                <xsl:value-of select="concat(string-join($range-pg[position()=(1,2)],'/'),'/',.)"/>
                            </a>
                            <xsl:text>, </xsl:text>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="er:#CLI_CH_{
                            if (string(number($pg[1]))!='NaN') then 
                            format-number(number($pg[1]),'00') 
                            else 
                            $pg[1]}/P{translate($pg[1],'.','-')}">
                            <xsl:value-of select="."/>
                        </a>                            
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>           
    </td>
</xsl:template>


</xsl:stylesheet>

我得到的输出如下。

<!DOCTYPE html>
<html xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><title>TABLE OF LEGISLATION</title><link rel="stylesheet" href="C:\Users\u0138039\Desktop\Proview\HK\In Progress\Company Law-Insolvency 2014 Edition_xml\XSLT\main.css" type="text/css"></link></head>
    <body>
        <section class="tr_toa">
            <div class="toa">
                <a name="CLI_TOL_02"></a>
<div class="toa-title">TABLE OF PRC LEGISLATION<br /></div><div class="para align-center">
                    <span class="font-style-bold">All references are to paragraph number</span>
                </div><br /><br /><div class="toa-section">
                    <div class="toa-div level-div1">
                        <table class="toa-entry">
                            <tbody>
                                <tr class="primary-entry">
                                    <td class="entry-name">Administrative Penalties Law</td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 8</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 20</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 41</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 44</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 45</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 51</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="primary-entry">
                                    <td class="entry-name">Civil Procedure Law</td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Ch 19</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-002">14.002</a>, <a href="er:#CLI_CH_14/P14-006">14.006</a>, <a href="er:#CLI_CH_14/P14-007">14.007</a>, <a href="er:#CLI_CH_14/P14-012">14.012</a></td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 40</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-013">14.013</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 76</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-016">14.016</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 92</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 93</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 94</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 112</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-014">14.014</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 199</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-006">14.006</a>, <a href="er:#CLI_CH_14/P14-007">14.007</a>, <a href="er:#CLI_CH_14/P14-008">14.008</a></td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 206</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-006">14.006</a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
All references are to paragraph number
<div class="toa-section">
                    <div class="toa-div level-div1">
                        <table class="toa-entry">
                            <tbody>
                                <tr class="primary-entry">
                                    <td class="entry-name">Administrative Penalties Law</td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 8</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 20</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 41</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 44</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 45</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 51</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-026">14.026</a>
                                    </td>
                                </tr>
                                <tr class="primary-entry">
                                    <td class="entry-name">Civil Procedure Law</td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Ch 19</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-002">14.002</a>, <a href="er:#CLI_CH_14/P14-006">14.006</a>, <a href="er:#CLI_CH_14/P14-007">14.007</a>, <a href="er:#CLI_CH_14/P14-012">14.012</a></td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 40</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-013">14.013</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 76</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-016">14.016</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 92</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 93</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 94</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-025">14.025</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 112</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-014">14.014</a>
                                    </td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 199</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-006">14.006</a>, <a href="er:#CLI_CH_14/P14-007">14.007</a>, <a href="er:#CLI_CH_14/P14-008">14.008</a></td>
                                </tr>
                                <tr class="secondary-entry">
                                    <td class="entry-name">Art 206</td>
                                    <td class="pgs">
                                        <a href="er:#CLI_CH_14/P14-006">14.006</a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
</div>
        </section>
    </body>
</html>

请告诉我在XSLT中出错的原因以及为什么我的输出中有重复的内容。

由于

1 个答案:

答案 0 :(得分:3)

罪魁祸首就是这个

<xsl:apply-templates select="//toa-section"/>

删除它,你将不会重复。