转换数字并链接它

时间:2013-05-27 06:57:21

标签: xslt xslt-1.0

我有以下xml文档。

<chapter>
    <para>
        <phrase>3.006</phrase> Parties may agree to undergo a particular ADR technique in the event of a dispute between them by entering into an ADR agreement. There are a number of ADR procedures to choose from. The procedure may involve only the parties to the dispute (such as negotiation), or may involve an independent third party. The third party may facilitate the parties&#x2019; own consensual resolution of the dispute (as in mediation/conciliation), or the third party may give an opinion on the issues in dispute (as is usually the case with a Dispute Review Board). The third party may produce a binding decision (such as adjudication) or a non-binding decision (such as expert determination). These various ADR techniques will be discussed (in paras.3.007-3.111) below. They can be compared with partnering which involves dispute avoidance (see paras.3.112 to 3.127), and the more traditional arbitration and litigation (see paras.3.128 to 3.172).
    </para>
    <para>
        <phrase>3.008</phrase> It may be stating the obvious, but negotiation can (and should) be used on its own to resolve a dispute. In fact, many dispute resolution clauses in commercial contracts require the parties to try to resolve disputes between them by negotiation prior to the initiation of any arbitration or legal proceedings. A common dispute resolution clause will use words to the effect of &#x201C;the parties shall use their best efforts in good faith to reach a reasonable and equitable resolution&#x201D; (the concept of good faith negotiation will be discussed further in para.3.020 below). Alternatively, negotiation can be used as a facilitating technique in one of the many other methods of dispute resolution. It is of particular importance in mediation or conciliation (which is discussed in para.3.027 below).
    </para>
    <para>Arbitration agreements are discussed in more detail in Chapter 9.</para>
</chapter>

这里实际上我想要一个将数字转换为超链接的xslt,如下所示。

情况1:
如果找到类似3.128的内容,则应将其转换为

<a href="er:#AHK_CH_03/P03-128">3.128</a> 

案例2:
    如果找到Chapter 9之类的东西,它应该转换为

<a href="er:#AHK_CH_18>Chapter 9</a>.

请让我知道我该怎么做。

感谢。

1 个答案:

答案 0 :(得分:1)

使用XSLT 2.0,我们可以生成给定的XSLT以获得所需的输出:

<强> XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="text()">
    <xsl:analyze-string select="." regex="(([Cc]hapter)\s(\d+))">
      <xsl:matching-substring>
        <a href="{concat('er:#AHK_CH_',regex-group(3))}">
          <xsl:value-of select="."/>
        </a>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:analyze-string select="." regex="([0-9])\.([0-9]+)">
          <xsl:matching-substring>
            <a
              href="{concat('er:#AHK_CH_',format-number(number(regex-group(1)),'00'),'/P',format-number(number(regex-group(1)),'00'),'-',regex-group(2))}">
              <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:stylesheet>

<强>输出:

    <a href="er:#AHK_CH_03/P03-006">3.006</a> Parties may agree to undergo a particular ADR technique in the event of a dispute between them by entering into an ADR agreement. There are a number of ADR procedures to choose from. The procedure may involve only the parties to the dispute (such as negotiation), or may involve an independent third party. The third party may facilitate the parties’ own consensual resolution of the dispute (as in mediation/conciliation), or the third party may give an opinion on the issues in dispute (as is usually the case with a Dispute Review Board). The third party may produce a binding decision (such as adjudication) or a non-binding decision (such as expert determination). These various ADR techniques will be discussed (in paras.<a href="er:#AHK_CH_03/P03-007">3.007</a>-<a href="er:#AHK_CH_03/P03-111">3.111</a>) below. They can be compared with partnering which involves dispute avoidance (see paras.<a href="er:#AHK_CH_03/P03-112">3.112</a> to <a href="er:#AHK_CH_03/P03-127">3.127</a>), and the more traditional arbitration and litigation (see paras.<a href="er:#AHK_CH_03/P03-128">3.128</a> to <a href="er:#AHK_CH_03/P03-172">3.172</a>).


    <a href="er:#AHK_CH_03/P03-008">3.008</a> It may be stating the obvious, but negotiation can (and should) be used on its own to resolve a dispute. In fact, many dispute resolution clauses in commercial contracts require the parties to try to resolve disputes between them by negotiation prior to the initiation of any arbitration or legal proceedings. A common dispute resolution clause will use words to the effect of “the parties shall use their best efforts in good faith to reach a reasonable and equitable resolution” (the concept of good faith negotiation will be discussed further in para.<a href="er:#AHK_CH_03/P03-020">3.020</a> below). Alternatively, negotiation can be used as a facilitating technique in one of the many other methods of dispute resolution. It is of particular importance in mediation or conciliation (which is discussed in para.<a href="er:#AHK_CH_03/P03-027">3.027</a> below).

  Arbitration agreements are discussed in more detail in <a href="er:#AHK_CH_9">Chapter 9</a>.