XSLT - 添加新节点分析 text() 节点
XSLT - add new node analyzing text() node
我有一个 xml 如下,
<doc>
<chap><The root>
<element that>
<declares>
<the document to be an XSL style sheet></chap>
</doc>
我需要编写 xsl 以将名为 <p>
的单独节点添加到存在于 <chap>
和 >
之间的文本中 <chap>
。
所以输出应该是,
<doc>
<p><The root></p>
<p><element that></p>
<p><declares></p>
<p><the document to be an XSL style sheet></p>
</doc>
我可以为 <chap>
节点内的文本编写一个模板,例如 <xsl:template match="chap/text()"
但我想不出一种方法来通过分析其中的 text() 节点来添加新的 <p>
<chap>
有什么建议吗?
为此,您可以使用 analyze-string
元素来获取与正则表达式匹配的文本
<xsl:analyze-string select="." regex="<(.*)>">
并输出括号匹配的文本,使用regex-group
<xsl:value-of select="regex-group(1)" />
试试这个 XSLT 模板
<xsl:template match="chap">
<xsl:analyze-string select="." regex="<(.*)>">
<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="'>'"/>
<xsl:for-each select="tokenize(.,$separator)">
<p>
<xsl:value-of select="normalize-space(.)"/>>
</p>
</xsl:for-each>
</xsl:template>
我有一个 xml 如下,
<doc>
<chap><The root>
<element that>
<declares>
<the document to be an XSL style sheet></chap>
</doc>
我需要编写 xsl 以将名为 <p>
的单独节点添加到存在于 <chap>
和 >
之间的文本中 <chap>
。
所以输出应该是,
<doc>
<p><The root></p>
<p><element that></p>
<p><declares></p>
<p><the document to be an XSL style sheet></p>
</doc>
我可以为 <chap>
节点内的文本编写一个模板,例如 <xsl:template match="chap/text()"
但我想不出一种方法来通过分析其中的 text() 节点来添加新的 <p>
<chap>
有什么建议吗?
为此,您可以使用 analyze-string
元素来获取与正则表达式匹配的文本
<xsl:analyze-string select="." regex="<(.*)>">
并输出括号匹配的文本,使用regex-group
<xsl:value-of select="regex-group(1)" />
试试这个 XSLT 模板
<xsl:template match="chap">
<xsl:analyze-string select="." regex="<(.*)>">
<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="'>'"/>
<xsl:for-each select="tokenize(.,$separator)">
<p>
<xsl:value-of select="normalize-space(.)"/>>
</p>
</xsl:for-each>
</xsl:template>