你还没有告诉我们这导致你想,但根据您的匹配模式,它看起来就好像你现在想应用的模板上content-style
但你call-template
过程para
元素的字符串值。
因此,我已经改变的代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="para" match="para">
<xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
<div>
<xsl:choose>
<xsl:when test="./@align">
<xsl:attribute name="class">
<xsl:text>para align-</xsl:text>
<xsl:value-of select="./@align"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">
<xsl:text>para</xsl:text>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="contains(./content-style[1],'/')">
<div class="para">
<xsl:apply-templates/>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
<xsl:template match="text()">
<xsl:analyze-string select="." regex="(([Cc]hapter)\s(\d+))">
<xsl:matching-substring>
<xsl:value-of select="."/>
</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:template>
<xsl:template match="para/content-style[1]">
<xsl:analyze-string select="." regex="([0-9]+)/([0-9]+)/([0-9]+)">
<xsl:matching-substring>
<span class="phrase">
<a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/>
<xsl:value-of select="."></xsl:value-of>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="([0-9]+)/([0-9]+)">
<xsl:matching-substring>
<span class="phrase">
<a name="{concat('P',regex-group(1),'-',regex-group(2))}"/>
<xsl:value-of select="."></xsl:value-of>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."></xsl:value-of>
</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 match="item">
<!--<xsl:apply-templates select="./para[1]/node()[1][self::page]" mode="first"/>-->
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="item/para">
<li class="item">
<xsl:variable name="strl">
<xsl:value-of select="string-length(../@num)"/>
</xsl:variable>
<xsl:for-each select=".">
<xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
<div class="para">
<xsl:choose>
<xsl:when test="name(../../parent::*[1]) = 'section' and $strl > '2' and not(preceding-sibling::para)">
<xsl:apply-templates select="../@num" mode="next"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="not(preceding-sibling::para)">
<span class="item-num">
<xsl:value-of select="../@num"/>
</span>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="child::node()[not(self::para)]"/>
</div>
</xsl:for-each>
</li>
</xsl:template>
<xsl:template mode="next" match="@num">
<xsl:analyze-string select="." regex="([0-9]+)\.([0-9]+)\.([0-9]+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/>
<span class="phrase">
<xsl:value-of select="."></xsl:value-of>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="([0-9]+)\.([0-9]+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2))}"/>
<span class="phrase">
<xsl:value-of select="."></xsl:value-of>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."></xsl:value-of>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
<!-- <xsl:apply-templates select="../para"/></div>
</li>-->
</xsl:template>
<xsl:template match="page[not(preceding-sibling::node()[not(self::text()) or normalize-space()])]"/>
</xsl:transform>
这导致http://xsltransform.net/eiZQaEP/1,其输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<div class="para">
<div class="para">
<span class="phrase">
<a name="P1-4"/>1/4</span> 4.—(1) In these Rules, unless the context otherwise requires, the following expressions have the meanings hereby respectively assigned to them, namely:
<ol class="eng-orderedlist orderedlist">
<li class="item">
<div class="para">
<span class="item-num"/>“attend” includes the appearance by any person using electronic, mechanical or other means permitted by the Court;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“bailiff” includes the registrar, any clerk or other officer of the Court charged with the duties of a bailiff in the Subordinate Courts;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“Civil Procedure Convention” means the conventions set out in Appendix C to these Rules and includes any convention, treaty or agreement of any description or any provision thereof between different States relating to civil procedure in the court;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“folio” means 100 words, each figure being counted as one word;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“Form” means a form set out in Appendix A to these Rules, and a form so numbered in the Appendix;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“Judge” means a judge of the High Court or District Judge and includes, in cases where he is empowered to act, a Magistrate or the Registrar, as the case may require;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“officer” means an officer of the Supreme Court or Subordinate Courts;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“originating process” means a writ of summons or an originating summons;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“originating summons” means every summons for the commencement of proceedings other than a writ of summons;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“pleading” does not include an originating summons or preliminary act;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“receiver” includes a manager or consignee;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“Registry” means the Registry of the Supreme Court or the Registry of the Subordinate Courts, as the case may be, and references to the Registrar shall be construed accordingly;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“scheduled territories” has the meaning assigned to it by the Exchange Control Act (Chapter 99);</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“Sheriff” includes a bailiff of the Subordinate Courts;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“sign”, in relation to the signing of documents by the Registrar, includes the affixing of a facsimile signature;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“solicitor” has the same meaning as in the Legal Profession Act (Chapter 161);</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“summons” means every summons in a pending cause or matter.</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“working day” means any day other than a Saturday, Sunday or public holiday;</div>
</li>
<li class="item">
<div class="para">
<span class="item-num"/>“writ” means a writ of summons.</div>
</li>
</ol>
</div>
</div>
</root>
主要的变化是简化
<div class="para">
<xsl:call-template name="phrase"/><!--
<xsl:variable name="content" select="generate-id(content-style[1])" />-->
<xsl:apply-templates select="child::node()[not(self::content-style)]"/>
到
<div class="para">
<xsl:apply-templates/>
</div>
,然后再消除对<xsl:template name="phrase" match="para/content-style[1]" mode="phrase">
名称和模式属性得到<xsl:template name="phrase" match="para/content-style[1]">
。这样一切都落在了正确的位置。所以基本上重要的一步是尽可能地使用apply-templates和匹配模板,而不是试图使用call-template
。如果您需要或想要使用call-template
,请记住,它不会更改上下文节点,因此在匹配para
a call-template
的模板中与指定的模板继续处理该para
元素。
哇@Martin,谢谢你,这个作品很有魅力,请你详细解释一下我出错的地方以及你如何解决这个问题?再次感谢 – user2423959
我已经对我所做的更改添加了一些解释。 –