Xml,xsl Javascript排序 [英] Xml, xsl Javascript sorting

查看:61
本文介绍了Xml,xsl Javascript排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用javascript对xml数据进行排序的方法,并希望最终过滤出数据。我知道这在xsl文件中是可能的,但我想在客户端做。

I am looking for a way to sort my xml data with javascript, and want to eventually filter out the data as well. I know all this is possible in the xsl file but i would like to do it client side.

我搜索了多个地方用javascript进行排序,但大多数都是特定于xml文件或我无法弄清楚发生了什么。

I have searched multiple places for sorting with javascript but most of it was either too xml file specific or I couldn't figure out what was going on.

非常感谢任何建议

推荐答案

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

The first part of this is performing the transformation in 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)。

This function accepts two params. The first is the xml that you want to transform. The second is the xslt that you want to use to transform the xml. Both params accept either strings that will be transformed to nodes or nodes themselves (such as XHR.responseXML).

拼图的第二部分是排序,你将使用xsl的内置 xsl:sort

The second part of the puzzle is sorting which you will use xsl's built-in xsl:sort.

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

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

All parameters are optional besides the select statement.

示例排序用法:

<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:sort 在 w3schools

这篇关于Xml,xsl Javascript排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆