XSLT 2.0 has an easy function to tokenize strings, but in XSLT 1.0 you have to be more creative. The way I would usually attack something like this is with a recursive template which does something with the text before the first space and then calls itself recursively with the remaining text, stopping when it runs out.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="dataroot">
<xml><xsl:apply-templates/></xml>
</xsl:template>
<xsl:template match="M_17">
<package id="{package_id}" cat="{cat}">
<nazwa><xsl:value-of select="nazwa"/></nazwa>
<xsl:if test="author"><author><xsl:value-of select="author"/></author></xsl:if>
<xsl:if test="www"><www><xsl:value-of select="translate(www,'#','')"/></www></xsl:if>
<xsl:if test="opis"><opis><xsl:value-of select="opis"/></opis></xsl:if>
<xsl:if test="img"><img><xsl:value-of select="translate(img,'#','')"/></img></xsl:if>
<xsl:apply-templates select="depends | conflicts | after | replaces" />
</package>
</xsl:template>
<xsl:template match="depends | conflicts | after | replaces">
<xsl:param name="text" select="concat(normalize-space(), ' ')" />
<xsl:if test="$text">
<xsl:copy>
<xsl:value-of select="substring-before($text, ' ')" />
</xsl:copy>
<xsl:apply-templates select=".">
<xsl:with-param name="text" select="substring-after($text, ' ')" />
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The trick here is what we do with the text
parameter. Initially I'm setting it to concat(normalize-space(), ' ')
, which means the whole text of the target element with
- leading and trailing whitespace removed
- internal whitespace normalized to a single space character and
- one trailing space added
So $text
is initially word1-space-word2-space-...-wordN-space
Now at each step we create a new element with the same name as the original one and with the first word of $text
as its content. We then recurse, passing everything after the first space to the next step (i.e. word2-space-...-wordN-space
). Eventually we reach the point where $text
is just wordN-space
, at which point we produce an element for wordN
and then finish because substring-after($text, ' ')
is empty.
Note that
<xsl:copy>
<xsl:value-of select="substring-before($text, ' ')" />
</xsl:copy>
will copy the namespace declarations that are in scope on the input element. This is harmless but you may consider it looks a bit messy. To avoid this you could use
<xsl:element name="{local-name()}">
<xsl:value-of select="substring-before($text, ' ')" />
</xsl:element>
instead.