Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将XML子节点放在同一元素的多个父节点下_Sql Server_Xml_Xslt - Fatal编程技术网

Sql server 将XML子节点放在同一元素的多个父节点下

Sql server 将XML子节点放在同一元素的多个父节点下,sql-server,xml,xslt,Sql Server,Xml,Xslt,我有一个数据库查询,它在一场比赛中抽取多个候选人,并将他们放入一个XML文件中。我遇到的问题是,有些比赛有两个以上的候选人。我的XML数据模板一次只能接受两个候选对象。这是一个选举记录。到目前为止,我已经有了查询设置,所以它至少可以吸引4名候选人。当然,如果一个比赛少于4名候选人,它会忽略空数据,并仍然输出该比赛的2名候选人。我现在甚至参加了有18名以上候选人参加的比赛。这正变成一件很烦人的事。我的问题是: select rc.[race number] AS RaceNumber,

我有一个数据库查询,它在一场比赛中抽取多个候选人,并将他们放入一个XML文件中。我遇到的问题是,有些比赛有两个以上的候选人。我的XML数据模板一次只能接受两个候选对象。这是一个选举记录。到目前为止,我已经有了查询设置,所以它至少可以吸引4名候选人。当然,如果一个比赛少于4名候选人,它会忽略空数据,并仍然输出该比赛的2名候选人。我现在甚至参加了有18名以上候选人参加的比赛。这正变成一件很烦人的事。我的问题是:

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>

101
总统
100
5083
999877
D
巴拉克·奥巴马
1.
53
5077
888888
R
罗姆尼
47
0
102
美国国会区1
100
5085
216879
D
议员布拉利
1.
57
5086
159657
R
本兰格
42
0
133
得梅因县监事会
100
5154
11629
D
鲍勃·贝克
1.
34
5155
11323
D
吉姆·卡里
33
1.
5156
7018
R
达尔文邦格
0
5157
4415
R
贾梅塞伯格
0
如果你注意到,133有4个候选人。我想做的是让133显示前两个候选对象,然后创建一个重复节点,其中包含其余候选对象的名称/投票等

下面是我正在使用以下XSL文件将我的选举XML数据解析到ticker中:

<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 -->

基础知识
MYNTV
#CCFF99
选举结果
50
51
52
53
54
55
56
57
58
59
60
61
我想我的问题是,这能实现吗

我应该在XSL文件或SQL查询中执行该操作吗

我当前的查询是我能够将多个Canadidate输出的唯一方法。我只想弄清楚如何复制比赛号码,特别是剩余候选人的比赛名称,因为我的股票代码模板一次只能显示2名候选人

如果有人能告诉我是或不是,那将是理想的,但我必须以这样或那样的方式弄清楚。谢谢大家的帮助

编辑**

我只希望有2个以上候选的比赛分成自己的节点,而不是输出到一个节点,所有4个候选信息,如比赛133,我希望有2个比赛133节点,第一个有第一组候选,第二个有第二组候选。假设比赛有4名候选人。我不知道…我已经到了一个地步,我觉得这是不可能的


我真的在试图弄清楚我是应该更多地关注查询,还是应该尝试使用更复杂的XSL文件获得所需的输出。我在评论中的意思是,如果您不使用实际数据,而是创建了一个包含最少行和几个字段的样本集,那么使用该样本集将更容易。完全远离候选人和选举,只需使用一些显示相同场景的虚拟数据即可

我会尽力提供一些建议,因为我相信我理解你的要求

我想我的问题是,这能实现吗

是的,我想可以

我应该在XSL文件或SQL查询中执行该操作吗

我认为这两种方法都可以,但今晚我的XSL不够强大,无法想出方法

因此,关注SQL,我认为可以将查询拆分为两部分,然后在对XML执行查询之前合并结果。这样,在使用XML之前,四元组将被分成两对。第一部分得到seqnums 1和2,第二部分得到seqnums 3和4

您可以借用这里的技术来获得seqnum对


顺便说一句,MSSQL的哪个版本?

建议:将其归结为最简单的术语,去掉所有不需要证明问题的额外内容。也就是说,用一组简单的示例数据、sql、xslt和所需的输出替换真正的代码片段。在这一点上,我们会更容易提供帮助。@Karlkienger我会尽力做到这一点。我的SQL返回298行数据。我不知道如何更好地提问。我想我应该告诉自己,“对不起,伙计”。我同意这样的建议,即在一条战线(SQL或XSLT)上尽可能地与之抗争。如果您决定使用XSLT(全部或部分),请不要对元素进行编号,例如,
,等等。XSLT足够聪明,可以计算相同名称的元素,如果需要,只从中选择前N个,或将它们分组为大小相等的组。顺便说一句,在您的上一场比赛(133)中您已将获得11323票的候选人指定为
,而获得7018票的下一位候选人为
。这看起来不对。MSSQL 2008。这是个好主意。我将尝试在SQL中划分它,看看能得到什么。我问了另一个问题,试图用更少的信息来总结它,但我将尝试在使用XML之前分割它们,就像您建议的那样。谢谢你的信任。我仍在探索用VB post输出文件来修改XML。犯错误我可以把事情弄得尽可能复杂。哈再次感谢,我会让你知道我的想法。我首先需要弄清楚如何正确分割查询。谢谢