We have Input XML. WE want to split OrderDescription element at new line character into different part such that it will create new substring as Comment element IN Output XML.
Let's say Number of string is 6 . so , it should create 6 new comment element in Stockline element in output XML.
that is for ex new tag portion should be created on each split
<StockLine>
<StockCode></StockCode>
<Comment>Some Comment as split string</comment>
</StockLine>
Also OrderDetail is Repeating node here. and it is important to have Each comment associated with StockCode element is empty.
Input XML :
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<Customer>000016</Customer>
<OrderDate>2016-04-19</OrderDate>
<SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
</OrderHeader>
<OrderDetails>
<StockLine>
<StockCode>ABB-CDE-FGH-01</StockCode>
<OrderDescription>EDIORDER-SAVE COMMENTS
C3 Generic
LOC 0833
Expected arrival 01/07/2016
OTYPE NE
TRKPC 01 GM/00007643020008361321</OrderDescription>
<OrderLineID>OR-1561179</OrderLineID>
</StockLine>
</OrderDetails>
</Orders>
</SalesOrders>
We have applied XSLT AS:
<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="StockLine">
<xsl:variable name="i" select="position()" />
<xsl:copy>
<xsl:copy-of select="StockCode"/>
<NComment>
<xsl:value-of select="normalize-space(tokenize(../StockLine[1]/OrderDescription, '\n')[$i])"/>
</NComment>
<xsl:copy-of select="OrderLineID"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Expected Output XML:
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<Customer>000016</Customer>
<OrderDate>2005-04-19</OrderDate>
<SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
</OrderHeader>
<OrderDetails>
<StockLine>
<StockCode></StockCode>
<Comment>EDIORDER-SAVE COMMENTS</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>C3 Generic</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>LOC 0833</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>Expected arrival 01/07/2016</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>OTYPE NE</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>TRKPC 01 GM/00007643020008361321</comment>
</StockLine>
Thanks In Advance!