处理指令重复两次

时间:2014-05-23 07:49:43

标签: xslt

我是以下的XML。

<section level="sect1">
<title num="1.3"><content-style font-style="bold">Common Breaches</content-style></title>
<page num="9"/>
<section level="sect2">
<title><content-style font-style="bolditalic">Non-payment of Rent/Service Charge/Hiring Charge/Licence Fee</content-style></title>
<orderedlist type="manual">
<item num="1.3.1"><para>The most common breach in a Tenancy Agreement is the late payment or non payment of rent. The Tenancy Agreement usually provides for the right of the Landlord to re-enter the premises and determine the tenancy upon non-payment of rent for a period of time. This is in addition to the Landlord&apos;s rights to sue the Tenant for any outstanding rent and any monies owing under the tenancy up to the expiry of the tenancy and forfeit the security deposit.</para></item>
<item num="1.3.2"><para>The Landlord may commence what are known as Writ of Distress proceedings or Writ of Summons proceedings to claim for the unpaid rent. The Writ of Distress is the more effective remedy, however only rent may be recovered under the Writ of Distress.</para></item>
</orderedlist>
</section>

我正在使用下面的XSLT。

<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="size">
        <xsl:value-of select="string-length(ancestor::chapter[1]/@num)"/>
    </xsl:variable>
    <xsl:variable name="Chn">
        <xsl:value-of select="ancestor::chapter[1]/@num"/>
    </xsl:variable>
    <xsl:variable name="chapternumber">
        <!-- Get num attribute of parent node -->
        <xsl:choose>
            <xsl:when test="$size=1">
                <xsl:value-of select="concat('0',$Chn)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$Chn"/>
            </xsl:otherwise>
        </xsl:choose>

    </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-', $sectnum)"/>
    </xsl:variable>
    <!-- Template Content  -->
    <xsl:if test="./page[1]">
        <xsl:apply-templates select="./page[1]"/>
    </xsl:if>
    <div class="{$classname}">
        <a name="{$sectionname}"> </a>
        <div class="section-title">
            <xsl:if test="not(contains(./@num,'unnumbered'))">
                <xsl:if test="./title/@num">
                    <span class="section-num">
                        <a name="{concat('P',translate(./title/@num,'.','-'))}"></a>
                        <xsl:value-of select="./title/@num"/>
                    </span>
                    <xsl:text> </xsl:text>
                </xsl:if>
            </xsl:if>
              <xsl:apply-templates select="./title/child::node()[fn:not(self::page)]"/>
        </div>
        <!--<xsl:apply-templates select="child::node()[not(self::title)]"/>-->
    </div>
</xsl:template>

<xsl:template name="para" match="section/para">
    <xsl:choose>
        <xsl:when test="current()/@align">
            <div class="para align-{@align}">
                <xsl:apply-templates/>
            </div>
        </xsl:when>
        <xsl:when test="current()/@num">
            <xsl:choose>
                <xsl:when test="child::node()[1][self::*]">
                    <xsl:apply-templates select="child::page[1]"/>
                    <div class="para">
                        <xsl:call-template name="phrase"/>
                        <xsl:apply-templates select="child::node()[not(self::page)]"/>
                    </div>
                </xsl:when>
                <xsl:otherwise>
                    <div class="para">
                        <xsl:call-template name="phrase"/>
                        <xsl:apply-templates/>
                    </div>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <div class="para">
                <xsl:apply-templates/>
            </div>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="text()">
    <xsl:analyze-string select="." regex="(([Cc]hapter)\s(\d+))">
        <xsl:matching-substring>
            <xsl:choose>
                <xsl:when test="number(regex-group(3)) &lt; number(9)">
                    <a href="{concat('er:#MCCL_CH_',format-number(number(regex-group(3)),'00'),'/','MCCL_CH_',format-number(number(regex-group(3)),'00'))}">
                        <xsl:value-of select="."/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:analyze-string select="." regex="[Pp]aragraphs\s([0-9]+)\.([0-9]+)\sand\s([0-9]+)\.([0-9]+)">
                <xsl:matching-substring>
                    <xsl:choose>
                        <xsl:when test="number(regex-group(1)) &lt; number(9)">
                            <a
                                href="{concat('er:#MCCL_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'0'),'-',format-number(number(regex-group(2)),'000'))}">
                                <xsl:value-of select="substring-before(., ' and')"/>
                            </a>
                            <xsl:text> and </xsl:text>
                            <a
                                href="{concat('er:#MCCL_CH_',format-number(number(regex-group(3)),'00'),'/P',format-number(number(regex-group(3)),'0'),'-',format-number(number(regex-group(4)),'000'))}">
                                <xsl:value-of select="substring-after(., 'and ')"/>
                            </a>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="."/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:analyze-string select="." regex="[Pp]aragraph\s([0-9]+)\.([0-9]+)">
                        <xsl:matching-substring>
                            <xsl:choose>
                                <xsl:when test="number(regex-group(1)) &lt; number(9)">
                                    <a
                                        href="{concat('er:#MCCL_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'0'),'-',format-number(number(regex-group(2)),'000'))}">
                                        <xsl:value-of select="."></xsl:value-of>
                                    </a>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="."></xsl:value-of>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:matching-substring>
                        <xsl:non-matching-substring>
                            <xsl:analyze-string select="."  regex="http://[^ ]+">
                                <xsl:matching-substring>
                                    <a href="{.}">
                                        <xsl:value-of select="."/>
                                    </a>
                                </xsl:matching-substring>
                                <xsl:non-matching-substring>
                                    <xsl:value-of select="."/>
                                </xsl:non-matching-substring>
                            </xsl:analyze-string>
                        </xsl:non-matching-substring>
                    </xsl:analyze-string>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</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">
    <xsl:choose>
        <xsl:when test="not(ends-with(@num, '.')) and fn:contains(@num,'.')">
            <xsl:apply-templates/>
        </xsl:when>
        <xsl:otherwise>
            <li class="item">
                <xsl:apply-templates/>
            </li>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="orderitempara" match="item/para">
    <xsl:choose>
        <xsl:when test="contains(../../@type,'manual')">
            <div class="para">
                <xsl:choose>
                    <xsl:when test="position()=1">
                        <xsl:choose>
                            <xsl:when test="contains(parent::item[1]/@num,'bull')">
                                <xsl:text>&#8226;</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:choose>
                                    <xsl:when test="not(ends-with(../@num, '.')) and fn:contains(../@num,'.')">
                                        <xsl:call-template name="phrase"/>
                                    </xsl:when>
                                    <xsl:when test="../@num">
                                        <span class="item-num">
                                            <xsl:apply-templates select="parent::item[1]/@num"/>
                                        </span>
                                    </xsl:when>
                                    <xsl:otherwise>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:otherwise>
                        </xsl:choose>
                        <xsl:text> </xsl:text>
                        <xsl:apply-templates/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates/>
                    </xsl:otherwise>
                </xsl:choose>
            </div>
        </xsl:when>
        <xsl:otherwise>
            <span class="bullet-list">
                <xsl:value-of select="../../@type"/>
            </span>
            <xsl:apply-templates/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="page">
    <xsl:processing-instruction name="pb">
        <xsl:text>label='</xsl:text>
        <xsl:value-of select="./@num"/>
        <xsl:text>'</xsl:text>
        <xsl:text>?</xsl:text>
    </xsl:processing-instruction>
  <a name="{concat('pg_',./@num)}"/>
    <xsl:apply-templates/>
</xsl:template>


<xsl:template name="phrase">
    <xsl:choose>
        <xsl:when test="parent::title">
            <xsl:variable name="phrase">
                <xsl:value-of select="concat('P',@num)"/>
            </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>
            <span class="phrase1">
                <xsl:value-of select="current()"/>
            </span>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="phrase">
                <xsl:value-of select="concat('P',../@num)"/>
            </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>
            <span class="phrase">
                <xsl:value-of select="../@num"/>
            </span>
        </xsl:otherwise>
    </xsl:choose>
    <!--<xsl:apply-templates/>-->


</xsl:template>

当我运行这个时,我得到如下输出。

enter image description here  这里处理指令编号9(pb label='9')正在重复,请告诉我如何获得以下输出。

enter image description here

由于

1 个答案:

答案 0 :(得分:0)

让您的page模板处于其他模式

<xsl:apply-templates select="page[1]" mode="insert_page"/>

<xsl:template match="page" mode="insert_page">