0

Seeking one help on xsl payment file template. I have to restrict the length of a tag after for loop to 140 characters. In attached xml, after concatenating the DocumentPayable/DocumentNumber/ReferenceNumber, I want to print only 140 characters in Ustrd tag for one payment info tag (i.e. PmtInf). How can this be achieved via xsl? I am attaching the current working xsl template wherein I am able to concatenate the values and xml file which has got the sample data.

<xsl:variable name="varustrd" select="oraext:create-delimited-string (DocumentPayable/DocumentNumber/ReferenceNumber, ',' )"/>

<xsl:for-each select="DocumentPayable">

<Ustrd>
<xsl:value-of select="(DocumentNumber/ReferenceNumber)"/>
</Ustrd>

<Ustrd1>
<xsl:value-of select="$varustrd"/>
</Ustrd1>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Can you make use of the [function](https://www.w3.org/TR/xpath-10/#function-substring) `substring($varustrd,1,140)`? – Heiko Theißen Feb 13 '23 at 16:15
  • no, this is not working. what I require is that the output of this for loop should be truncated to 140 characters in the final output. This is the for loop I am using and giving me the full string. Which I have to chop down to 140 characters................ , – Saurabh Maheshwari Feb 15 '23 at 05:10

1 Answers1

0

You can fill a variable with the concatenation and the output only its first 140 characters:

<xsl:variable name="char140">
  <xsl:for-each select="DocumentPayable/DocumentNumber/ReferenceNumber">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">,</xsl:if>
  </xsl:for-each>
</xsl:variable>
<Ustrd>
  <xsl:value-of select="substring($char140,1,140)"/>
</Ustrd>

If you want to produce multiple <Ustrd> elements, each with 140 characters, you need a recursive template

<xsl:call-template name="char140">
  <xsl:with-param name="s" select="$char140"/
</xsl:call-template>

which is defined in a top-level element

<xsl:template name="char140">
  <xsl:param name="s"/>
  <xsl:if test="$s">
    <Ustrd>
      <xsl:value-of select="substring($s,1,140)"/>
    </Ustrd>
    <xsl:call-template name="char140">
      <xsl:with-param name="s" select="substring($s,141)"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • This worked. Thanks Heiko. Superb – Saurabh Maheshwari Feb 15 '23 at 07:35
  • One more question in continuation of this - if I want to generate next Ustrd tag which will start from 141st value to next 140 characters in the next tag and then similarly from 281st character to 420th character (i.e. each Ustrd tag should have 140 characters and after that it should go to next tag). Pls suggest how should I make this change – Saurabh Maheshwari Feb 15 '23 at 07:58
  • Thanks for sharing this template... when I injected this template and invoked the template in xsl, it is breaking the tag as expected, but xmlns="" is coming extra after tag name. I am getting the output as below. how to remove this pls? 2017SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS,250322,310320,41310232,Barter Rectify- May20,SAJ/2018/3408,S – Saurabh Maheshwari Feb 15 '23 at 08:36
  • This means that you have an `xmlns=""` attribute somewhere in your stylesheet and expect the `` element to be in the default namespace. Replace `` with `` in the `` in this case. If you continue to have problems, please raise a new question on StackOverflow for them. – Heiko Theißen Feb 15 '23 at 10:00
  • Thanks a ton. This worked. Appreciate it. – Saurabh Maheshwari Feb 15 '23 at 10:46
  • One last question - By any chance can we control the repetation of this tag to only four times? i.e. 140*4 = 560 characters should be printed in 4 tags. And lets say even after that there is a possibility of 5th tag to come, we need to suppress it. i.e. we just need 4 tags irrespective of how many USTRD tags should be generated based on the for loop output? can we restrict the occurrence to 4 times only? – Saurabh Maheshwari Feb 15 '23 at 10:49
  • In this case it would be easier to have four elements like in my first example, surrounded by `` to avoid empty `` elements. No recursion needed then. – Heiko Theißen Feb 15 '23 at 10:52
  • Sure, will do that. Would you please mind helping me on this question as well. Highly appreciate your quick turn around time. https://stackoverflow.com/questions/75455742/replacing-characters-in-xml?noredirect=1#comment133138485_75455742 – Saurabh Maheshwari Feb 15 '23 at 10:56