XSLT - 添加新节点分析 text() 节点

XSLT - add new node analyzing text() node

我有一个 xml 如下,

<doc>
  <chap>&lt;The root&gt;
     &lt;element that&gt;
     &lt;declares&gt;
     &lt;the document to be an XSL style sheet&gt;</chap>
</doc>

我需要编写 xsl 以将名为 <p> 的单独节点添加到存在于 <chap>&gt; 之间的文本中 <chap>

所以输出应该是,

<doc>
  <p>&lt;The root&gt;</p>
  <p>&lt;element that&gt;</p>
  <p>&lt;declares&gt;</p>
  <p>&lt;the document to be an XSL style sheet&gt;</p>
</doc>

我可以为 <chap> 节点内的文本编写一个模板,例如 <xsl:template match="chap/text()" 但我想不出一种方法来通过分析其中的 text() 节点来添加新的 <p> <chap>

有什么建议吗?

为此,您可以使用 analyze-string 元素来获取与正则表达式匹配的文本

<xsl:analyze-string select="." regex="&lt;(.*)&gt;">

并输出括号匹配的文本,使用regex-group

<xsl:value-of select="regex-group(1)" />

试试这个 XSLT 模板

<xsl:template match="chap">
    <xsl:analyze-string select="." regex="&lt;(.*)&gt;">
      <xsl:matching-substring>
        <p><xsl:value-of select="regex-group(1)" /></p>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="." />
      </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

阅读 http://www.xml.com/pub/a/2003/06/04/tr.html

上的正则表达式匹配

检查这个

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="chap/text()" name="tokenize">
    <xsl:param name="separator" select="'&gt;'"/>
    <xsl:for-each select="tokenize(.,$separator)">
            <p>
                <xsl:value-of select="normalize-space(.)"/>&gt;
            </p>
    </xsl:for-each>
</xsl:template>