如何在xml中显示列表的最后一个元素?

Posted

技术标签:

【中文标题】如何在xml中显示列表的最后一个元素?【英文标题】:How to display the last element of a list in xml? 【发布时间】:2021-12-17 17:27:35 【问题描述】:

我有以下 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>

在这种情况下,决策中有 2 个项目。里面可以只有一件,也可以多于两件。在我准备的文件中,我必须始终显示最后一个,因为它是当前的。我目前正在使用以下代码,我需要对其进行返工:

    <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>

如何修改示例以便仅显示 xml 的最后一项? 是否有必要在决定/项目中放置一个 id 以某种方式对其进行排序 提前致谢!

【问题讨论】:

【参考方案1】:

如果你匹配最后一个元素,例如match="decisions/item/fees/item[last()]" 那么您的模板仅适用于任何 fees 父元素的任何最后一个 item 子元素(在所有 item 子元素中)。

【讨论】:

感谢您的回复。但是,在这种情况下,我在两个项目中都获得了最后一个建议的金额 - 2 和 2.2 我需要从第二个项目中获得建议的金额,即 32、2.2 和 2.2 @Bellerephon,这听起来好像您想在 decisions/item[last()]/fees/item 上使用 last() 谓词,但我还没有完全理解您的 XML 词汇表。 抱歉表达错误。它发生了你写它的方式,我很感激!美好而成功的一天!

以上是关于如何在xml中显示列表的最后一个元素?的主要内容,如果未能解决你的问题,请参考以下文章

如何按元素内容过滤解析的XML数据[jQuery]

如何删除一个list中最后一个元素

如何在 Python 中以不同方式处理列表中的最后一个元素?

如何删除添加到列表中的最后一个元素?

如何在R中循环中的最后一个元素之后附加到列表?

如何按元素内容过滤解析的 XML 数据 [jQuery]