如何匹配案例并应用模板

时间:2015-02-11 12:22:52

标签: xpath xslt-2.0

我有以下XML代码行。

<title><page>651</page>CHAPTER 13 This is <content-style font-style="italic">This goes in content-style</content-style> The title</title>

我在这里尝试以下操作。

获取CHAPTER之后的数字并将其与Chapter联系起来,我可以使用以下代码进行操作。

<xsl:value-of select="concat('Chapter ', substring-before(substring-after(child::title,' '),' ')"/>

忽略page中的title,我使用以下模板匹配并且能够执行此操作。

<xsl:template match="title/page"/>

如果只有普通数据,我正在使用以下内容获取它。

<xsl:value of select ="substring-after(substring-after(./title,' '),' ')">

但问题出现在上面的类型中,这里我需要在substring-after(substring-after(.,' '),' ')上应用模板,不幸的是这不起作用。

我有以下XSLT

<xsl:template match ="title">
<xsl:apply-templates/>
</xsl:template>
    <xsl:template match="content-style">
        <xsl:variable name="fontStyle">
            <xsl:value-of select="concat('font-style-',@font-style)"/>
        </xsl:variable>
        <span class="{$fontStyle}">
            <xsl:value-of select="."/>
            <xsl:apply-templates select="para"/>
        </span>
    </xsl:template>

预期O / P

<div class="chapter-title">
                    <span class="chapter-num">Chapter 13</span><br /><br /> This is <span class="font-style-italic">This goes in content-style</span> the title</span></div>

请告诉我我该怎么做。

由于

1 个答案:

答案 0 :(得分:0)

您的预期输出具有无法匹配的跨度结束标记。

忽略这一点,我会做类似的事情:

<xsl:template match ="title">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="content-style">
        <span class="font-style-{@font-style}">
            <xsl:value-of select="."/>
        </span>
</xsl:template>

<xsl:template match="title/text()">
  <xsl:analyze-string select="." regex="Chapter [0-9]+">
    <xsl:matching-substring>
      <span class="chapter-num">
       <xsl:value-of select="."/>
      </span><br/>>br/>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
       <xsl:value-of select="."/>
    </xsl:-nonmatching-substring>
  </xsl:analyze-string>
</xsl:template>