代码之家  ›  专栏  ›  技术社区  ›  fluf

XML、XSL JavaScript排序

  •  3
  • fluf  · 技术社区  · 16 年前

    我正在寻找一种用JavaScript对XML数据进行排序的方法,并希望最终过滤掉这些数据。我知道在XSL文件中所有这些都是可能的,但我想在客户端进行。

    我已经用javascript搜索了多个地方进行排序,但大多数地方要么是XML文件特有的,要么是我不知道发生了什么。

    真的很感谢你的建议

    6 回复  |  直到 13 年前
        1
  •  4
  •   fearphage    13 年前

    第一部分是在javascript中执行转换:

    function transformXML(_xml, _xsl) {
      var
        xml = typeof _xml == 'string' 
          ? new DOMParser().parseFromString(_xml, 'text/xml') 
          : _xml // assume this is a node already
        ,xsl = typeof _xsl == 'string'
          ? new DOMParser().parseFromString(_xsl, 'text/xml')
          : _xsl // assume this is a node already
        ,processor = new XSLTProcessor()
      ;
    
      processor.importStylesheet(xsl);
    
      return processor.transformToDocument(xml.firstChild);
    }
    

    此函数接受两个参数。第一个是要转换的XML。第二个是要用来转换XML的XSLT。这两个参数都接受将转换为节点或节点本身的字符串(如xhr.responsexml)。

    难题的第二部分是排序,您将使用XSL内置的 xsl:sort .

    <xsl:sort
      select="expression"
      lang="language-code"
      data-type="text|number|qname"
      order="ascending|descending"
      case-order="upper-first|lower-first"/>
    

    除select语句外,所有参数都是可选的。

    示例排序用法:

    <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
    
      <xsl:value-of select="artist"/>
      <xsl:text> - </xsl:text>
      <xsl:value-of select="title"/>
    </xsl:for-each>
    

    你可以找到更多关于 排序:XSL w3schools .

        2
  •  2
  •   antony.trupe    16 年前

    我不会在XSL表中排序。 我用 tablesorter plugin jquery .

    这个 Getting Started 这一节非常直截了当(并在下面转载)。

    要使用TableSorter插件,请在HTML文档的标记中包含jQuery库和TableSorter插件:

    <script type="text/javascript" src="/path/to/jquery-latest.js"></script> 
    <script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script> 
    

    TableSorter在标准HTML表上工作。您必须包括AD和T车身标签:

    <table id="myTable"> 
    <thead> 
    <tr> 
        <th>Last Name</th> 
        <th>First Name</th> 
        <th>Email</th> 
        <th>Due</th> 
        <th>Web Site</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
        <td>Smith</td> 
        <td>John</td> 
        <td>jsmith@gmail.com</td> 
        <td>$50.00</td> 
        <td>http://www.jsmith.com</td> 
    </tr> 
    <tr> 
        <td>Bach</td> 
        <td>Frank</td> 
        <td>fbach@yahoo.com</td> 
        <td>$50.00</td> 
        <td>http://www.frank.com</td> 
    </tr> 
    <tr> 
        <td>Doe</td> 
        <td>Jason</td> 
        <td>jdoe@hotmail.com</td> 
        <td>$100.00</td> 
        <td>http://www.jdoe.com</td> 
    </tr> 
    <tr> 
        <td>Conway</td> 
        <td>Tim</td> 
        <td>tconway@earthlink.net</td> 
        <td>$50.00</td> 
        <td>http://www.timconway.com</td> 
    </tr> 
    </tbody> 
    </table> 
    

    首先告诉TableSorter在加载文档时对表进行排序:

    $(document).ready(function() 
        { 
            $("#myTable").tablesorter(); 
        } 
    ); 
    

    单击标题,您将看到您的表现在可以排序了!初始化表时还可以传入配置选项。这将告诉TableSorter按升序对第一列和第二列进行排序。

    $(document).ready(function() 
        { 
            $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} ); 
        } 
    );
    
        3
  •  0
  •   Diego Magalhães    16 年前

    一个非常好的方法是在XSL内部进行带有参数检查的排序,然后在其上应用一些JS,一个完整的工作示例可从以下网站获得:

    http://www.google.com.br/search?hl=pt-BR&q=xml+javascript+data+sort&meta=

        4
  •  0
  •   James Black    16 年前

    为什么不把它转换成一个对象数组并对其进行排序,因为您可能需要转换它来显示它。

    不过,如果必须用JavaScript对XML进行排序,您可以在这里找到一些帮助。 http://www.velocityreviews.com/forums/t170211-clientside-filtering-and-sorting-xml-with-javascript-work-in-iebut-not-firefox.html

        5
  •  0
  •   Chad Cache    16 年前

    所有主要的浏览器版本都支持XSLT(即6+、FF 1+、SF 2+、Chrome、Opera 9+)。

    不确定您在做什么,但XSLT也可以在Ajax中使用。

        6
  •  0
  •   Ether    15 年前

    那么,为什么不使用原型库呢? 它有 http://prototypejs.org/api/enumerable/sortBy 它有 http://prototypejs.org/api/enumerable/grep 那你为什么不把你的元素分类,然后把它们变大呢?如果你想在javascript中使用客户端?