Nan0
Nan0

Reputation: 61

Get value next existing element after sorting

I have this xml :

<ns4:Dmpp ProductId="oLRbYoE+VfG=" deliveryEnvironment="P" code="1482223" codeType="CNK">
   <ns4:Data from="2020-03-01">
      <Reimbursable>false</Reimbursable>
   </ns4:Data>
   <ns4:Data from="2012-06-01" to="2016-07-14">
      <Price>6.3100</Price>
      <Reimbursable>false</Reimbursable>
   </ns4:Data>
   <ns4:Data from="2016-07-15" to="2019-12-11">
      <Reimbursable>false</Reimbursable>
   </ns4:Data>
      <ns4:Data from="2019-12-12" to="2020-02-29">
      <Price>7.5000</Price>
      <Reimbursable>true</Reimbursable>
   </ns4:Data>
</ns4:Dmpp>

With this xsl

<xsl:for-each select="ns4:Dmpp">
      <xsl:text>INSERT INTO DMPPACKAGING VALUES ('</xsl:text>
      <xsl:value-of select="@ProductId" />
      <xsl:text>','</xsl:text>
      <xsl:value-of select="@deliveryEnvironment" />
      <xsl:text>','</xsl:text>
      <xsl:for-each select="ns4:Data">
        <xsl:sort select="translate(@from,'-','')" order="descending" data-type="number" />
        <xsl:if test="position() = 1">
          <xsl:value-of select="@from" />
          <xsl:text>','</xsl:text>
          <xsl:choose>
            <xsl:when test="@to !=''">
              <xsl:value-of select="@to"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="'0000-00-00'" />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text>','</xsl:text>
          <xsl:value-of select="../@codeType" />
          <xsl:text>','</xsl:text>
          <xsl:value-of select="../@code" />
          <xsl:text>','</xsl:text>
          <xsl:value-of select="Reimbursable" />
          <xsl:text>','</xsl:text>
          <xsl:choose>
            <xsl:when test="Price !=''">
              <xsl:value-of select="Price"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="../ns4:Data/following-sibling::ns4:Data/Price" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:for-each>
      <xsl:text>');</xsl:text>
    </xsl:for-each>

As you can see in this example the Position() = 1 give me only the data of the <Reimbursable> element.

I would like to get the more recent know Price of product. With my code i only get it if it's the second element of sorted element but it's not always the case.

Any help will be appreciated !

Upvotes: 0

Views: 73

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117102

Actually, my suggestion was to do:

<xsl:text>MOST RECENT REIMBURSABLE: </xsl:text>
<xsl:for-each select="ns4:Data[Reimbursable]">
    <xsl:sort select="@from" order="descending" />
    <xsl:if test="position() = 1">
        <xsl:value-of select="@from"/>
    </xsl:if>
</xsl:for-each>
<xsl:text> MOST RECENT PRICE: </xsl:text>
<xsl:for-each select="ns4:Data[Price]">
    <xsl:sort select="@from" order="descending" />
    <xsl:if test="position() = 1">
        <xsl:value-of select="@from"/>
    </xsl:if>
</xsl:for-each>

Upvotes: 1

Nan0
Nan0

Reputation: 61

Here is how i solved my issue with suggestion of @michael.hor257k :

I'll only put the code inside the <xsl:choose> where i need to get the more recent Price element.

<xsl:choose>
   <xsl:when test="Price !=''">
      <xsl:value-of select="Price"/>
   </xsl:when>
   <xsl:otherwise>            
      <xsl:for-each select="../ns4:Data/Price">
         <xsl:sort select="translate(../@from,'-','')" order="descending" data-type="number" />
         <xsl:if test="position() = 1">
            <xsl:value-of select="." />
         </xsl:if>
      </xsl:for-each>
   </xsl:otherwise>
</xsl:choose>

Upvotes: 0

Related Questions