给定XML,其中FORMNUMBER只能是数字;从数字开始,然后包括alpha;或遵循数字字母数字模式:
<FORMSLIST>
<FORMS>
<FORMNUMBER>3 ABC</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 ABC 45</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 XYZ</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>18</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>326</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>21 B</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 EFG 79</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 ABCD</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
</FORMSLIST>
这是我的样式表(Alphanumeric sort on mixed string value revisited上的变体):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vAlpha" select="' ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each-group select="FORMSLIST/FORMS" group-by="FORMSEQ">
<xsl:sort select="FORMSEQ" data-type="number"/>
<!-- NEED HELP WITH THE FOLLOWING SORT -->
<xsl:for-each select="current-group()[(floor(number(FORMNUMBER)) = floor(number(FORMNUMBER))) or (substring-before(FORMNUMBER,' ') and translate(substring-before(FORMNUMBER,' '),' 0123456789','') = '')]">
<xsl:sort select="translate(FORMNUMBER, $vAlpha, '')" data-type="number"/>
<xsl:sort select="substring-after(FORMNUMBER,' ')"/>
<xsl:element name="{local-name()}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:for-each>
<!-- THIS SORT WORKS AND I DON'T HAVE QUESTIONS ON THIS: ALPHA ONLY FORMS; BEGIN WITH ALPHA AND THEN GO TO NUMERIC...MAY ALSO GO BACK TO ALPHA -->
<xsl:for-each select="current-group()[contains($vAlpha,substring(FORMNUMBER,1,1)) or (translate(FORMNUMBER, $vAlpha, '') = '')]">
<xsl:sort select="translate(FORMNUMBER, ' 0123456789', '')"/>
<xsl:sort select="translate(FORMNUMBER, $vAlpha, '')" data-type="number"/>
<xsl:element name="{local-name()}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
结果转换为:
<FORMSLIST>
<FORMS>
<FORMNUMBER>3 ABC</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 ABCD</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 XYZ</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>18</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>21 B</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>326</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 ABC 45</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 EFG 79</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
</FORMSLIST>
我想要的结果是将'12 ABC 45'和'12 EFG 79'表格包含在这样的排序中(列表首先按开头编号排序):
<FORMSLIST>
<FORMS>
<FORMNUMBER>3 ABC</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 ABCD</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>3 XYZ</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 ABC 45</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>12 EFG 79</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>18</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>21 B</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
<FORMS>
<FORMNUMBER>326</FORMNUMBER>
<FORMSEQ>99900</FORMSEQ>
</FORMS>
</FORMSLIST>
我尝试了很多变化,但似乎无法让它发挥作用。
答案 0 :(得分:0)
在XSLT 2.0中,tokenize(FORMNUMBER,'')返回由FORMNUMBER中的空格分隔的标记序列。因此,您可以使用这些<xsl:sort>
元素:
<xsl:sort select="tokenize(FORMNUMBER, ' ')[1]" data-type="number"/>
<xsl:sort select="tokenize(FORMNUMBER, ' ')[2]" />
<xsl:sort select="tokenize(FORMNUMBER, ' ')[3]" data-type="number"/>
未测试。但看起来它会完全符合您的要求。