XSLT - 分析以下文本值
XSLT - analyse following text value
我有一个 XML text() 节点格式不正确,
示例:
<section>
<p>A number,of words have, been, suggested,as sources for,the term,</p>
</section>
这里有些 ',' 后没有 space 字符,有些有。我需要做的是,如果 ',' 后面没有 space 字符,则在 ',' 字符后添加一个 '*' 字符。
所以,预期的结果,
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term*</p>
</section>
我认为这可以使用正则表达式来完成,但是我如何 select ,XSLT 中正则表达式中不跟 space 的字符。此外, some 存在于结束元素之前(输入中的 last ),我也需要 select those 。
<xsl:template match="para">
<xsl:copy>
<xsl:analyze-string select="." regex=",\s*">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<xsl:value-of select="'*'"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
您已将输入中的最后一个 ,
替换为 ,*
,但您的声明中并没有这么说。我希望以下 XSLT 对您有所帮助:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="p/text()">
<xsl:value-of select="replace(., ',([^\s]|$)',',*')"/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term,*</p>
</section>
在这里,正则表达式 ,([^\s]|$)
匹配逗号和逗号之后的第一个字符,如果不是 space 字符; ,*
将 ,
替换为 ,*
并保持匹配组不变。
我有一个 XML text() 节点格式不正确,
示例:
<section>
<p>A number,of words have, been, suggested,as sources for,the term,</p>
</section>
这里有些 ',' 后没有 space 字符,有些有。我需要做的是,如果 ',' 后面没有 space 字符,则在 ',' 字符后添加一个 '*' 字符。
所以,预期的结果,
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term*</p>
</section>
我认为这可以使用正则表达式来完成,但是我如何 select ,XSLT 中正则表达式中不跟 space 的字符。此外, some 存在于结束元素之前(输入中的 last ),我也需要 select those 。
<xsl:template match="para">
<xsl:copy>
<xsl:analyze-string select="." regex=",\s*">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<xsl:value-of select="'*'"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
您已将输入中的最后一个 ,
替换为 ,*
,但您的声明中并没有这么说。我希望以下 XSLT 对您有所帮助:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="p/text()">
<xsl:value-of select="replace(., ',([^\s]|$)',',*')"/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<section>
<p>A number,*of words have, been, suggested,*as sources for,*the term,*</p>
</section>
在这里,正则表达式 ,([^\s]|$)
匹配逗号和逗号之后的第一个字符,如果不是 space 字符; ,*
将 ,
替换为 ,*
并保持匹配组不变。