function - XLST concatenating two arrays -
i'm trying concatenate 2 string arrays in special way. arrays like this:
- first array (arg1): 'a', 'b', 'c'
- second array (arg2): '-3', '', '-4'
the result should 1 string: 'a-3/b/c-4'
but code i'm getting result (only last part returned): 'c-4'
code:
<xsl:function name="functx:k" as="xs:string"> <xsl:param name="arg1" as="xs:string*"/> <xsl:param name="arg2" as="xs:string*"/> <xsl:variable name="indexedpath"/> <xsl:for-each select="$arg1"> <xsl:variable name="i" select="position()" as="xs:integer"/> <xsl:variable name="indexedpathnew" select="concat($indexedpath, $arg1[$i], $arg2[$i], '/')"/> <xsl:variable name="indexedpath" select="$indexedpathnew"/> <xsl:choose> <xsl:when test="$i=count($arg1)"> <xsl:value-of select="$indexedpathnew"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:function>
a second possibility concatenate these 2 strings:
- first string: 'a/b/c'
- second string: '-3//-4'
the result again should (one string): 'a-3/b/c-4'. think splitting , concatenating easier (i don't have code this, idea).
can please me find out doing wrong, or how properly?
i think want
<xsl:function name="functx:k" as="xs:string"> <xsl:param name="arg1" as="xs:string*"/> <xsl:param name="arg2" as="xs:string*"/> <xsl:sequence select="string-join(for $pos in 1 count($arg1) return concat($arg1[$pos], $arg2[$pos]), '/')"/> </xsl:function>
assuming xslt/xpath 3.0 (as instance available saxon 9.6) can simplify code to
<xsl:function name="functx:k" as="xs:string"> <xsl:param name="arg1" as="xs:string*"/> <xsl:param name="arg2" as="xs:string*"/> <xsl:sequence select="string-join(for-each-pair($arg1, $arg2, concat#2), '/')"/> </xsl:function>
note arguments sequences of strings, not arrays. might arrays part of language in version 3.1: http://www.w3.org/tr/xpath-31/#id-arrays.
Comments
Post a Comment