XSLT:包装文本而不转换为字符串

时间:2013-10-31 13:40:38

标签: xml xslt xml-parsing xslt-2.0

有没有办法在不丢失子元素的情况下包装节点的特定部分,方法是将它们转换为字符串?

这就是我所拥有的:

<root>
    <caption>Figure 3.1 Description of an Image, sometimes with <inline>Bold</inline> or <inline>Italic</inline> emphases.</caption>
</root>

......这就是我的需要:

<root>
    <caption><inline>Figure 3.1</inline>Description of an Image, sometimes with <inline>Bold</inline> or <inline>Italic</inline> emphases.</caption>
</root>

我努力使用正则表达式^Figure\s[0-9]+.[0-9]+来捕捉不同的变体(例如图11.10)并尝试了几个小时来解决问题,但是如果不删除以下<inline>就无法做到。 ..它甚至可能吗?

我正在使用XSLT 2.0!

谢谢!

1 个答案:

答案 0 :(得分:3)

为文本节点编写模板

<xsl:template match="caption/text()">
  <xsl:analyze-string select="." regex="^Figure\s[0-9]+\.[0-9]+">
    <xsl:matching-substring>
      <inline><xsl:value-of select="."/></inline>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

当然,您可以使用身份转换模板启动样式表:

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
相关问题