Xml XSLT-添加新节点分析文本()节点

Xml XSLT-添加新节点分析文本()节点,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我有一个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> 我可以在节点内的文本中编写一个模板,比如,要做到这一点,您可以使用分析字

我有一个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:analyze-string select="." regex="&lt;(.*)&gt;">
试试这个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>

阅读位于

的正则表达式匹配检查此项

<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>



@sanjay,使用
,而不是
,然后获得匹配的字符串。您还可以将
regex
简化为
*
我认为regex可能需要
([^]*
以避免贪婪匹配。
<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>
<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>