Xslt 获取前一个同级并应用模板

Xslt 获取前一个同级并应用模板,xslt,Xslt,我有下面的XML代码 <root> <title num="1/2"><content-style font-style="bold">Application</content-style> (O 1 r 2)</title> <para><content-style font-style="bold">2.</content-style>—(1) Subject to paragraph (2),

我有下面的XML代码

<root>
<title num="1/2"><content-style font-style="bold">Application</content-style> (O 1 r 2)</title>
<para><content-style font-style="bold">2.</content-style>—(1) Subject to paragraph (2), these Rules apply to all proceedings in-</para>
<para>(2) These Rules do not have effect in relation to proceedings in respect of which rules have been or may be made under any written law for the specific purpose of such proceedings or in relation to any criminal proceedings.</para>
<para>(3) Where prior to the coming into operation of these Rules, reference is made in any written law to any Rules of Court that reference shall be to these Rules.</para>
</root>

应用程序(O 1 r 2)
2.-(1)根据第(2)款的规定,本规则适用于-
(2) 本规则不适用于已根据或可能根据任何成文法为该等法律程序的特定目的而制定规则的法律程序,也不适用于任何刑事法律程序。
(3) 凡在本规则实施前,任何成文法已提述任何法院规则,而该提述即为本规则。
下面是XSLT

<xsl:template name="para" match="para">
  <xsl:choose>
    <xsl:when test="./@align">
      <div class="para align-{@align}">
        <xsl:apply-templates/>
      </div>
    </xsl:when>
    <xsl:otherwise>

      <xsl:choose>
        <xsl:when test="preceding-sibling::title[1]/@num[1]">
          <xsl:apply-templates/>
        </xsl:when>
        <xsl:otherwise>
          <div class="para">
            <xsl:apply-templates/>
          </div>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>

</xsl:template>

在这里,当我试图运行此
时,它没有按预期工作。对于第一个
段落
应直接应用模板,对于第二个和第三个段落,首先应存在一个
,并且在其内部应应用模板(否则条件),但在我的情况下,对于所有段落
,都得到了满足,并且直接应用了模板。请告诉我哪里出了问题

谢谢

尝试更换

<xsl:when test="preceding-sibling::title[1]/@num[1]">


尝试更换

<xsl:when test="preceding-sibling::title[1]/@num[1]">


前面的同级::*轴匹配所有前面的同级,而不仅仅是直接同级,因此您必须首先选择直接同级,然后检查它是否是
标题
元素:

preceding-sibling::*[1]/self::title
我建议使用更XSLT的方法,用更具体的模板替换嵌套的

<xsl:template match="para[@align]" priority="2">
  <div class="para align-{@align}">
    <xsl:apply-templates/>
  </div>
</xsl:template>

<xsl:template match="para[preceding-sibling::*[1]/self::title/@num]" priority="1">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="para">
  <div class="para">
    <xsl:apply-templates/>
  </div>
</xsl:template>

如果您坚持使用
,请仅使用一种:

<xsl:template match="para">
  <xsl:choose>
    <xsl:when test="./@align">
      <div class="para align-{@align}">
        <xsl:apply-templates/>
      </div>
    </xsl:when>
    <xsl:when test="preceding-sibling::*[1]/self::title/@num">
      <xsl:apply-templates/>
    </xsl:when>
    <xsl:otherwise>
      <div class="para">
        <xsl:apply-templates/>
      </div>
    </xsl:otherwise>
  </xsl:choose>    
</xsl:template>

前面的同级::*轴匹配所有前面的同级,而不仅仅是直接同级,因此您必须首先选择直接同级,然后检查它是否是
标题
元素:

preceding-sibling::*[1]/self::title
我建议使用更XSLT的方法,用更具体的模板替换嵌套的

<xsl:template match="para[@align]" priority="2">
  <div class="para align-{@align}">
    <xsl:apply-templates/>
  </div>
</xsl:template>

<xsl:template match="para[preceding-sibling::*[1]/self::title/@num]" priority="1">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="para">
  <div class="para">
    <xsl:apply-templates/>
  </div>
</xsl:template>

如果您坚持使用
,请仅使用一种:

<xsl:template match="para">
  <xsl:choose>
    <xsl:when test="./@align">
      <div class="para align-{@align}">
        <xsl:apply-templates/>
      </div>
    </xsl:when>
    <xsl:when test="preceding-sibling::*[1]/self::title/@num">
      <xsl:apply-templates/>
    </xsl:when>
    <xsl:otherwise>
      <div class="para">
        <xsl:apply-templates/>
      </div>
    </xsl:otherwise>
  </xsl:choose>    
</xsl:template>