如何在对值进行排序之前对其进行处理? [英] How to process on values before sorting them?
问题描述
我之前发布了这个问题 在这个门户网站上,我得到了一个令人满意的解决方案.现在我的问题已经发展了一点.如果链接问题中的 XML 已更新,以便我的数字具有与它们关联的单位,例如 cm
、m
、in
、px
等,这样新的 XML 是:
I previously posted this question on this portal for which I got a satisfactory solution. Now my problem has evolved a bit. What if my XML in the linked question is updated so that my numbers have a unit associated with them like cm
, m
, in
, px
etc. So that the new XML is:
<root>
<a>
<b>12cm</b>
<e>hello</e>
</a>
<a>
<b>11m</b>
<e>how</e>
</a>
<a>
<c>13m</c>
<f>are</f>
</a>
<a>
<b>21cm</b>
<f>you</f>
</a>
<a>
<d>22cm</d>
<e>hello</e>
</a>
<a>
<c>14m</c>
<f>hi</f>
</a>
现在我首先需要将这些数字转换为类似的单位(让我们以 cm
为例),然后才能对它们进行排序并找到最大数字.我该怎么做?
Now I would first need to transform theses numbers in a similar unit (lets say in cm
) before I can sort them and find the maximum number. How can I do that?
我在之前的问题中使用了以下 XSL,但现在我需要更新它,以便我能够在对数字进行排序之前对其进行转换.
I used the following XSL for my previous problem but now I need to update it so that I am able to transform my numbers before sorting them.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="root">
<xsl:for-each select="a/b | a/c | a/d">
<xsl:sort select="." order="descending"/> <!-- find the unit and transform it in centimeters before sorting -->
<xsl:if test="position() = 1">
<highest><xsl:copy-of select="."/></highest>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
注意:我使用的是 XSLT 1.0
Note: I am using XSLT 1.0
推荐答案
我在此处改编了 Dimitre Novatchev 的答案 (xslt 排序输出 xml).
I have adapted the answer of Dimitre Novatchev in here (xslt sort output xml).
如果您将以下样式表应用于您的输入:
If you apply the following stylesheet to your input:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="letters" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:template match="/">
<xsl:variable name="unsorted">
<root>
<xsl:for-each select="root/a/*">
<xsl:choose>
<xsl:when test="string(number(translate(., $letters, '')))!='NaN' and translate(current(), '0123456789', '') = 'cm'">
<xsl:element name="{local-name()}">
<xsl:attribute name="oldvalue"><xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="number(translate(., $letters, ''))"/>
</xsl:element>
</xsl:when>
<xsl:when test="string(number(translate(., $letters, '')))!='NaN' and translate(current(), '0123456789', '') = 'm'">
<xsl:element name="{local-name()}">
<xsl:attribute name="oldvalue"><xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="number(translate(., $letters, '')*100)"/>
</xsl:element>
</xsl:when>
<xsl:when test="string(number(translate(., $letters, '')))!='NaN' and translate(current(), '0123456789', '') = 'in'">
<xsl:element name="{local-name()}">
<xsl:attribute name="oldvalue"><xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="number(translate(., $letters, '')*2.54)"/>
</xsl:element>
</xsl:when>
<xsl:when test="string(number(translate(., $letters, '')))!='NaN' and translate(current(), '0123456789', '') = 'px'">
<xsl:attribute name="oldvalue"><xsl:value-of select="."/></xsl:attribute>
<xsl:element name="{local-name()}">
<xsl:value-of select="number(translate(., $letters, '') div 37.795275591)"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</root>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($unsorted)"/>
<xsl:apply-templates select="$vPass1/*"/>
</xsl:template>
<xsl:template match="root">
<root>
<xsl:for-each select="*">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<highest><xsl:copy-of select="."/></highest>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
它输出:
<root>
<highest>
<c oldvalue="14m">1400</c>
</highest>
</root>
这篇关于如何在对值进行排序之前对其进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!