4 Replies Latest reply: Mar 20, 2007 9:25 AM by 560761 RSS

    How to sum strings?

    451057
      Hi,

      I am doing an XSLT transformation in BPEL. I have a situation where I have a collection with strings that I need to concat into one target field. The structure is like this:
      <qwerty>
          <animal>cat</animal>
          <animal>dog</animal>
          <animal>mouse</animal>
      </qwerty>
      I need this in the target: catdogmouse

      How do I do this?

      Regards Pete
        • 1. Re: How to sum strings?
          560761
          Don't really understand what you want to do,
          but this will concatenate the text in the elements
          and store it in a 'target' variable:
          ...
          <xsl:template match="qwerty">
          <xsl:variable name="target">
          <xsl:apply-templates select="animal" mode="text"/>
          </xsl:variable>
          <xsl:value-of select="$target"/>
          </xsl:template>

          <xsl:template match="animal" mode="text">
          <xsl:value-of select="text()"/>
          </xsl:template>

          Mik
          • 2. Re: How to sum strings?
            451057
            Thanks Mik,

            I have a translation in BPEL where the source schema can contain a number of strings, but the target schema needs these in one field. I think it is some kind of comments.

            Everything takes place in one giant transformation so it needs to be done within one match. I have tried with for-each but have not been able to get it working.

            Regards Pete
            • 3. Re: How to sum strings?
              451057
              OK I got it working.
              <xsl:variable name="f_191_1_dummy">
                <xsl:for-each select="/tns1:hentNesteIndberetningResponse/tns1:hentNesteIndberetningReturn/tns1:Report/ns8:FertilizerAccountStructure/ns8:ConsumptionNitrogenStructure/ns8:OrganicFertilizerExceptManureStructure/ns8:FertilizerQuantitiesIncludedInAccountStructure/ns8:FertilizerQuantityCollection/ns8:FertilizerQuantityStructure/ns8:UnclassifiedFertilizer[../ns8:UnclassifiedFertilizer != '']">
                  <xsl:value-of select="concat(normalize-space(./text()),' ')"/>
                </xsl:for-each>
              </xsl:variable>
              <ns0:F_191_1>
                 <xsl:value-of select="substring($f_191_1_dummy,1,255)"/>
              </ns0:F_191_1>
              It works but please let me know if there is a better solution.

              Regards Pete
              • 4. Re: How to sum strings?
                560761
                If it works, don't touch it !

                I prefer using the functional programming style with apply-templates and match,
                rather than the imperative call-template or for-each, it is closer to the Zen of XSLT.
                Just as structured programming is about getting rid of the goto,
                and OO polymorphism is about getting rid of the switch/case statement,
                so functional programming is about pattern matching not iteration and invocation.

                Note how you can use modes to distinguish between multiple templates
                that match on the same pattern. In the example above, adding the template
                for animal with mode='text' does not interfere with any other animal template.
                The mode names are arbitrary, there is nothing special about 'text' in this context,
                I could have used mode='Zen'.

                Mik