Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt xsl:sort:按数值排序_Xslt_Sorting_Numerical - Fatal编程技术网

Xslt xsl:sort:按数值排序

Xslt xsl:sort:按数值排序,xslt,sorting,numerical,Xslt,Sorting,Numerical,我必须按数字顺序把代码分类。 代码有四个字符和四个数字 比如说, COMP2100 COMP2400 COMP3410 LAWS2202 LAWS2250 当我这么做的时候 它显示上述结果 然而,我希望这是“数字顺序”,即 COMP2100 LAWS2202 COMP2250 COMP2400 COMP3410 如何做到这一点?注意:OP现在提供了示例XML。下面的理论可以简单地适应这种XML 一、XSLT1.0第1部分 下面是一个简单的解决方案,它假设您的断言代码有四个字符和四个数字: …

我必须按数字顺序把代码分类。 代码有四个字符和四个数字

比如说,

COMP2100
COMP2400
COMP3410
LAWS2202
LAWS2250
当我这么做的时候 它显示上述结果

然而,我希望这是“数字顺序”,即

COMP2100
LAWS2202
COMP2250
COMP2400
COMP3410
如何做到这一点?

注意:OP现在提供了示例XML。下面的理论可以简单地适应这种XML

一、XSLT1.0第1部分

下面是一个简单的解决方案,它假设您的断言代码有四个字符和四个数字:

…提供了相同的结果:

<?xml version="1.0" encoding="utf-8"?>
<t>
  <i>COMP2100</i>
  <i>LAWS2202</i>
  <i>LAWS2250</i>
  <i>COMP2400</i>
  <i>COMP3410</i>
</t>
请注意,在XPath 2.0中绝不需要使用正则表达式;双翻译方法的工作原理与XPath1.0中的一样好。但是,replace方法很可能更有效。

提供的XSLT代码中有两个明显的错误:

用于选择元素的命名空间与提供的XML文档的默认命名空间不同。只需更改:xmlns:xsi=file://Volumes/xxxxxxx/Assignment 到xmlns:xsi=file://Volumes/xxxxxxx/Assignment.

目前的排序不是数字。更改:

致:

生成的结果现在按课程代码的数字部分正确排序:


嘿,阿巴克,谢谢你的信息!我已经提供了我的XML和XSL,你能告诉我哪一个在效率方面最有效吗?谢谢大家!@珍妮多-没问题!鉴于您使用的是XSLT1.0,XSLT1.0解决方案1可能是最有效的。但是,除非处理非常大的文档,否则XSLT1.0解决方案1和XSLT1.0解决方案2将非常相似。我的建议是根据你未来的需求来选择;同样,XSLT 1.0解决方案2更为简单。@ABach,您的XSLT 2.0解决方案有明显的错误-请始终通过在代表性数据上实际运行解决方案并确认它们产生正确的结果来验证您的解决方案。此外,XSLT 1.0解决方案不适用于OP提供的特定XML文档结构。类似的情况如何:它是smartform的一部分,因此不能有多个相同的字段值
<?xml version="1.0" encoding="utf-8"?>
<t>
  <i>COMP3410</i>
  <i>LAWS2202</i>
  <i>COMP2400</i>
  <i>COMP2100</i>
  <i>LAWS2250</i>
</t>
<?xml version="1.0" encoding="utf-8"?>
<t>
  <i>COMP2100</i>
  <i>LAWS2202</i>
  <i>LAWS2250</i>
  <i>COMP2400</i>
  <i>COMP3410</i>
</t>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:variable name="vNums" select="'1234567890'" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <t>
      <xsl:apply-templates>
        <xsl:sort select="translate(., translate(., $vNums, ''), '')"
          data-type="number" />
      </xsl:apply-templates>
    </t>
  </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<t>
  <i>COMP2100</i>
  <i>LAWS2202</i>
  <i>LAWS2250</i>
  <i>COMP2400</i>
  <i>COMP3410</i>
</t>
<xsl:sort select="replace(., '[^\d]', '')" data-type="number" />
   <xsl:sort select="substring(xsi:code, 5)" data-type="number" />
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:xsi="file://Volumes/u4783938/Assignment">
<xsl:template match="/">
    <html>
    <head>
        <title> Course Catalogue </title>
    </head>
    <body bgcolor="#FF9999">
        <h1> <div style="text-align:center"> Course Catalogue </div> </h1>
        <xsl:for-each select="xsi:catalogue/xsi:course">
        <xsl:sort select="substring(xsi:code, 5)"
         data-type="number" />
        <div style="width:1000px;margin-bottom:4px;color:white;background-color:#F36;text-align:justify;border:outset;margin-left:auto;margin-right:auto;">
            <xsl:apply-templates select="xsi:code" />
            <br />
            <xsl:apply-templates select="xsi:title" />
            <br />
            <xsl:apply-templates select="xsi:year" />
            <br />
            <xsl:apply-templates select="xsi:science" />
            <br />
            <xsl:apply-templates select="xsi:area" />
            <br />
            <xsl:apply-templates select="xsi:subject" />
            <br />
            <xsl:apply-templates select="xsi:updated" />
            <br />
            <xsl:apply-templates select="xsi:unit" />
            <br />
            <xsl:apply-templates select="xsi:description" />
            <br />
            <xsl:apply-templates select="xsi:outcomes" />
            <br />
            <xsl:apply-templates select="xsi:incompatibility" />
        </div>
        </xsl:for-each>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>
<catalogue xmlns="file://Volumes/u4783938/Assignment"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="file://Volumes/u4443554/Assignment/courses.xsd">
    <course>
        <code>ABCD3410</code>
        <title> Information Technology in Electronic Commerce </title>
        <year>later year</year>
        <science>C</science>
        <area> Research School of Computer Science </area>
        <subject> Computer Science </subject>
        <updated>2012-03-13T13:12:00</updated>
        <unit>6</unit>
        <description>Tce </description>
        <outcomes>Up trCommerce. </outcomes>
        <incompatibility>COMP1100</incompatibility>
    </course>
    <course>
        <code>COMP2011</code>
        <title> Course 2011 </title>
        <year>Year 2011</year>
        <science>C++</science>
        <area> Research School of Computer Science </area>
        <subject> Computer Science </subject>
        <updated>2012-03-13T13:12:00</updated>
        <unit>6</unit>
        <description>Tce </description>
        <outcomes>Up trCommerce. </outcomes>
        <incompatibility>COMP1100</incompatibility>
    </course>
</catalogue>
<html xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="file://Volumes/u4783938/Assignment">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title> Course Catalogue </title>
   </head>
   <body bgcolor="#FF9999">
      <h1>
         <div style="text-align:center"> Course Catalogue </div>
      </h1>
      <div style="width:1000px;margin-bottom:4px;color:white;background-color:#F36;text-align:justify;border:outset;margin-left:auto;margin-right:auto;">COMP2011<br> Course 2011 <br>Year 2011<br>C++<br> Research School of Computer Science <br> Computer Science <br>2012-03-13T13:12:00<br>6<br>Tce <br>Up trCommerce. <br>COMP1100
      </div>
      <div style="width:1000px;margin-bottom:4px;color:white;background-color:#F36;text-align:justify;border:outset;margin-left:auto;margin-right:auto;">ABCD3410<br> Information Technology in Electronic Commerce <br>later year<br>C<br> Research School of Computer Science <br> Computer Science <br>2012-03-13T13:12:00<br>6<br>Tce <br>Up trCommerce. <br>COMP1100
      </div>
   </body>
</html>