2009-05-11 2 views
3

JavaScript로 내 xml 데이터를 정렬하는 방법을 찾고 있습니다. 결국 데이터를 필터링하려고합니다. 나는 이것이 xsl 파일에서 가능하다는 것을 알고 있지만 클라이언트 쪽에서 그것을하고 싶습니다.Xml, xsl Javascript sorting

자바 스크립트로 정렬하기 위해 여러 위치를 검색했지만 그 중 대부분도 특정 xml 파일이거나 내가 무슨 일이 있었는지 파악할 수 없었습니다.

정말 어떤 조언을 주셔서 감사합니다겠습니까

아주 좋은 approuch 정렬에 그 안에 parameer 검사와 XSL을 한 다음에 몇 JS를 적용하는 것입니다
+0

XML을 무엇에 정렬합니까? XML은 어떻게 생겼습니까? – Nosredna

답변

4

에 이용 될 수있다. 첫 번째는 변환하려는 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> 

당신은 자세한 내용을 w3schoolsxsl:sort을 찾을 수 있습니다.

0

XSLT는 참고로 모든 주요 브라우저 버전에서 지원됩니다^_은^(IE 6+, FF 1+, SF 2+, 크롬, 오페라 9 +) 당신이 뭘

확실하지하지만 XSLT도 수

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); 
} 

이 함수는 두 PARAMS을 받아이 중 첫 번째 부분은 자바 스크립트에 변환을 수행 AJAX

2

xsl 시트를 정렬하지 않습니다. 나는 tablesorterplugin ~ jquery을 사용합니다.

Getting Started 섹션은 매우 간단합니다 (아래에서 재현됩니다).표준 HTML 테이블에서 작동 tablesorter에

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

:

이 tablesorter에 플러그인을 사용하려면, jQuery 라이브러리와 HTML 문서의 태그 내부의 tablesorter에 플러그인을 포함한다. 문서가로드되는 테이블 정렬 tablesorter에 말에 의해

<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>[email protected]</td> 
    <td>$50.00</td> 
    <td>http://www.jsmith.com</td> 
</tr> 
<tr> 
    <td>Bach</td> 
    <td>Frank</td> 
    <td>[email protected]</td> 
    <td>$50.00</td> 
    <td>http://www.frank.com</td> 
</tr> 
<tr> 
    <td>Doe</td> 
    <td>Jason</td> 
    <td>[email protected]</td> 
    <td>$100.00</td> 
    <td>http://www.jdoe.com</td> 
</tr> 
<tr> 
    <td>Conway</td> 
    <td>Tim</td> 
    <td>[email protected]</td> 
    <td>$50.00</td> 
    <td>http://www.timconway.com</td> 
</tr> 
</tbody> 
</table> 

시작 : 당신은 THEAD 및 TBODY 태그를 포함해야 헤더에

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

을 클릭하면 테이블 지금 것을 볼 수 있습니다 정렬 가능! 테이블을 초기화 할 때 구성 옵션을 전달할 수도 있습니다. 이것은 tablesorter에게 첫 번째와 두 번째 열을 오름차순으로 정렬하도록 지시합니다.

$(document).ready(function() 
    { 
     $("#myTable").tablesorter({sortList: [[0,0], [1,0]]}); 
    } 
);