Search code examples
xsltxslt-1.0xslt-2.0xslt-grouping

Splitting Multiline XML tag into multiple Nodes


I have a XML below, where new lines are added after each line at Note__c tag. I need to produce the XML by splitting them into multiple Note__c tags. Input XML-

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
	<data>
		<schema>yify-xjmoeLTbNXA560rHQ</schema>
		<payload>
			<Note__c>01/15/2020
			123456
			DFGRTE766
			6tgBFR</Note__c>
			<Line_Length__c>72.0</Line_Length__c>
			<CreatedById>00554000003OENsAAO</CreatedById>
			<Contact_Name__c/>
			<Sent_By_Name__c>SBM</Sent_By_Name__c>
			<CreatedDate>2020-01-15T16:10:40.551Z</CreatedDate>
			<Order_Number__c>14831</Order_Number__c>
			<Does_not_require_reformatting__c>false</Does_not_require_reformatting__c>
		</payload>
		<event>
			<replayId>139219</replayId>
		</event>
	</data>
	<channel>/event/Order_Note__e</channel>
</snotification>

Where Note__c contains multiple strings with new line added after each(except the last one) Expected Output -

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
	<data>
		<schema>yify-xjmoeLTbNXA560rHQ</schema>
		<payload>
      <Notes>
			<Note__c>01/15/2020</Note__c>
			<Note__c>123456</Note__c>
			<Note__c>DFGRTE766</Note__c>
			<Note__c>6tgBFR</Note__c>
      </Notes>
			<Line_Length__c>72.0</Line_Length__c>
			<CreatedById>00554000003OENsAAO</CreatedById>
			<Contact_Name__c/>
			<Sent_By_Name__c>SBM</Sent_By_Name__c>
			<CreatedDate>2020-01-15T16:10:40.551Z</CreatedDate>
			<Order_Number__c>14831</Order_Number__c>
			<Does_not_require_reformatting__c>false</Does_not_require_reformatting__c>
		</payload>
		<event>
			<replayId>139219</replayId>
		</event>
	</data>
	<channel>/event/Order_Note__e</channel>
</snotification>

I have written this XSLT but it is missing few tags under the payload element -

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="snotification/data/payload">
    <Notes>
            <xsl:for-each select="tokenize(Note__c,'\n')">
                <Note__c>
                    <xsl:value-of select="."/>
                </Note__c>
            </xsl:for-each>
        </Notes>
</xsl:template>
</xsl:stylesheet>

Output of this-

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
   <data>
      <schema>yify-xjmoeLTbNXA560rHQ</schema>
      <Notes>
         <Note__c>01/15/2020</Note__c>
         <Note__c>			123456</Note__c>
         <Note__c>			DFGRTE766</Note__c>
         <Note__c>			6tgBFR</Note__c>
      </Notes>
      <event>
         <replayId>139219</replayId>
      </event>
   </data>
   <channel>/event/Order_Note__e</channel>
</snotification>

not sure what is missing.

Thanks Sugata


Solution

  • Change your XSLT to

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <!-- identity transform -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="snotification/data/payload/Note__c">
            <Notes>
                <xsl:for-each select="tokenize(.,'\n')">
                    <Note__c>
                        <xsl:value-of select="normalize-space(.)"/>
                    </Note__c>
                </xsl:for-each>
            </Notes>
        </xsl:template>
    </xsl:stylesheet>
    

    The output should be as desired.