xmlxslt-2.0map-force

Altova Mapforce: Multiply single sequence values with single sequence values


im looking for a solution to my problem. I am using altova mapforce 2015 x64 with xslt2.

I have two sequences of numbers in xslt2. they both have the same length (and always will). e.g. they look like this:

seq1: 10 20 ...

seq2: 1 2 ...

What i need is to multiply those two in the way that seq1[0] is multiplied with seq2[0], seq[1]*seq[1] and so on.

so in my needed solution i would get the sequence 10 40 instead i get the following 10 20 20 40. so the whole sequences are multiplied.

so maybe somebody knows an answer or a workaround to that problem, both is highly appreciated.

thanks

EDIT:

the problem with altova is that i cannot add xslt2 code myself, the file will be overwritten everytime i modify something in the mapping.


Solution

  • You could use an xsl:for-each and a range to determine the position. Here's an example...

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:variable name="seq1" select="(10,20)"/>
        <xsl:variable name="seq2" select="(1,2)"/>
    
        <xsl:template match="/">
            <xsl:variable name="newSeq" as="item()*">
                <xsl:for-each select="1 to max((count($seq1),count($seq2)))">
                    <xsl:sequence select="$seq1[current()] * $seq2[current()]"/>
                </xsl:for-each>                        
            </xsl:variable>
            <results>
                <xsl:value-of select="$newSeq" separator=","/>
            </results>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Output (separated by commas just to show it's a sequence)

    <results>10,40</results>
    

    Note: This will only handle an even number of sequence items. If either sequence has more than the other, those will not be included in the output. You'd have to add additional logic if you needed to handle that situation.

    EDIT - Just saw your comment about not being able to add XSLT code yourself. I'll leave my answer here in case someone not using mapforce finds this answer useful.