Xml 在xslt中,如何查找文本末尾是否包含句号
我正在用BizTalk映射器VS2010编写一些xslt脚本(1.0版) 现在在输入xml文件中,我有以下标记Xml 在xslt中,如何查找文本末尾是否包含句号,xml,xslt,Xml,Xslt,我正在用BizTalk映射器VS2010编写一些xslt脚本(1.0版) 现在在输入xml文件中,我有以下标记 <STUDENTS> <STUDENT><DETAILS NAME="Tuna">These are student. details. of Student1.</DETAILS></STUDENT> <STUDENT></STUDENT> </STUDENTS&
<STUDENTS>
<STUDENT><DETAILS NAME="Tuna">These are student. details. of Student1.</DETAILS></STUDENT>
<STUDENT></STUDENT>
</STUDENTS>
详细信息NAME=“Tuna”这些是学生。细节。学生人数1/详情
现在,对于上面的每一项,输出必须如下所示
<INFO NAME="Tuna">These are student. details. of Student1</INFO>
这些是学生。细节。学生会1
我使用下面的脚本
<xsl:for-each select="//STUDENTS/STUDENT">
<INFO>
<xsl:attribute name="NAME">
<xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME="'),'"'))" />
</xsl:attribute>
<xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'>'),'</DETAILS>'))" />
<xsl:value-of select="translate($replace1,'.','')"/>
</INFO>
</xsl:for-each>
我的输出如下所示
<INFO NAME="Tuna">These are student details of "Student1" </INFO>
这些是“学生1”的学生详细信息
但我只想删除结尾出现的“.”。我该怎么做?非常感谢您的建议
提前感谢。编辑请注意,这是XSLT2.0的答案。如果它没有任何用处,我会删除它 测试
matches()
函数和正则表达式是否满足您的条件(
在字符串末尾)。你会发现一把小提琴
如果matches()
返回true,则输出不包括最后一个字符的输入文本的子字符串。换句话说,它返回一个子字符串$replace1
,从第一个字符(索引1)开始,长度string-length()-1
注意,我已经冒昧地从样式表中删除了xsl:for-each
。在许多情况下,使用模板是一种更好的方法
样式表
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/STUDENTS">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="STUDENT">
<INFO>
<xsl:attribute name="NAME">
<xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME="'),'"'))" />
</xsl:attribute>
<xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'>'),'</DETAILS>'))" />
<xsl:choose>
<xsl:when test="matches($replace1,'\.$')">
<xsl:value-of select="substring($replace1,1,string-length($replace1)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replace1"/>
</xsl:otherwise>
</xsl:choose>
</INFO>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS>
<INFO NAME="Tuna">These are student. details. of Student1</INFO>
<INFO NAME=""/>
</STUDENTS>
输出
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/STUDENTS">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="STUDENT">
<INFO>
<xsl:attribute name="NAME">
<xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME="'),'"'))" />
</xsl:attribute>
<xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'>'),'</DETAILS>'))" />
<xsl:choose>
<xsl:when test="matches($replace1,'\.$')">
<xsl:value-of select="substring($replace1,1,string-length($replace1)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replace1"/>
</xsl:otherwise>
</xsl:choose>
</INFO>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS>
<INFO NAME="Tuna">These are student. details. of Student1</INFO>
<INFO NAME=""/>
</STUDENTS>
这些是学生。细节。学生会1
我正在写一些xslt脚本(1.0版)
如果您使用的是XSLT 1.0,请尝试以下操作:
<xsl:value-of select="substring($replace1, 1, string-length($replace1) - contains(concat($replace1, '§'), '.§'))"/>
或者,优选地:
<xsl:value-of select="substring($replace1, 1, string-length($replace1) - (substring($replace1, string-length($replace1), 1) = '.'))"/>
XSLT 1.0的问题可能重复。你说得对,伊恩-很抱歉。我只是忽略了它。聪明,我喜欢它:-)@IanRoberts哦,天哪。我相信你的意思是恭维,但我发誓我会停止做“聪明”和“可爱的把戏”。显然,我旧病复发了。我将编辑我的答案,并发布一个不那么“聪明”和更直接的方法。