简体   繁体   中英

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:

<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.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

One solution is two apply substring-before and substring-after two times and call that template recurively on the rest.

<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,' '),' '),'&#xa;')" />
    <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, ' '), ' '), '&#xA;')"/>

      <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):

<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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM