XSLT 1.0 looping

107 views Asked by At

I am unsure how to switch from for-each to loop logic with some count logic

Here is the small sample of the xml. The sequence numbers and discount numbers can range greatly. I have already sorted prior so the xml is in the correct order.

<ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:RETAILSEQUENCENUMBER>1</ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER>1</ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE>ZD01</ns2:DISCOUNTTYPECODE>
</ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
<ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:DATAHEADERCOLUMN_SEGNAM>/POSDW/E1BPLINEITEMDISCOUNT002</ns2:DATAHEADERCOLUMN_SEGNAM>
    <ns2:RETAILSEQUENCENUMBER>1</ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER>2</ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE>Z407</ns2:DISCOUNTTYPECODE>
</ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
<ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:DATAHEADERCOLUMN_SEGNAM>/POSDW/E1BPLINEITEMDISCOUNT002</ns2:DATAHEADERCOLUMN_SEGNAM>
    <ns2:RETAILSEQUENCENUMBER>1</ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER>3</ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE>Z407</ns2:DISCOUNTTYPECODE>
</ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:RETAILSEQUENCENUMBER>2</ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER>1</ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE>ZD01</ns2:DISCOUNTTYPECODE>
</ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
<ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:DATAHEADERCOLUMN_SEGNAM>/POSDW/E1BPLINEITEMDISCOUNT002</ns2:DATAHEADERCOLUMN_SEGNAM>
    <ns2:RETAILSEQUENCENUMBER>2</ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER>2</ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE>Z407</ns2:DISCOUNTTYPECODE>
</ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>

Here is my XSLT

      <xsl:for-each select="ns0:idocData/ns2:_x002F_POSDW_x002F_E1POSTR_CREATEMULTIP001GRP">
        <xsl:for-each select="ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002">
          <ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
    <ns2:DATAHEADERCOLUMN_SEGNAM><xsl:value-of select="ns2:DATAHEADERCOLUMN_SEGNAM"/></ns2:DATAHEADERCOLUMN_SEGNAM>
    <ns2:RETAILSEQUENCENUMBER><xsl:value-of select="ns2:RETAILSEQUENCENUMBER"/></ns2:RETAILSEQUENCENUMBER>
    <ns2:DISCOUNTSEQUENCENUMBER><xsl:value-of select="ns2:DISCOUNTSEQUENCENUMBER"/></ns2:DISCOUNTSEQUENCENUMBER>
    <ns2:DISCOUNTTYPECODE><xsl:value-of select="ns2:DISCOUNTTYPECODE"/></ns2:DISCOUNTTYPECODE>
          </ns2:_x002F_POSDW_x002F_E1BPLINEITEMDISCOUNT002>
        </xsl:for-each>
      </xsl:for-each>

My requirement is to loop though the xml and where a ns2:DISCOUNTTYPECODE is found to be ZD01 make that ZD11. If there is another ZD01 found within the same ns2:RETAILSEQUENCENUMBER then make the next DISCOUNTTYPECODE = ZD12 and then ZD13 etc. Only on the ZD01 records.

Once the XML then falls onto the next RETAILSEQUENCENUMBER the logic then needs to start again so that ZD01 becomes ZD11.

1

There are 1 answers

1
Matt Stephens On BEST ANSWER

I'd recommend writing another transform which would preprocess the data to sort and group it using one of the techniques from the dpawson site mentioned above, this would make determining the discount types a lot easier.

Once the data has been grouped and sorted, this transform would be concerned only with any changes to discount types etc. You could then look at how many previous ZD01 siblings there are and infer the new discount type based on the number of previous siblings

  • 0 previous ZD01 elements = ZD11
  • 1 previous ZD01 element = ZD12
  • etc.
  • etc.

The previous siblings could also be used to determine whether renumbering needs to start again based on a different retail number value.