Regex XSLT正则表达式-不后跟
我需要找到后面没有空格的Regex XSLT正则表达式-不后跟,regex,xml,xslt,xslt-2.0,Regex,Xml,Xslt,Xslt 2.0,我需要找到后面没有空格的,“文本,并在XSLT中为每个文本显式地添加空格值 例如: 输入: <chap> <para>10,20,30,40,50,60,</para> <para>10, 20, 30, 40, 50, 60</para> </chap> 10,20,30,40,50,60, 10, 20, 30, 40, 50, 60 输出: <chap> <para&
,“
文本,并在XSLT中为每个文本显式地添加空格值
例如:
输入:
<chap>
<para>10,20,30,40,50,60,</para>
<para>10, 20, 30, 40, 50, 60</para>
</chap>
10,20,30,40,50,60,
10, 20, 30, 40, 50, 60
输出:
<chap>
<para>10, 20, 30, 40, 50, 60, 60, </para>
<para>10, 20, 30, 40, 50, 60</para>
</chap>
10, 20, 30, 40, 50, 60, 60,
10, 20, 30, 40, 50, 60
XSLT
我可以使用正则表达式来完成这项任务,但是任何人都可以建议我如何找到,“
文本后面没有空格字符?您可以使用replace()
函数将后跟非空白字符的逗号(例如$x
)替换为逗号+&
+非空白字符$x
:
<xsl:template match="para">
<xsl:copy>
<xsl:value-of select="replace(.,',(\S)',', $1')"/>
</xsl:copy>
</xsl:template>
您可以使用replace()
函数将后跟非空白字符的逗号(例如$x
)替换为逗号+
+非空白字符$x
:
<xsl:template match="para">
<xsl:copy>
<xsl:value-of select="replace(.,',(\S)',', $1')"/>
</xsl:copy>
</xsl:template>
这里是一个支持exslt的xslt-1.0解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="fn str">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/node()"/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name(.)}">
<xsl:copy-of select="./@*"/>
<xsl:apply-templates select="./node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()[contains(., ',')][count(str:split(., ',')) > count(str:split(., ', '))]">
<xsl:choose>
<xsl:when test="contains(., ',')">
<xsl:for-each select="str:tokenize(., ', ')">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
,
最后一个文本模板只匹配包含“,”的文本,后面不跟空格,这是一个支持exslt的xslt-1.0解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="fn str">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/node()"/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name(.)}">
<xsl:copy-of select="./@*"/>
<xsl:apply-templates select="./node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()[contains(., ',')][count(str:split(., ',')) > count(str:split(., ', '))]">
<xsl:choose>
<xsl:when test="contains(., ',')">
<xsl:for-each select="str:tokenize(., ', ')">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
,
最后一个文本模板只匹配包含“,”的文本,后面不跟空格
string连接(标记化($in,,\s*),,,)
这假定可以将逗号后的多个空格替换为单个空格
(刚刚注意到@pouyan已经在评论中提出了这种方法)。我会的
string连接(标记化($in,,\s*),,,)
这假定可以将逗号后的多个空格替换为单个空格
(刚刚注意到@pouyan已经在一篇评论中提出了这种方法)。您可以按,“
拆分文本,然后搜索任何”,“
并向其中添加”,“
拆分文本,然后搜索任何”,“
并向其中添加”