在xslt 1.0中使用正则表达式

时间:2013-08-08 08:05:46

标签: xslt-1.0

我想在xslt 1.0中使用正则表达式

输入

<book>
    <p>
        The clavicle is broken more <inlinegraphic></inlinegraphic>
        mad_2235.eps often than any other bone in the body
    </p>
</book>

输出

<book>
    <p>
        The clavicle is broken more
        <graphic name="mad_2235.eps" source="ISBN" in-line="yes"/>
        often than any other bone in the body
    </p>
</book>

谢谢, MUTHU

1 个答案:

答案 0 :(得分:0)

XSLT 1.0不执行正则表达式,但在这种情况下我认为您不需要它们。我会从身份转换开始

<!-- copy everything from input to output verbatim, except where overridden
     by more specific templates -->
<xsl:template match="@*|node()">
  <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>

然后使用模板覆盖此模板,该模板匹配紧跟inlinegraphic元素且包含.eps

的任何文本节点
<xsl:template match="text()[preceding-sibling::node()[1][self::inlinegraphic]]
                           [contains(., '.eps')]">
  <!-- take everything before the first .eps as the graphic name -->
  <graphic name="{normalize-space(substring-before(., '.eps'))}.eps"
           source="ISBN" in-line="yes"/>
  <!-- and leave everything after that as normal text -->
  <xsl:value-of select="substring-after(., '.eps')" />
</xsl:template>

最后添加一个模板以删除inlinegraphic元素本身

<xsl:template match="inlinegraphic" />