混合字符串值的字母数字排序
给定的 XML 片段:
Given XML snippet of:
<forms>
<FORM lob="BO" form_name="AI OM 10"/>
<FORM lob="BO" form_name="CL BP 03 01"/>
<FORM lob="BO" form_name="AI OM 107"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="123 DDE"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="AI OM 98"/>
</forms>
我需要按 form_name 字母顺序对 FORM 节点进行排序,以便在 form_name 中包含AI OM"的所有表单组合在一起,然后在其中按整数按数字顺序排列(其他表单相同).
I need to sort the FORM nodes by form_name alphabetically so all the forms containing 'AI OM' in the form_name are grouped together and then within that they are in numeric order by the integers (same for other forms).
form_name 可以是开放季节,因为字母和数字可以按任何顺序排列:
The form_name can be is open season as letters and numbers can be in any order:
XX ## ##
XX XX ##
XX XX ###
XX XX ## ##
XX###
XXXXX
'##XXX
XXX###
XX ## ##
XX XX ##
XX XX ###
XX XX ## ##
XX ###
XX XXXX
'## XXX
XXX###
我认为需要发生的是字符串需要在字母和数字之间拆分.我想数字部分可能会在删除任何空格的情况下进行排序.
What I THINK needs to happen is that string needs to be split between alpha and numeric. The numeric part could probably be sorted with any spaces removed I suppose.
鉴于form_name"格式没有规则,我不知道如何拆分字符串然后涵盖所有排序/分组组合.
I am at a loss as to how to split the string and then cover all the sorting/grouping combinations given that there are no rules around the 'form_name' format.
我们使用的是 XSLT 2.0.谢谢.
We are using XSLT 2.0. Thanks.
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vDigits" select="'0123456789 '"/>
<xsl:variable name="vAlpha" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ '"/>
<xsl:template match="/*">
<forms>
<xsl:for-each select="FORM">
<xsl:sort select="translate(@form_name,$vDigits,'')"/>
<xsl:sort select="translate(@form_name,$vAlpha,'')"
data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</forms>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<forms>
<FORM lob="BO" form_name="AI OM 10"/>
<FORM lob="BO" form_name="CL BP 03 01"/>
<FORM lob="BO" form_name="AI OM 107"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="123 DDE"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="AI OM 98"/>
</forms>
产生想要的、正确的结果:
<forms>
<FORM lob="BO" form_name="AI OM 10"/>
<FORM lob="BO" form_name="AI OM 98"/>
<FORM lob="BO" form_name="AI OM 107"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="CL BP 00 02"/>
<FORM lob="BO" form_name="CL BP 03 01"/>
<FORM lob="BO" form_name="123 DDE"/>
</forms>
请注意:
两条
指令实现了两阶段排序
XPath translate()
函数用于生成仅字母排序键或仅数字排序键.
The XPath translate()
function is used to produce either the alpha-only sort-key or the digits-only sort-key.