3 Replies Latest reply: Mar 18, 2013 5:51 PM by vladodias RSS

    XSLT Logic

    916888
      Can any one help me to get the below output logic in xslt.

      requirement:
      In a Shipment there are number of orders are present. For each order there is a number of order line Items. Order Line Items contain item. For each iteration we are maintaining the hierarchy levels. In the orderLine Items section, if the previous item is equal to current item, there is no need to increment hierarchy.

      points:

      There is only one shipment
      Number of orders are present per shipment.
      number of orderLineItems per each order.
      for each iteration of orders and orderlineItems we have to maintain a hierarchy levels(increment value),but when comes to orderLineItems one condition is there if the previous item is equals to Current item,no need to increment hierarchy level.otherwise we should increment the hierarchy levels like below output we needed.

      below is the input.

      Input:
      <Shipment>
      <Shipment1>Shipment151</Shipment1>
      <ShipmentValue>ShipmentValue52</ShipmentValue>
      <Order>
      <Orderlevel1>Orderlevel153</Orderlevel1>
      <Orderlevel2>Orderlevel254</Orderlevel2>
      <Orderlevel3>Orderlevel355</Orderlevel3>
      <OrderLineItems>
      <Level1>Level156</Level1>
      <Level2>Level257</Level2>
      <Level3>Level358</Level3>
      <Item>Item59</Item>
      </OrderLineItems>
      <OrderLineItems>
      <Level1>Level160</Level1>
      <Level2>Level261</Level2>
      <Level3>Level362</Level3>
      <Item>Item59</Item>
      </OrderLineItems>
      <OrderLineItems>
      <Level1>Level164</Level1>
      <Level2>Level265</Level2>
      <Level3>Level366</Level3>
      <Item>Item63</Item>
      </OrderLineItems>
      </Order>
      <Order>
      <Orderlevel1>Orderlevel168</Orderlevel1>
      <Orderlevel2>Orderlevel269</Orderlevel2>
      <Orderlevel3>Orderlevel370</Orderlevel3>
      <OrderLineItems>
      <Level1>Level171</Level1>
      <Level2>Level272</Level2>
      <Level3>Level373</Level3>
      <Item>Item74</Item>
      </OrderLineItems>
      <OrderLineItems>
      <Level1>Level175</Level1>
      <Level2>Level276</Level2>
      <Level3>Level377</Level3>
      <Item>Item78</Item>
      </OrderLineItems>
      <OrderLineItems>
      <Level1>Level179</Level1>
      <Level2>Level280</Level2>
      <Level3>Level381</Level3>
      <Item>Item78</Item>
      </OrderLineItems>
      </Order>
      </Shipment>

      below is the output we need.

      Output:

      <ns1:Shipment>
      <ns1:Shipment1>1</ns1:Shipment1>
      <ns1:ShipmentValue>S</ns1:ShipmentValue>
      <ns1:Order>
      <ns1:Orderlevel1>2</ns1:Orderlevel1>
      <ns1:Orderlevel2>1</ns1:Orderlevel2>
      <ns1:Orderlevel3>O</ns1:Orderlevel3>
      <ns1:OrderLineItems>
      <ns1:Level1>3</ns1:Level1>
      <ns1:Level2>2</ns1:Level2>
      <ns1:Level3>I</ns1:Level3>
      <ns1:Item>Item59</ns1:Item>
      </ns1:OrderLineItems>
      <ns1:OrderLineItems>
      <ns1:Item>Item59</ns1:Item>
      </ns1:OrderLineItems>
      <ns1:OrderLineItems>
      <ns1:Level1>4</ns1:Level1>
      <ns1:Level2>2</ns1:Level2>
      <ns1:Level3>I</ns1:Level3>
      <ns1:Item>Item63</ns1:Item>
      </ns1:OrderLineItems>
      </ns1:Order>
      <ns1:Order>
      <ns1:Orderlevel1>5</ns1:Orderlevel1>
      <ns1:Orderlevel2>1</ns1:Orderlevel2>
      <ns1:Orderlevel3>O</ns1:Orderlevel3>
      <ns1:OrderLineItems>
      <ns1:Level1>6</ns1:Level1>
      <ns1:Level2>5</ns1:Level2>
      <ns1:Level3>I</ns1:Level3>
      <Item>Item74</Item>
      </ns1:OrderLineItems>
      <ns1:OrderLineItems>
      <ns1:Level1>7</ns1:Level1>
      <ns1:Level2>5</ns1:Level2>
      <ns1:Level3>I</ns1:Level3>
      <Item>Item78</Item>
      </ns1:OrderLineItems>
      <ns1:OrderLineItems>
      <ns1:Item>Item78</ns1:Item>
      </ns1:OrderLineItems>
      </ns1:Order>
      </ns1:Shipment>
        • 1. Re: XSLT Logic
          vladodias
          Hi,

          You're output doc looks weird... I'm wondering if the process that's going to read it won't face similar problem as you're facing now...

          I don't quite understand your requirement... But have a look at this sample as it will help you on how to get the previous value for comparison...

          Input
          <root>
          <entry><value>1</value></entry>
          <entry><value>2</value></entry>
          <entry><value>2</value></entry>
          <entry><value>4</value></entry>
          <entry><value>4</value></entry>
          </root>
          XSLT
          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
          <xsl:template match="/">
          <xsl:for-each select="/root/entry">
          <xsl:variable name="current" select="./value"/>
          <xsl:variable name="previous" select="(preceding-sibling::*)[last()]/value"/>
          <xsl:choose>
          <xsl:when test="not($previous) or $current != $previous">
          <xsl:value-of select="$current"/> different from previous
          </xsl:when>
          <xsl:otherwise>
          <xsl:value-of select="$current"/> SAME from previous
          </xsl:otherwise>
          </xsl:choose>
          </xsl:for-each>
          </xsl:template>
          </xsl:stylesheet>
          Output
          1 different from previous
          2 different from previous
          2 SAME from previous
          4 different from previous
          4 SAME from previous
          Cheers,
          Vlad
          • 2. Re: XSLT Logic
            916888
            Here I m explained.Please look.
            1)Only one Shipment.

            2)Multiple Orders per shipment (<Order> is unbounded)

            3)Multiple OrderLineitems Per order(<OrderLineItems> is unbounded inside an order)




            <Shipment>
            <Shipment1>1</Shipment1> <!-- harcodeded value -->
            <ShipmentValue>S</ShipmentValue> <! hardcoded value -->
            <Order><!-- unbounded -->
            <Orderlevel1>2</Orderlevel1><!-- sequence number should come for each iteration -->
            <Orderlevel2>1</Orderlevel2><!-- harcodeded value -->
            <Orderlevel3>O</Orderlevel3><!-- harcodeded value -->
            <OrderLineItems><!-- unbounded -->
            <Level1>3</Level1><!-- sequence number should come for each iteration -->
            <Level2>2</Level2><!-- sequence number of Orderlevel1 should come here for each iteration-->
            <Level3>I</Level3><!-- harcodeded value -->
            <Item>Item59</Item><!-- Itemvalue -->
            </OrderLineItems>

            Condition:
            while generating the sequence number When previousitem =CurentItem don't increment the sequence number.otherwise increment the sequence number.

            Edited by: 913885 on Mar 17, 2013 9:25 PM
            • 3. Re: XSLT Logic
              vladodias
              This bit here will give you the previous item from comparison, you just need to have the right namespace alias (ns1)...
              <xsl:variable name="previous" select="(preceding-sibling::*)[last()]/ns1:Item"/>
              Just remember that XSLT is a functional language, and as so it avoids state and mutable data (as opposed to a procedural language)... So you have to think your solution as functional, not as procedural (where you can increment variables)...
              http://en.wikipedia.org/wiki/Functional_programming

              Cheers,
              Vlad