Home > Software engineering >  How to display the last element of a list in xml?
How to display the last element of a list in xml?

Time:11-03

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 !

CodePudding user response:

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.

  • Related