[英]split string on every 2 space XSLT 1.0
I need help with splitting string on every second space in XSLT 1.0 I am new in XSLT I have string: 我需要有关在XSLT 1.0中每隔两个空格分割字符串的帮助,我是XSLT的新手,我有字符串:
<gml:posList>50.5625 4.54111 50.56333 4.55167 50.56278 4.57694 50.55972 4.60167 50.55361 4.625 50.54528</gml:posList>
I need split string on every second space... I want output: 我需要在第二个空格上分割字符串...我想要输出:
50.5625 4.54111 50.5625 4.54111
50.56333 4.55167 50.56333 4.55167
Thank you for your help 谢谢您的帮助
Use a named recursive template: 使用命名的递归模板:
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text, $delimiter) and contains(substring-after($text, $delimiter), $delimiter)">
<token>
<xsl:value-of select="substring-before($text, $delimiter)"/>
<xsl:value-of select="$delimiter"/>
<xsl:value-of select="substring-before(substring-after($text, $delimiter), $delimiter)"/>
</token>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<token>
<xsl:value-of select="$text"/>
</token>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Demo: http://xsltransform.net/ej9EGcu 演示: http : //xsltransform.net/ej9EGcu
One solution is two apply substring-before
and substring-after
two times and call that template recurively on the rest. 一种解决方案是两次substring-before
substring-after
substring-before
和substring-after
两次应用两次,然后在其余部分上递归调用该模板。
<xsl:template name="splitEverySecond">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:value-of select="concat(substring-before($pText,' '),' ',substring-before(substring-after($pText,' '),' '),'
')" />
<xsl:call-template name="splitEverySecond">
<xsl:with-param name="pText" select="substring-after(substring-after($pText,' '),' ')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="posList">
<xsl:call-template name="splitEverySecond" select="normalize-space(.)" />
</xsl:template>
Considerably shorter and simpler : 更短更简单 :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="normalize-space($pText)">
<xsl:variable name="vText" select="concat(normalize-space($pText), ' ')"/>
<xsl:value-of select="substring-before($vText, ' ')"/>
<xsl:value-of select=
"concat(' ', substring-before(substring-after($vText, ' '), ' '), '
')"/>
<xsl:call-template name="split">
<xsl:with-param name="pText" select=
"substring-after(substring-after($vText, ' '), ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document (prefixes bound to some namespace to make it well-formed XML): 在提供的XML文档上应用此转换时 (将前缀绑定到某些名称空间以使其格式正确的XML):
<gml:posList xmlns:gml="some:gml">50.5625 4.54111 50.56333 4.55167 50.56278 4.57694 50.55972 4.60167 50.55361 4.625 50.54528</gml:posList>
the wanted, correct result is produced : 产生想要的正确结果 :
50.5625 4.54111
50.56333 4.55167
50.56278 4.57694
50.55972 4.60167
50.55361 4.625
50.54528
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.