Search code examples
xmlxslt

Concat element in a loop with Separator


The problem I'm facing seems simple. What I want to do is build a string by concatenating the results of a foreach element loop.

Data XML:

<OutboundPaymentInstruction>
<DocumentPayable>
            <DocumentNumber>
                <ApplicationInternalID>200</ApplicationInternalID>
                <ReferenceNumber>TEST_003</ReferenceNumber>
                <DocCategory>STD INV</DocCategory>
                <SequenceName>SEQ1</SequenceName>
                <SequenceValue>520822000792</SequenceValue>
            </DocumentNumber>
</DocumentPayable>          
<DocumentPayable>
            <DocumentNumber>
                <ApplicationInternalID>200</ApplicationInternalID>
                <ReferenceNumber>TEST_004</ReferenceNumber>
                <DocCategory>STD INV</DocCategory>
                <SequenceName>SEQ1</SequenceName>
                <SequenceValue>520822000792</SequenceValue>
            </DocumentNumber>
</DocumentPayable>
<DocumentPayable>
            <DocumentNumber>
                <ApplicationInternalID>200</ApplicationInternalID>
                <ReferenceNumber>TEST_005</ReferenceNumber>
                <DocCategory>STD INV</DocCategory>
                <SequenceName>SEQ1</SequenceName>
                <SequenceValue>520822000792</SequenceValue>
            </DocumentNumber>
</DocumentPayable>
</OutboundPaymentInstruction>

Expected output: TEST_003/TEST_004/TEST_005 and should not cross 140 characters.

I tried with XSL file but ReferenceNumber is coming as like in the Image enter image description here


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="OutboundPaymentInstruction">
        <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <xsl:variable name="Invoices" select="DocumentNumber/ReferenceNumber"/>
            <Ustrd>
                <xsl:for-each select="DocumentPayable/DocumentNumber">
                    <xsl:choose>
                        <xsl:when test="position() = 1">
                            <xsl:value-of select="ReferenceNumber"/>
                        </xsl:when>
                        <xsl:otherwise>
            /<xsl:value-of select="ReferenceNumber"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </Ustrd>
        </Document>
    </xsl:template>
</xsl:stylesheet>

Please help.


Solution

  • You could simplify the contents of your for-each loop, and omit the implicit whitespace:

                <xsl:for-each select="DocumentPayable/DocumentNumber">
                    <xsl:if test="position() &gt; 1">
                        <xsl:text>/</xsl:text>
                    </xsl:if>
                    <xsl:value-of select="ReferenceNumber"/>
                </xsl:for-each>