【问题标题】:How to get the last records that match a specific element value using xslt?如何使用 xslt 获取与特定元素值匹配的最后一条记录?
【发布时间】:2021-05-07 20:49:35
【问题描述】:

我有一个 xml,其中包含如下所示的汽车记录列表

 <Car>
    <color>red</color>
    <type>toyota</type>
   <status>done</status>
  </Car>
  <Car>
    <color>green</color>
    <type>mazda</type>
    <status>done</status>
  </Car>
  <Car>
    <color>yellow</color>
    <type>ford</type>
  <status>working</status>
  </Car>
  <Car>
    <color>green</color>
    <type>mazda</type>
    <status>pending</status>
  </Car>

我目前正在获取最后一次这样的汽车记录

<Cars>
      <Car> 
          <xsl:copy-of select="//Car[last()]"/>
      </Car>       
  </Cars>

我想看看是否有可能获得与我的元素值匹配的最后一辆汽车记录

例子

从上面的 xml 示例中,我想获取不包含挂起状态的最后一辆汽车记录,因此我的输出如下所示。

这是最后一条符合我条件的记录

 <Car>
    <color>yellow</color>
    <type>ford</type>
  <status>working</status>
  </Car>

而且也只有这样的我想要的记录。

<Cars>
<Car>
        <color>yellow</color>
      <status>working</status>
      </Car>
<Cars>

【问题讨论】:

    标签: c# xml xslt


    【解决方案1】:

    你可以这样做:

    <Cars>
      <Car> 
          <xsl:copy-of select="//Car[not(status='pending')][last()]"/>
      </Car>       
    </Cars>
    

    【讨论】:

    • 你可以在这里测试它:xsltfiddle.liberty-development.net/gVrvcxU
    • 如果我只想获取颜色和状态,我能做什么,所以输出看起来像这样 yellowworking汽车>
    • 阅读有关 XSLT 工作原理的书籍或教程?
    • 你推荐我读什么书或教程来帮助我。谢谢
    • 我有这本书,发现它是一本很好的参考书:XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2010-11-30
    • 2012-01-23
    • 2023-01-28
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多