xml - 如何排序和获取最小开始日期(XSLT)
问题描述
我想使用 xslt 对 xml 进行排序并获取最小开始日期(删除其他)
这是我的xml
<name>
<name>
<firstName>Huio</firstName>
<lastName>Kuyoshitu</lastName>
<detail>
<action>P</action>
<userId>0902</userId>
<startDate>2019-01-01T00:00:00.000</startDate>
<endDate>2030-12-31T00:00:00.000</endDate>
</detail>
<detail>
<action>P</action>
<userId>0902</userId>
<startDate>1990-01-01T00:00:00.000</startDate>
<endDate>1999-12-31T00:00:00.000</endDate>
</detail>
</name>
</name>
我想获得最小开始日期(1990-01-01T00:00:00.000)的详细信息。
这是预期的 xml
<name>
<name>
<firstName>Huio</firstName>
<lastName>Kuyoshitu</lastName>
<detail>
<action>P</action>
<userId>0902</userId>
<startDate>1990-01-01T00:00:00.000</startDate>
<endDate>1999-12-31T00:00:00.000</endDate>
</detail>
</name>
</name>
我试过这段代码。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:copy>
<xsl:for-each-group select="name/name/detail" group-by="userId">
<xsl:sort select='startDate' order="ascending" />
<xsl:for-each select="current-group()">
<xsl:sort select='startDate' order="ascending" />
<xsl:if test="position()=1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但名字和姓氏标签消失了。
解决方案
XSLT 2 及更高版本min
在序列上使用 XPath 2 功能xs:dateTime
将任务减少到
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="name/name">
<xsl:copy>
<xsl:apply-templates
select="let $min-date := min(detail/startDate/xs:dateTime(.))
return node()[not(self::detail) or self::detail[xs:dateTime(startDate) = $min-date]]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- python - 有条件地删除熊猫中相同的列对
- angular - Ionic 4 使用 setInterval 函数试图每秒调用一个函数,但该函数从未调用
- linux - 在 grep 匹配的模式输出行前面打印文件名
- python - 按钮在 pycharm 中使用 python 和 tkinter 在新窗口中打开主页的重复窗口
- javascript - 对于 click() 方法,for 循环不能正常工作
- awk - 在图案后提取线条
- javascript - 填充同一文档中的 FORM 字段的可点击链接(但具有多个 FORM)
- image-processing - 预测编码如何帮助无损压缩?
- sql - 如何查看在 SQL 中复制的内容
- sql-server - 对选定行进行累积计算的 SQL SELECT