How to display the last element of a list in xml?

I have the following xml:

<decisions type="Decision[]">
    <item>
      <fees type="Fees[]">
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax1]]></name>
            <value type="String"><![CDATA[15]]></value>
          </feeType>
          <percent type="String"></percent>
          <amount type="String"><![CDATA[25]]></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"></maxAmount>
          <suggestedAmount type="String"><![CDATA[30]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax2]]></name>
            <value type="String"><![CDATA[16]]></value>
          </feeType>
          <percent type="String"><![CDATA[1]]></percent>
          <amount type="String"></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"><![CDATA[500]]></maxAmount>
          <suggestedAmount type="String"><![CDATA[2]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax3]]></name>
            <value type="String"><![CDATA[18]]></value>
          </feeType>
          <percent type="String"><![CDATA[1]]></percent>
          <amount type="String"></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"></maxAmount>
          <suggestedAmount type="String"><![CDATA[2]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
      </fees>
    </item>
    
    <item>
      <fees type="Fees[]">
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax1]]></name>
            <value type="String"><![CDATA[15]]></value>
          </feeType>
          <percent type="String"></percent>
          <amount type="String"><![CDATA[30]]></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"></maxAmount>
          <suggestedAmount type="String"><![CDATA[32]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax2]]></name>
            <value type="String"><![CDATA[17]]></value>
          </feeType>
          <percent type="String"><![CDATA[1]]></percent>
          <amount type="String"></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"><![CDATA[500]]></maxAmount>
          <suggestedAmount type="String"><![CDATA[2.20]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
        <item>
          <feeType type="NameValuePair">
            <name type="String"><![CDATA[Tax3]]></name>
            <value type="String"><![CDATA[18]]></value>
          </feeType>
          <percent type="String"><![CDATA[2]]></percent>
          <amount type="String"></amount>
          <currency type="NameValuePair">
            <name type="String"><![CDATA[EUR]]></name>
            <value type="String"><![CDATA[EUR]]></value>
          </currency>
          <minAmount type="String"></minAmount>
          <maxAmount type="String"></maxAmount>
          <suggestedAmount type="String"><![CDATA[2.20]]></suggestedAmount>
          <comment type="String"></comment>
        </item>
      </fees>
    </item>
  </decisions>

In this case there are 2 items in the decisions. There can be only one item inside, also more than two. In the document I am preparing, I must always show the last one, because it is the current one. I am currently using the following code, which I need to rework:

    <xsl:template match="decisions/item/fees/item">
      <xsl:if test="feeType/name != ''">
        <xsl:value-of select="feeType/name"/>
          <xsl:choose>
            <xsl:when test="suggestedAmount &gt; 0">
              <xsl:text> </xsl:text><xsl:value-of select="suggestedAmount"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="amount &gt; 0">
                <xsl:text> </xsl:text><xsl:value-of select="amount"/><xsl:text> </xsl:text><xsl:value-of select="currency/name"/>
              </xsl:if>
              <xsl:if test="percent &gt; 0">
                <xsl:text> </xsl:text><xsl:value-of select="percent"/><xsl:text> </xsl:text>%
              </xsl:if> 
            </xsl:otherwise>
          </xsl:choose>
     </xsl:if>           
   </xsl:template>

How to rework the example so that I can show only the last item of the xml ?
Will it be necessary to put an id in the decisions/item to sort it in some way
Thanks in advance !

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you match on the last element with e.g. match="decisions/item/fees/item[last()]" then your template only applies to any last item child (of all item children) of any fees parent element.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x