0

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!

NEO
  • 389
  • 8
  • 31
  • Try to adapt the first stylesheet in the answer http://stackoverflow.com/a/38160578/252228 to this problem, it is nearly doing what you ask for here. – Martin Honnen Jul 05 '16 at 16:05
  • Here After adopting the first stylesheet getting this : http://xsltransform.net/pPzifqt/5 – NEO Jul 05 '16 at 16:30
  • Well, your input data is slightly different, but if you adapt the path to `` then you get the new elements created. You need some more edits, but I think you should be able to adjust a path or a literal result element on your own. – Martin Honnen Jul 05 '16 at 17:31
  • After applying this : getting following error:: http://paste.ofcode.org/f7w5f5ZivbcDLRfnaZakg7 – NEO Jul 06 '16 at 02:06
  • @MartinHonnen i just searched one post from stackoverflow .you answered previously. : http://stackoverflow.com/questions/35453956/how-to-process-a-sequence-with-one-or-more-items/35458867 – NEO Jul 06 '16 at 03:15
  • I get no such error in http://xsltransform.net/pPzifqt/6 so you probably have a different input document. Make sure you select only one element as the first argument to tokenize. – Martin Honnen Jul 06 '16 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116554/discussion-between-vickps-and-martin-honnen). – NEO Jul 06 '16 at 09:43

0 Answers0