Tuesday, October 22, 2024

OIC - Converting one XML Field Values to distinct Comma-Separated Entries" | How do I generate a comma-separated list with XSLT/XPath?

Usecase:

Here, we will demonstrate how we can fetch unique comma separeated values for a xml field values.

For instance , here

Input file:

<Root>

  <qualification_code>A</qualification_code>

  <qualification_code>B</qualification_code>

  <qualification_code>A</qualification_code>

  <qualification_code>C</qualification_code>

  <qualification_code>B</qualification_code>

</Root>

Expected Output:

  "A", "B", "C"


Solution:

Implementation steps:

  1. Use distinct-values() function to get the distinct node values.
  2. For each record, take the values using select ="."
  3. Using text field to hard codes like  quotes or comma and space
  4. Use position() != last() condition to avoid the last comma.

xslt code:

<xsl:template match="/" xml:id="id_11">

    <nstrgmpr:Write xml:id="id_12">

        <ns22:RecordSet xml:id="id_31">

            <ns22:Record xml:id="id_32">

                <ns22:Test xml:id="id_36">

                    <xsl:for-each xml:id="id_35" select="distinct-values($readFile/nsmpr2:ReadResponse/ns25:RecordSet/ns25:Record/ns25:QUALIFICATION_CODE)">

                        <xsl:text>"</xsl:text>

                        <xsl:value-of xml:id="id_37" select="."/>

                       <xsl:text>"</xsl:text>

                        <xsl:text>,</xsl:text>

                        <xsl:if test="position() != last()">

                            <xsl:text>, </xsl:text>

                        </xsl:if>

                    </xsl:for-each>

                </ns22:Test>

            </ns22:Record>

        </ns22:RecordSet>

    </nstrgmpr:Write>

</xsl:template>

Screenshot:





No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...