We have input XML.In that,StockCode element is empty for many line items (OrderLineID elements). For all empty StockCode element in XML there must be Comment element value. Similarly,For all such empty Comment elements there must be StockCode Value Exist.
Note :
OrderDetail is repeating Node here.
Scenario:
We have to split OrderDescription string . such a way that it always look for empty StockCode element. then only Assign split string values to Comment elements. Otherwise for all StockCode having value should not assign split string to Comment Element
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>
<StockLine>
<StockCode>BCD-EFGH-01</StockCode>
<OrderLineID>OR-1561186</OrderLineID>
</Comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561180</OrderLineID>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561181</OrderLineID>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561182</OrderLineID>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561183</OrderLineID>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561184</OrderLineID>
</StockLine>
<StockLine>
<StockCode></StockCode>
</Comment>
<OrderLineID>OR-1561185</OrderLineID>
</StockLine>
</OrderDetails>
</Orders>
</SalesOrders>
Existing XSLT For Transformation:
XSLT2.0
<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"/>
<Comment>
<xsl:value-of select="normalize-space(tokenize(../StockLine[1]/OrderDescription, '
')[$i])"/>
</Comment>
<xsl:copy-of select="OrderLineID"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Expected Output value:
<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>
</Comment>
<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>
<StockLine>
<StockCode>BCD-EFGH-01</StockCode>
<OrderLineID>OR-1561186</OrderLineID>
</Comment>
</StockLine>
<StockLine>
<Comment>EDIORDER-SAVE COMMENTS</Comment>
</StockCode>
<OrderLineID>OR-1561180</OrderLineID>
</StockLine>
<StockLine>
<Comment>C3 Generic</Comment>
</StockCode>
<OrderLineID>OR-1561181</OrderLineID>
</StockLine>
<StockLine>
<Comment>LOC 0833</Comment>
</StockCode>
<OrderLineID>OR-1561182</OrderLineID>
</StockLine>
<StockLine>
<Comment>Expected arrival 01/07/2016</Comment>
</StockCode>
<OrderLineID>OR-1561183</OrderLineID>
</StockLine>
<StockLine>
<Comment> OTYPE NE</Comment>
</StockCode>
<OrderLineID>OR-1561184</OrderLineID>
</StockLine>
<StockLine>
<Comment>TRKPC 01 GM/00007643020008361321</Comment>
</StockCode>
<OrderLineID>OR-1561185</OrderLineID>
</StockLine>
</OrderDetails>
</Orders>
</SalesOrders>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…