2010-07-19 5 views
5

알파벳을 반복 할 필요가 있습니다. 각 문자에 대해 문자를 인쇄 한 다음 데이터베이스에서 해당 문자로 시작하는 모든 항목을 표시하기 위해 이미 작동하고있는 코드를 배치해야합니다. 현재는 select를 사용하여 내가 표시 한 모든 항목 이름의 첫 글자를 가져옵니다. 그러나 모든 글자를 표시 한 다음 항목이없는 곳에 '표시 할 항목 없음'을 표시해야합니다. 그래서, 나는 더 이상 항목이있는 문자 만 반환하는 select를 사용할 수 없습니다.xsl 알파벳 목록을 반복합니다.

어떻게하면 각 문자를 하드 코딩하지 않고 각 후 26 번씩 내 템플릿을 호출하지 않고도이 작업을 수행 할 수 있습니까?

+2

좋은 질문 (+1). XSLT 1.0 솔루션에 대한 내 대답을 참조하십시오. –

답변

5

이 변환 :

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

<xsl:key name="kItemBy1stLetter" match="item" 
    use="substring(.,1,1)"/> 

<xsl:variable name="vDoc" select="/"/> 

<xsl:variable name="vAlphabet" select= 
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" 
/> 

<my:message>No items found.</my:message> 

<xsl:variable name="vMessage" select="document('')/*/my:message"/> 

<xsl:template match="/"> 
    <xsl:for-each select= 
    "(document('')//node()|document('')//@*|document('')//namespace::*) 
          [ not(position() > 26)] 
    "> 

    <xsl:variable name="vcurLetter" select= 
     "substring($vAlphabet, position(), 1)"/> 
    <xsl:for-each select="$vDoc"> 
     <xsl:variable name="vSearchResult" select= 
     "key('kItemBy1stLetter', $vcurLetter)"/> 

     <xsl:value-of select="concat('&#xA;',$vcurLetter, ': &#xA;')"/> 

     <xsl:copy-of select="$vSearchResult | $vMessage[not($vSearchResult)]/text()"/> 
     </xsl:for-each> 
     <xsl:text>&#xA;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
이 XML 문서에 적용

("데이터베이스"의 역할) :

:

<database> 
<item>Bicycles</item> 
<item>Computers</item> 
<item>Cars</item> 
<item>Forks</item> 
<item>Gellato</item> 
<item>Hypervehicles</item> 
<item>Ichtiosaurs</item> 
<item>Jobs</item> 
<item>Knots</item> 
<item>Lens</item> 
<item>Miracles</item> 
<item>Notes</item> 
</database> 

가 원하는 결과을 생산

A: 
No items found. 

B: 
<item>Bicycles</item> 

C: 
<item>Computers</item> 
<item>Cars</item> 

D: 
No items found. 

E: 
No items found. 

F: 
<item>Forks</item> 

G: 
<item>Gellato</item> 

H: 
<item>Hypervehicles</item> 

I: 
<item>Ichtiosaurs</item> 

J: 
<item>Jobs</item> 

K: 
<item>Knots</item> 

L: 
<item>Lens</item> 

M: 
<item>Miracles</item> 

N: 
<item>Notes</item> 

O: 
No items found. 

P: 
No items found. 

Q: 
No items found. 

R: 
No items found. 

S: 
No items found. 

T: 
No items found. 

U: 
No items found. 

V: 
No items found. 

W: 
No items found. 

X: 
No items found. 

Y: 
No items found. 

Z: 
No items found. 
+1

좋은 답변입니다! 그리고 "it-use-it-it-it-it-it-it-it-it-it-it-it- 그러나 알파벳은 바로 거기에 있습니다. 그래서 재귀를 통해 "각각의 템플릿을 26 번 호출하기"에 아무런 문제가 없다는 것을 지적하는 것이 좋습니다. –