2013-02-05 1 views
1

XSL에서 어떻게 자연사 정렬을 할 수 있습니까? 나는 다음과 같이 요소의 목록을 가지고 있도록XSL에서 자연스러운 정렬?

<items> 
    <item>A 24</item> 
    <item>B 12</item> 
    <item>B 11</item> 
    <item>C 10</item> 
    <item>A 1</item> 
    <item>B 2</item> 
</item> 

가 어떻게 출력을 정렬 할 수 있습니다 :

예를 들어

은 다음 XML 조각을 주어?

<ul> 
    <li>A 1</li> 
    <li>A 24</li> 
    <li>B 2</li> 
    <li>B 11</li> 
    <li>B 12</li> 
    <li>C 10</li> 
</ul> 

편집 : 나는 예를 들어 임의의 문자열로 작업 할 수있는 솔루션에 특히 관심이 있어요. PHP의 natsort 작동 방식과 비슷한 일반적인 패턴을 따르지 않는 것들.

답변

0

여러 xsl:sort의를 사용할 수 있습니다 샘플 입력에서 실행

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/items"> 
    <ul> 
     <xsl:apply-templates select="item"> 
     <xsl:sort select="substring-before(., ' ')" /> 
     <xsl:sort select="substring-after(., ' ')" data-type="number" /> 
     </xsl:apply-templates> 
    </ul> 
    </xsl:template> 

    <xsl:template match="item"> 
    <li> 
     <xsl:value-of select="."/> 
    </li> 
    </xsl:template> 
</xsl:stylesheet> 

, 이것은 생산 :

<ul> 
    <li>A 1</li> 
    <li>A 24</li> 
    <li>B 2</li> 
    <li>B 11</li> 
    <li>B 12</li> 
    <li>C 10</li> 
</ul> 

더 철저한 (일반) 자연 정렬은 훨씬 더 복잡 접근 방식이 필요합니다. Dimitre Novatchev는 스택 오버플로에 an XSLT 1.0 approach을 제공하고 다른 사이트는 an XSLT 2.0 approach 인 것으로 보입니다. 둘 다 너무 길어 여기서 재생산 할 가치가 없으므로 링크에서 찾아보십시오.

+0

일반적으로 이렇게 할 수 있습니까? 공간에 끈을 쪼개지 않고? 예를 들어 파일 목록이 '200327582_RFTS, 138082008_RFTS, 138012009 RFTS, 10101010, ABCDEF' 인 경우 –

+0

@unpluggd [Here] (http://stackoverflow.com/a/8352114/1945651)는 Dimitre Novatchev의 SO 대답으로 몇 가지 제한 사항이있는 구현으로 보입니다. [This] (http://www.mhonarc.org/archive/html/xsl-list/2011-03/msg00003.html)는 XSLT 2.0 구현 인 것으로 보입니다. – JLRishe