如何使用 xsl 1.0 查找最小值和最大值?

2024-01-31

文件_1.xml

<productlist>
    <items>
        <item>Pen</item>
        <price>8</item>
    </items>
    <items>
        <item>Pen</item>
        <price>5</item>
    </items>
    <items>
        <item>Pen</item>
        <price>10</item>
    </items>
    <items>
        <item>Bag</item>
        <price>15</item>
    </items>
    <items>
        <item>Bag</item>
        <price>22</item>
    </items>
    <items>
        <item>Bag</item>
        <price>20</item>
    </items>
</productlist>

文件_2.xml

<productlist>
    <items>
        <item>Pen</item>
    </items>
    <items>
        <item>Bag</item>
    </items>
</productlist>

需要使用 xsl 1.0 输出如下所示的最大值和最小值

<productlist>
    <items>
        <item>Pen</item>
        <min>5</min>
        <max>10</max>
    </items>
    <items>
        <item>Bag</item>
        <min>15</min>
        <max>22</max>
    </items>
</productlist>

排序,然后取第一项作为最小值,最后一项作为最大值:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="data-url" select="'file_1.xml'"/>
<xsl:variable name="data-doc" select="document($data-url)"/>

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="k1" match="items" use="item"/>

<xsl:template match="@* |  node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="items">
  <xsl:variable name="this" select="."/>
  <xsl:copy>
    <xsl:copy-of select="item"/>
    <xsl:for-each select="$data-doc">
      <xsl:for-each select="key('k1', $this/item)">
         <xsl:sort select="price" data-type="number" order="ascending"/>
         <xsl:if test="position() = 1">
           <min>
             <xsl:value-of select="price"/>
           </min>
         </xsl:if>
         <xsl:if test="position() = last()">
           <max>
             <xsl:value-of select="price"/>
           </max>
         </xsl:if>
       </xsl:for-each>
     </xsl:for-each>
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

当我在输入文档上使用 Saxon 6.5.5 应用上述样式表时

<productlist>
    <items>
        <item>Pen</item>
    </items>
    <items>
        <item>Bag</item>
    </items>
</productlist>

另一个文件在哪里

<productlist>
    <items>
        <item>Pen</item>
        <price>8</price>
    </items>
    <items>
        <item>Pen</item>
        <price>5</price>
    </items>
    <items>
        <item>Pen</item>
        <price>10</price>
    </items>
    <items>
        <item>Bag</item>
        <price>15</price>
    </items>
    <items>
        <item>Bag</item>
        <price>22</price>
    </items>
    <items>
        <item>Bag</item>
        <price>20</price>
    </items>
</productlist>

我得到了想要的结果

<productlist>
   <items>
      <item>Pen</item>
      <min>5</min>
      <max>10</max>
   </items>
   <items>
      <item>Bag</item>
      <min>15</min>
      <max>22</max>
   </items>
</productlist>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 xsl 1.0 查找最小值和最大值? 的相关文章

随机推荐

  • 如何在 cmake 上获取库的完整本机名称?

    我需要将构建的库的本机名称 libfoo so 或 foo dll 传递给 add custom command 如何获取目标的完整库名称 该财产LOCATION有它但有完整的路径 属性 OUTPUT NAME 不返回任何内容 您可以使用生
  • 使用 Storyboard 中的属性字符串本地化 UILabel

    我有一个 UILabel 其文本在情节提要中设置为 归属 当我生成 Main strings 文件以翻译成其他语言时 该标签的文本不会出现 我尝试通过复制对象 ID 手动将条目添加到 Main strings 文件中 我尝试设置 text
  • Firebird x Windows 7 x gds32.dll错误

    我有一个来自新客户的 fdb 文件 firebird 他不知道版本 我尝试过使用一些 GUI 来访问数据库 但没有成功 他们都说它缺少 gds32 dll 但我有这个 我已将此 dll 复制到 GUI 文件夹 已将 dll 复制到 syst
  • GlGenTextures 不断返回 0

    我正在尝试生成这样的纹理 define checkImageWidth 64 define checkImageHeight 64 static GLubyte checkImage checkImageHeight checkImageW
  • 如何模拟 @InjectMocks 类的方法?

    例如我有处理程序 Component public class MyHandler AutoWired private MyDependency myDependency public int someMethod return anoth
  • 回调()或返回回调()

    可能我对 Node 的事件循环了解不够 说我有一个函数foo其中包含一个异步函数async func 我有吗 1 function foo callback stuff here async func function do somethi
  • 负载平衡线程请求百分比

    我有一个工作线程池 我在其中根据百分比向它们发送请求 例如 工作人员 1 必须处理总请求的 60 工作人员 2 必须处理总请求的 31 最后工作人员 3 必须处理 9 我需要从数学上知道如何缩小数字并保持比率 这样我就不必向线程 1 发送
  • 如何在 MFC 中的 CRichEditCtrl 中显示红色波浪线

    我正在致力于在 MFC 应用程序中实现拼写检查器 我想要做的是在拼写错误的单词下显示红线 我找到了一个示例 但它仅适用于简单的编辑框 因为它可以简单地使用编辑控件默认字体进行计算来绘制波浪线 但它不适用于丰富的编辑控件 因为在丰富的编辑控件
  • Vue组件通信

    我有两个 Vue 组件 Vue component A Vue component B 如何从组件B访问组件A 组件之间的通信如何进行 跨组件通信在 Vue js 文档中并没有引起太多关注 也没有很多教程涵盖这个主题 由于组件应该是隔离的
  • 用于生成用户友好的相对时间戳的 PHP 库

    我正在寻找 PHPlibrary生成用户友好的时间表示 例如 当时间戳为 两小时前 时time 2 3600 有几个现有问题及其答案和博客文章 1 https stackoverflow com questions 2690504 php
  • 为什么 build_runner 在 dart/flutter 中序列化 JSON 时不生成文件

    我只是尝试使用 3 个月前存储的相同命令生成文件 我对后端和 devops 不太擅长 但现在它不再生成文件了 它告诉我使用我不记得的删除命令 但即使使用该命令 我的文件仍然不会生成 这就是我的日志的样子 下面您可以找到一个简单类的代码 我的
  • 为什么 swift 中函数有多种返回类型?

    我注意到这个函数有一个独特的返回类型 func chooseStepFunction backward Bool gt Int gt Int func stepForward input Int gt Int return input 1
  • FileSystemWatcher 在网络机器上设置凭据

    有没有办法为 filesystemwatcher 对象设置凭据 该应用程序在不同的用户上运行 该用户无权访问网络计算机上的目录 但我想向 filesystemwatcher 对象提供凭据 以便它可以侦听该目录 可行吗 不需要 只需确保您的应
  • PHP 强制下载损坏的 .xlsx 文件

    我正在开发一个允许教师上传文档和学生下载文档的网站 然而 有一个问题 Microsoft Word docx 文件下载完美 但下载 Excel xlsx 文件时 Excel 会显示 此文件已损坏 无法打开 对话框 任何对此的帮助将不胜感激
  • Windows 手机 8.1 |如何判断本地文件夹中是否存在文件?

    如何确定文件是否存在于本地文件夹中 Windows Storage ApplicationData Current LocalFolder 在 Windows Phone 8 1 上 不幸的是 目前没有直接的方法来检查文件是否存在 您可以尝
  • PyQt5 QWebEngineView不显示网页

    The part where webpage should be rendered gets white for a fraction of second and then gets empty 这是我的代码 基本上是https www p
  • 构建两个独立数据库集成的最佳方法?

    我在工作中遇到了以下问题 我没有经验或知识来回答这些问题 我希望你们中的一些明智的人能够为我指明正确的方向 任何答案将不胜感激 Scenario 实施立面图案 http en wikipedia org wiki Facade patter
  • 捕获 async void 方法抛出的异常

    使用 Microsoft for NET 的异步 CTP 是否可以在调用方法中捕获异步方法抛出的异常 public async void Foo var x await DoSomethingAsync Handle the result
  • 使用 Python 或其他方法从 PDF 中提取指向另一个 PDF 中页面的链接

    我有 5 个 PDF 文件 每个文件都有指向另一个 PDF 文件中不同页面的链接 这些文件都是大型 PDF 的目录 每个大约 1000 页 使得手动提取成为可能 但非常痛苦 到目前为止 我已尝试在 Acrobat Pro 中打开该文件 我可
  • 如何使用 xsl 1.0 查找最小值和最大值?

    文件 1 xml