如何限制XSLT1.0中的字符串字数?
在XSLT 1.0中,如何限制字符串的字数?类似于:如何限制XSLT1.0中的字符串字数?,xslt,string,Xslt,String,在XSLT 1.0中,如何限制字符串的字数?类似于: <xsl:template match="data"> <!-- your data element or whatever --> <xsl:call-template name="firstWords"> <xsl:with-param name="value" select="."/> <xsl:with-param name="count" se
<xsl:template match="data"> <!-- your data element or whatever -->
<xsl:call-template name="firstWords">
<xsl:with-param name="value" select="."/>
<xsl:with-param name="count" select="4"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="firstWords">
<xsl:param name="value"/>
<xsl:param name="count"/>
<xsl:if test="number($count) >= 1">
<xsl:value-of select="concat(substring-before($value,' '),' ')"/>
</xsl:if>
<xsl:if test="number($count) > 1">
<xsl:variable name="remaining" select="substring-after($value,' ')"/>
<xsl:if test="string-length($remaining) > 0">
<xsl:call-template name="firstWords">
<xsl:with-param name="value" select="$remaining"/>
<xsl:with-param name="count" select="number($count)-1"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
这是一个XSLT 1.0解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', 	 ()-'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="strTakeWords">
<xsl:with-param name="pN" select="10"/>
<xsl:with-param name="pText" select="/*"/>
<xsl:with-param name="pWords"
select="ext:node-set($vwordNodes)/*"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="word" priority="10">
<xsl:value-of select="concat(position(), ' ', ., ' ')"/>
</xsl:template>
<xsl:template name="strTakeWords">
<xsl:param name="pN" select="10"/>
<xsl:param name="pText"/>
<xsl:param name="pWords"/>
<xsl:param name="pResult"/>
<xsl:choose>
<xsl:when test="not($pN > 0)">
<xsl:value-of select="$pResult"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vWord" select="$pWords[1]"/>
<xsl:variable name="vprecDelims" select=
"substring-before($pText,$pWords[1])"/>
<xsl:variable name="vnewText" select=
"concat($vprecDelims, $vWord)"/>
<xsl:call-template name="strTakeWords">
<xsl:with-param name="pN" select="$pN -1"/>
<xsl:with-param name="pText" select=
"substring-after($pText, $vnewText)"/>
<xsl:with-param name="pWords" select=
"$pWords[position() > 1]"/>
<xsl:with-param name="pResult" select=
"concat($pResult, $vnewText)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下XML文档时:
<t>
(CNN) -- Behind closed doors in recent days,
senior White House aides have been saying that
measuring President Obama's first 100 days
is the journalistic equivalent of a Hallmark holiday.
</t>
(美国有线电视新闻网)--近几天来,闭门造车,
白宫高级助手一直在说
衡量奥巴马总统的头100天
这在新闻界相当于一个标志性的节日。
返回想要的结果:
<t>
(CNN) -- Behind closed doors in recent days,
senior White House aides have been saying that
measuring President Obama's first 100 days
is the journalistic equivalent of a Hallmark holiday.
</t>
(CNN)--最近几天关起门来,
白宫高层
注意事项:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', 	 ()-'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="strTakeWords">
<xsl:with-param name="pN" select="10"/>
<xsl:with-param name="pText" select="/*"/>
<xsl:with-param name="pWords"
select="ext:node-set($vwordNodes)/*"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="word" priority="10">
<xsl:value-of select="concat(position(), ' ', ., ' ')"/>
</xsl:template>
<xsl:template name="strTakeWords">
<xsl:param name="pN" select="10"/>
<xsl:param name="pText"/>
<xsl:param name="pWords"/>
<xsl:param name="pResult"/>
<xsl:choose>
<xsl:when test="not($pN > 0)">
<xsl:value-of select="$pResult"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vWord" select="$pWords[1]"/>
<xsl:variable name="vprecDelims" select=
"substring-before($pText,$pWords[1])"/>
<xsl:variable name="vnewText" select=
"concat($vprecDelims, $vWord)"/>
<xsl:call-template name="strTakeWords">
<xsl:with-param name="pN" select="$pN -1"/>
<xsl:with-param name="pText" select=
"substring-after($pText, $vnewText)"/>
<xsl:with-param name="pWords" select=
"$pWords[position() > 1]"/>
<xsl:with-param name="pResult" select=
"concat($pResult, $vnewText)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
pDelimiters
,该参数是一个字符串,由应被视为分隔符的所有字符组成。因此,与其他解决方案相比,可以指定每个分隔符(而不仅仅是“空格”)——在本例中为8个strTakeWords
递归调用自身,以累积之前的文本,并包括由标记化生成的单词列表中的每个单词,直到处理了指定数量的单词你能澄清一下吗?例如,您想输出第一个“n”字吗?我发布了一个解决方案,它可以正确使用数量可变的(指定的)字分隔符,而Mark Gravell的解决方案只将空格视为字分隔符。您好,非常感谢您的回复。我只是想澄清一下,我不能不幸地链接到扩展(我们在一个内部网络上)。没有XSLT1.0中的扩展,有没有办法做到这一点?顺致敬意,Will@Will我的解决方案不需要任何扩展函数,除了EXSLt node-set(),它由大多数XSLT 1.0处理器(如.NET XslCompiledTransform类)在内部实现。任何FXSL 1.x模板都是用纯XSLT编写的,唯一使用的扩展函数是前面提到的EXSLT node-set()函数。因此,在内部网络中使用此解决方案绝对没有障碍——只需使用任何XSLT 1.0处理器,它实现了EXSLT node-set()函数(如.NET XslCompiledTransform、Saxon 6、Xalan、JD等)。非常感谢您的回复。我只是想澄清一下,我不能不幸地链接到扩展(我们在一个内部网络上)。有没有不使用扩展就可以做到这一点的方法?顺致敬意,Will@Will我的解决方案不需要任何扩展函数,除了EXSLt node-set(),它由大多数XSLT 1.0处理器(如.NET XslCompiledTransform类)在内部实现。任何FXSL 1.x模板都是用纯XSLT编写的,唯一使用的扩展函数是前面提到的EXSLT node-set()函数。因此,在内部网络中使用此解决方案绝对没有障碍——只需使用任何XSLT 1.0处理器,它实现了EXSLT node-set()函数(如.NET XslCompiledTransform、Saxon 6、Xalan、JD等)