xslt-1.0 - XSLT 对变量中的数据进行排序并将它们保存到另一个变量
问题描述
我有个问题。我有以下 XML
<countryList>
<country>
<name>Afghanistan</name>
<population>29117000</population>
<area>654329</area>
</country>
<country>
<name>Albania</name>
<population>3195000</population>
<area>28748</area>
</country>
<country>
<name>Algeria</name>
<population>35423000</population>
<area>2381741</area>
</country>
<country>
<name>Andorra</name>
<population>84082</population>
<area>468</area>
</country>
</countryList>
我有一个问题。我需要做的就是划分人口/区域并对每个国家的这些划分进行排序。但是,我试过这个
<xsl:variable name="Podiel">
<xsl:value-of select="../population div ../area"/>
</xsl:variable>
<xsl:variable name="PodielPodiel">
<xsl:for-each select="$Podiel">
<xsl:sort select="." data-type="number" order="descending"/>
</xsl:for-each>
</xsl:variable>
但我仍然收到错误
The 'select' expression does not evaluate to a node set.
no result for data1.xml
有什么帮助吗?我只想知道所有部门的最大值。谢谢。
解决方案
不确定问题是否已解决,但是要node-set
在 XSLT 1.0 中使用,必须使用扩展函数。有关它在 XSLT 1.0 中的使用的更多详细信息,请参阅节点集扩展。
如果country
输入 XML 中的列表需要根据 的值进行排序,则可以在 中使用population div area
该函数。不需要声明变量并计算附加值然后访问变量。number()
<xsl:sort>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<!-- identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="countryList">
<xsl:copy>
<xsl:apply-templates select="country">
<xsl:sort select="number(population div area)" data-type="number" order="descending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<countryList>
<country>
<name>Andorra</name>
<population>84082</population>
<area>468</area>
</country>
<country>
<name>Albania</name>
<population>3195000</population>
<area>28748</area>
</country>
<country>
<name>Afghanistan</name>
<population>29117000</population>
<area>654329</area>
</country>
<country>
<name>Algeria</name>
<population>35423000</population>
<area>2381741</area>
</country>
</countryList>
推荐阅读
- node.js - 在 NodeJs 中上传和读取 excel 文件
- python - 按python中的另一个list.index对压缩列表进行排序
- javascript - 从 Firebase 获取用户个人资料信息时出现问题
- r - 拆分和重组数据帧时如何保留单个观察的行名?
- javascript - Angular - 无法获取父组件数据
- android - Nativescript,无法在设备上进行更改。错误是:无法读取 null 的属性“startsWith”
- javascript - 在与按钮相同的 div 块中获取跨度的内部文本
- reactjs - 如何在单个反应 js 组件中导出两个模块?
- c - C中的哈希宏定义
- python - 从 excel 创建 Python 字典