XSLT - Select 两个特殊字符之间的内容
XSLT - Select content between two special characters
我有一个这样的xml,
<doc>
<p>text1 <xml version="1.0" encoding="UTF-16"
standalone="yes"?> text2</p>
</doc>
我需要使用 XSLT 删除 < and >
表单上方文本之间的文本内容。所以预期的输出是,
<doc>
<p>text1 text2</p>
</doc>
我尝试使用正则表达式,但我想知道如何在 < and >
形式的正则表达式之间捕获文本。
知道如何使用 XSLT 执行此操作吗?
这应该有效。
(<(?:.?\n?)*>)
然后替换为“”(空)
输入:
<doc>
<p>text1 <xml version="1.0" encoding="UTF-16"
standalone="yes"?> text2</p>
</doc>
输出:
<doc>
<p>text1 text2</p>
</doc>
仅使用 XSLT-1.0,您可以通过应用以下模板实现此目的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="p">
<xsl:value-of select="concat(normalize-space(substring-before(text(), '<')),' ',normalize-space(substring-after(text(), '>')))" />
</xsl:template>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
此模板仅复制具有 标识模板 的所有节点,并对所有 <p>
元素应用特殊处理。
<p>
节点的特殊处理提取<
之前和>
之后的text()
节点,同时归一化space
字符出现(减少它们数到一)并连接结果。
就这些了。
我有一个这样的xml,
<doc>
<p>text1 <xml version="1.0" encoding="UTF-16"
standalone="yes"?> text2</p>
</doc>
我需要使用 XSLT 删除 < and >
表单上方文本之间的文本内容。所以预期的输出是,
<doc>
<p>text1 text2</p>
</doc>
我尝试使用正则表达式,但我想知道如何在 < and >
形式的正则表达式之间捕获文本。
知道如何使用 XSLT 执行此操作吗?
这应该有效。
(<(?:.?\n?)*>)
然后替换为“”(空)
输入:
<doc>
<p>text1 <xml version="1.0" encoding="UTF-16"
standalone="yes"?> text2</p>
</doc>
输出:
<doc>
<p>text1 text2</p>
</doc>
仅使用 XSLT-1.0,您可以通过应用以下模板实现此目的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="p">
<xsl:value-of select="concat(normalize-space(substring-before(text(), '<')),' ',normalize-space(substring-after(text(), '>')))" />
</xsl:template>
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
此模板仅复制具有 标识模板 的所有节点,并对所有 <p>
元素应用特殊处理。
<p>
节点的特殊处理提取<
之前和>
之后的text()
节点,同时归一化space
字符出现(减少它们数到一)并连接结果。
就这些了。