2011-09-05 43 views
1

我試圖直接從SQL查詢(TSQL,SQL Server 2008 R2)的HTML出來。 我有一個字段需要解析每天的日程安排,其中一個字段爲 ul列表,主題標題只列出一次,而不是每個主題。我不允許改變XML數據的形式。Xquery裏面爲

我該如何改變這個select子句來完成它?

select Day1Schedule = T.Item.query 
           (' 
            for $a in //Schedule/Day[@Number="1"]/Topic 
            for $b in $a/Subject 
               return 
             <ul> 
             <li> 
             <strong> 
             {$a/Header/text()} 
             </strong> 
             </li> 
             <li> 
              {$b/text()} 
             </li> 
            </ul> 
           ') 


<Schedule> 
<Day Number="1"> 
    <Topic> 
     <Header>Topic1</Header> 
     <Subject>T1Subject1</Subject> 
     <Subject>T1Subject2</Subject> 
     <Subject>T1Subject3</Subject> 
     <Subject>T1Subject4</Subject> 
     <Subject>T1Subject5</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic2</Header> 
     <Subject>T2Subject1</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic3</Header> 
     <Subject>T3Subject1</Subject> 
     <Subject>T3Subject2</Subject> 
     <Subject>T3Subject3</Subject> 
    </Topic> 
</Day> 
<Day Number="2"> 
    <Topic> 
     <Header>Topic1</Header> 
     <Subject>T1Subject1</Subject> 
     <Subject>T1Subject2</Subject> 
     <Subject>T1Subject3</Subject> 
     <Subject>T1Subject4</Subject> 
     <Subject>T1Subject5</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic2</Header> 
     <Subject>T2Subject1</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic3</Header> 
     <Subject>T3Subject1</Subject> 
     <Subject>T3Subject2</Subject> 
     <Subject>T3Subject3</Subject> 
    </Topic> 
</Day> 
<Day Number="3"> 
    <Topic> 
     <Header>Topic1</Header> 
     <Subject>T1Subject1</Subject> 
     <Subject>T1Subject2</Subject> 
     <Subject>T1Subject3</Subject> 
     <Subject>T1Subject4</Subject> 
     <Subject>T1Subject5</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic2</Header> 
     <Subject>T2Subject1</Subject> 
    </Topic> 
    <Topic> 
     <Header>Topic3</Header> 
     <Subject>T3Subject1</Subject> 
     <Subject>T3Subject2</Subject> 
     <Subject>T3Subject3</Subject> 
    </Topic> 
</Day> 
</Schedule> 

感謝您的任何想法?

回答

1

嘗試這樣:

for $a in //Schedule/Day[@Number="1"]/Topic 
return <ul> 
      <li><strong>{$a/Header/text()}</strong></li> 
      {for $b in $a/Subject 
      return <li>{$b/text()}</li>} 
     </ul> 

它每個主題返回頭一次。

+0

這就是它,我知道它可以做到,但還不完全理解語法。感謝您的快速回答 – vipasane