我有一個數據庫查詢,它可以在一次競賽中拉出多個候選項並將它們放入一個XML文件中。我遇到的問題是,有些比賽有超過2名候選人。我的XML數據模板一次只能選擇2個候選人。這是一個選舉代碼。到目前爲止,我已經進行了查詢設置,因此至少需要4個候選人。當然,如果一個種族少於4個候選人,它將省略NULL數據並仍然輸出該比賽的2個候選人。我現在甚至遇到了擁有18名以上候選人的比賽。這變成了一件相當麻煩的事情。這裏是我的查詢:將XML子節點放置在同一元素的多個父節點下
select rc.[race number] AS RaceNumber,
max(case when seqnum = 1 then title1 end) as title1,
max(case when seqnum = 1 then [precinct percent] end) as PrecintPercent,
max(case when seqnum = 1 then [candidate num] end) as Winner,
max(case when seqnum = 1 then Votes end) as WinningVotes,
max(case when seqnum = 1 then party end) as WinningParty,
max(case when seqnum = 1 then leader end) as Winner1,
max(case when seqnum = 1 then CAST(winner AS tinyint) end) as WinnerSelected,
max(case when seqnum = 1 then [leader percent] end) as WinnerPercent,
max(case when seqnum = 2 then [candidate num] end) as Loser,
max(case when seqnum = 2 then Votes end) as LosingVotes,
max(case when seqnum = 2 then party end) as LosingParty,
max(case when seqnum = 2 then leader2 end) as Loser2,
max(case when seqnum = 2 then [leader2 percent] end) as LoserPercent,
max(case when seqnum = 2 then CAST(winner AS tinyint) end) as LoserSelected,
max(case when seqnum = 3 then [candidate num] end) as Winner3,
max(case when seqnum = 3 then Votes end) as Winner3Votes,
max(case when seqnum = 3 then party end) as Winner3Party,
max(case when seqnum = 3 then [first name]+[last name]end) as Winner3,
max(case when seqnum = 3 then CAST(winner AS tinyint) end) as Winner3Selected,
max(case when seqnum = 4 then [candidate num] end) as Loser4,
max(case when seqnum = 4 then Votes end) as Loser4Votes,
max(case when seqnum = 4 then party end) as Loser4Party,
max(case when seqnum = 4 then [first name]+ [last name]end) as Loser4,
max(case when seqnum = 4 then CAST(winner AS tinyint) end) as Loser4Selected
from
(
select
r.title1,
r.[precinct percent],
rc.[race number],
rc.[candidate num],
rc.[Votes],
rc.[winner],
c.[party],
r.[leader],
r.[leader percent],
r.[leader2],
r.[leader2 percent],
c.[first name],
c.[last name],
row_number() over (partition by rc.[race number] order by votes desc) as seqnum
from dbo.[RACE CANDIDATES] rc
inner join dbo.[CANDIDATE] c on rc.[candidate num] = c.[candidate number]
inner join dbo.[RACE] r
on rc.[race number] = r.[race number]
) rc
group by rc.[race number]
FOR XML PATH ('ELECTION'), ROOT('root')
同樣,這種輸出至少4名候選人,如果有4以下是從XML文檔片段:
<root>
<ELECTION>
<RaceNumber>101</RaceNumber>
<title1>President</title1>
<PrecintPercent>100</PrecintPercent>
<Winner>5083</Winner>
<WinningVotes>999877</WinningVotes>
<WinningParty>D</WinningParty>
<Winner1>Barack Obama</Winner1>
<WinnerSelected>1</WinnerSelected>
<WinnerPercent>53</WinnerPercent>
<Loser>5077</Loser>
<LosingVotes>888888</LosingVotes>
<LosingParty>R</LosingParty>
<Loser2>Mitt Romney</Loser2>
<LoserPercent>47</LoserPercent>
<LoserSelected>0</LoserSelected>
</ELECTION>
<ELECTION>
<RaceNumber>102</RaceNumber>
<title1>U.S. Congress Dist. 1</title1>
<PrecintPercent>100</PrecintPercent>
<Winner>5085</Winner>
<WinningVotes>216879</WinningVotes>
<WinningParty>D</WinningParty>
<Winner1>Bruce Braley</Winner1>
<WinnerSelected>1</WinnerSelected>
<WinnerPercent>57</WinnerPercent>
<Loser>5086</Loser>
<LosingVotes>159657</LosingVotes>
<LosingParty>R</LosingParty>
<Loser2>Ben Lange</Loser2>
<LoserPercent>42</LoserPercent>
<LoserSelected>0</LoserSelected>
</ELECTION>
<ELECTION>
<RaceNumber>133</RaceNumber>
<title1>DesMoines County Board of Supervisors</title1>
<PrecintPercent>100</PrecintPercent>
<Winner>5154</Winner>
<WinningVotes>11629</WinningVotes>
<WinningParty>D</WinningParty>
<Winner1>Bob Beck</Winner1>
<WinnerSelected>1</WinnerSelected>
<WinnerPercent>34</WinnerPercent>
<Loser>5155</Loser>
<LosingVotes>11323</LosingVotes>
<LosingParty>D</LosingParty>
<Loser2>Jim Cary</Loser2>
<LoserPercent>33</LoserPercent>
<LoserSelected>1</LoserSelected>
<Winner3>5156</Winner3>
<Winner3Votes>7018</Winner3Votes>
<Winner3Party>R</Winner3Party>
<Winner3>DarwinBunger</Winner3>
<Winner3Selected>0</Winner3Selected>
<Loser4>5157</Loser4>
<Loser4Votes>4415</Loser4Votes>
<Loser4Party>R</Loser4Party>
<Loser4>JamesSeaberg</Loser4>
<Loser4Selected>0</Loser4Selected>
</ELECTION>
如果你注意到,133,有4個候選人。我想要做的是獲得133顯示前2名候選人,然後創建一個與其餘候選人名稱/票等在其中的重複節點。
這裏是XSL文件,我解析我當選XML數據與代碼:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<tickerfeed version="2.4">
<!-- START ELECTION CAROUSEL -->
<playlist type="flipping_carousel" name="ELECTION2" target="carousel">
<defaults>
<outputchannels>
<active>ABC</active>
<active>MYNTV</active>
</outputchannels>
<gui-color>#CCFF99</gui-color>
</defaults>
<xsl:for-each select="root/ELECTION">
<xsl:element name="element">
<template>ELECTION_RESULTS</template>
<xsl:element name="field">
<xsl:attribute name="name">50</xsl:attribute><xsl:value-of select="title1" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">51</xsl:attribute><xsl:value-of select="PrecinctPercent" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">52</xsl:attribute><xsl:value-of select="WinnerSelected" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">53</xsl:attribute><xsl:value-of select="Winner1" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">54</xsl:attribute><xsl:value-of select="WinnerPercent" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">55</xsl:attribute><xsl:value-of select="WinningVotes" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">56</xsl:attribute><xsl:value-of select="LoserSelected" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">57</xsl:attribute><xsl:value-of select="Loser2" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">58</xsl:attribute><xsl:value-of select="LoserPercent" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">59</xsl:attribute><xsl:value-of select="LosingVotes" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">60</xsl:attribute><xsl:value-of select="WinningParty" />
</xsl:element>
<xsl:element name="field">
<xsl:attribute name="name">61</xsl:attribute><xsl:value-of select="LosingParty" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</playlist>
<!-- END ELECTION CAROUSEL -->
我想我的問題是,是否可以實現?
我應該在XSL文件還是SQL查詢中執行操作?
我目前的查詢是我能夠獲得多個canadidates輸出的唯一方法。我只想弄清楚如何複製比賽號碼,尤其是與剩餘候選人的比賽號碼,因爲我的報告模板一次只能顯示2個候選人。
如果有人能告訴我是或否,那將是理想的,但我必須弄清楚這種或那種方式。謝謝大家的幫助。
編輯**
我只是想有超過2名候選人分裂成自己的節點,而不是輸出到一個節點,與所有4名候選人的資料,如種族133場比賽,我想具有2個-RACE 133節點,第一個具有第一組候選者,第二個節點具有第二組候選者。假設比賽有4名候選人。我不知道......我已經到了一個我覺得這是不可能的地步。
我真的想弄清楚,如果我應該更多地關注查詢或嘗試使用更復雜的XSL文件來獲得所需的輸出。把這根頭髮拉出來。
建議:把它歸結爲最簡單的條款,去掉所有不必要的額外東西來展示問題。也就是說,用一組裸露的骨骼樣本數據,sql,xslt和所需的輸出替換您的真實片段。在這一點上,我們會更容易幫助。 –
@KarlKieninger我會盡力做到這一點。我的SQL返回298行數據。 – user3242661
我不知道如何更好地提出問題。我想我應該告訴自己,「對不起的人」。 – user3242661