2

注意:我已经解决了这个问题的大部分,但遇到了障碍。请读到底部。你会看到我在哪里添加了一个(注意)部分。TIA。


我有一个相当广泛的连接查询,我想转储到 XML。我几乎可以正常工作,但是我在这里的某个地方缺少一个概念。我的查询(缩写)如下所示:

SELECT  Campaign.CampaignId "Campaign/ID"
      , Campaign.CompanyId "Campaign/CompanyID"
      , Campaign.CampaignName "Campaign/Name"
...
      , Audio.AudioID  "Campaign/Audio/ID"
      , Audio.[Name]  "Campaign/Audio/Name"
...
      , Video.CampaignVideosAudioMute  "Campaign/Video/Audio/Mute"
      , Video.CampaignVideosAudioVolume  "Campaign/Video/Audio/Volume"
      , Video.CampaignVideosPositionX  "Campaign/Video/Position/X"
...
      , Characters.CharacterID "Campaign/Characters/Character/ID"
      , Characters.[Name] "Campaign/Characters/Character/Name"
...
      , Element.ElementID "Campaign/Elements/Element/ID"
      , Element.Editable "Campaign/Elements/Element/Editable"
...
      , [Image].ImageID  "Campaign/Elements/Element/Image/ID"
      , [Image].[Path]  "Campaign/Elements/Element/Image/Path"
...
      , [Text].TextID  "Campaign/Elements/Element/Text/ID"
      , [Text].Value  "Campaign/Elements/Element/Text/Value"
FROM    vwCampaign Campaign
LEFT JOIN dbo.vwCampaignAudio Audio ON Campaign.CampaignId = Audio.CampaignId
LEFT JOIN dbo.vwCampaignCharacters Characters ON Campaign.CampaignId = Characters.CampaignId
LEFT JOIN dbo.vwCampaignVideo Video ON Campaign.CampaignId = Video.CampaignId
LEFT JOIN dbo.vwCampaignElements Element ON Campaign.CampaignId = Element.CampaignId
LEFT JOIN dbo.vwCampaignElementImage [Image] ON Element.CampaignId = [Image].CampaignId AND Element.ElementID = [Image].ElementID
LEFT JOIN dbo.vwCampaignElementText [Text] ON Element.CampaignId = [Text].CampaignId AND Element.ElementID = [Text].ElementID
WHERE   Campaign.CampaignId = 10370
FOR     XML PATH, ELEMENTS XSINIL

数据的工作方式我有:

  • 1 个广告系列行
  • 1 个音频行 - 与活动行相关
  • 1 个视频行 - 与广告系列行相关
  • 1-n 个字符行 - 与活动行相关
  • 1-n 个元素行 - 与活动行相关
  • 0 或 1 个图像行 - 与每个元素行相关
  • 0 或 1 个文本行 - 与每个元素行相关

XML 的输出如下:

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Campaign>
    <ID>10370</ID>
    <CompanyID>C2811EA3-361A-411A-BB4C-816A5D6C12DB</CompanyID>
    <Name>Keith01</Name>
    <URL>http://kab.rivworks.com/tests/kab02.htm</URL>
    <Module>Coupon</Module>
    <StartDate>2009-06-29T12:05:00</StartDate>
    <EndDate>2021-06-30T18:00:00</EndDate>
    <Notes>Test #1</Notes>
    <Meta>D7E7D735-8D64-4127-84B1-7D72FB5EDD17</Meta>
    <Orientation>Half-Body Left</Orientation>
    <PresentationPlayerFlashVars>config=http://cdn1.deal4it.com/rivworks/demos/skymall/skymall-coupon-3.xml</PresentationPlayerFlashVars>
    <Player>
      <CookieIdentity>honirjymcvzk</CookieIdentity>
      <Stage>
        <Top></Top>
        <Left></Left>
        <Height>423</Height>
        <Width>500</Width>
        <MarginLeft></MarginLeft>
        <Container>
          <Background>
            <Color>0xffffff</Color>
            <Image></Image>
          </Background>
        </Container>
      </Stage>
    </Player>
    <Audio>
      <ID xsi:nil="true" />
      <Name xsi:nil="true" />
      <Path xsi:nil="true" />
      <Meta xsi:nil="true" />
      <Genre xsi:nil="true" />
    </Audio>
    <Video>
      <ControlbarEvent>visible</ControlbarEvent>
      <Event>play</Event>
      <PresentationSkin>https://widgets.rivworks.com/player/latest/rivplayer.swf</PresentationSkin>
      <Audio>
        <Mute>False</Mute>
        <Volume>100</Volume>
      </Audio>
      <Position>
        <X>19</X>
        <Y>140</Y>
      </Position>
      <About>
        <Text>RIV Works</Text>
        <Url>http://www.deal4it.com</Url>
      </About>
      <Size>
        <Height>266</Height>
        <Width>400</Width>
      </Size>
      <Settings>
        <Autostart>True</Autostart>
        <Buffer>1</Buffer>
        <DelayPlay>0</DelayPlay>
        <Item>0</Item>
        <Quality>True</Quality>
        <Repeat>none</Repeat>
      </Settings>
    </Video>
    <Character>
      <ID>19029FFC-C1C0-4134-B813-93A9FF17C7F6</ID>
      <Name>Jenna</Name>
      <Actor>CD5AF2B6-C39A-4316-BFB0-D4450194EC80</Actor>
      <Meta>10041662-305F-4493-ACB3-460D687306A4</Meta>
      <Access>Public</Access>
      <Configuration>Individual</Configuration>
      <ImageThumbnail>http://cdn1.deal4it.com/rivworks/images/headshots/jenna.jpg</ImageThumbnail>
      <isPublic>1</isPublic>
      <Demographics>
        <Age>31 - 40</Age>
        <Ethnicity>Caucasian</Ethnicity>
        <Gender>Female</Gender>
      </Demographics>
    </Character>
    <Elements>
      <Element>
        <ID>D9B2A643-73EC-4D55-BA34-D643113CEDEA</ID>
        <Editable>1</Editable>
        <Meta>D5F6175C-8DC7-4F18-9A5F-E2021579498B</Meta>
        <Position>
          <Level>2</Level>
          <X>464</X>
          <Y>21</Y>
        </Position>
        <Image>
          <ID>90FF7F5A-75EC-4FB5-81B1-B9BEC4E8A22A</ID>
          <Path>http://developer.rivworks.com/images/a5b19fe8-c8d3-4588-9eac-7cdf39b52078.jpg</Path>
          <Link></Link>
          <Target></Target>
          <Meta>97261982-2131-41F7-9E2C-ADB10E31ED20</Meta>
          <Size>
            <Height>16</Height>
            <Width>16</Width>
          </Size>
        </Image>
        <Text>
          <ID xsi:nil="true" />
          <Value xsi:nil="true" />
          <Link xsi:nil="true" />
          <Target xsi:nil="true" />
          <Meta xsi:nil="true" />
          <FontColor xsi:nil="true" />
          <FontFamily xsi:nil="true" />
          <FontSize xsi:nil="true" />
        </Text>
      </Element>
    </Elements>
  </Campaign>
</row>

不幸的是,我对此有一些问题。

  1. 根元素仍处于行级别。根元素应该是
  2. 如果我有 3 个字符和 3 个元素,我最终会得到9 个根元素。从一个根元素到下一个根元素的唯一变化是显示的是哪个字符和哪个元素。(并将其与具有 0 或 1 个文本和/或图像的每个元素复合)

XML 应该看起来像:

<campaign>
  <ID>10370</ID>
  <CompanyID>C2811EA3-361A-411A-BB4C-816A5D6C12DB</CompanyID>
  <etc>...</etc>
  <Characters>
    <Character>
      <data>...</data>
    <Character>
    <Character>
      <data>...</data>
    <Character>
  </Characters>
  <Elements>
    <Element>
      <data>...</data>
      <Image>...</Image>
      <Text>...</Text>
    <Element>
    <Element>
      <data>...</data>
      <Image>...</Image>
      <Text>...</Text>
    <Element>
  </Elements>
</campaign>

我需要改变什么?我是否需要查看创建 XML 的不同方法,也许是某种嵌套子句?


注意:在玩了很多谷歌搜索/搜索之后,我改变了我的查询,所以它使用嵌套查询。这是它现在的样子:

    SELECT  Campaign.CampaignId     "Campaign/ID"
          , Campaign.CompanyId      "Campaign/CompanyID"
          , Campaign.CampaignName   "Campaign/Name"
...
          , Audio.AudioID   "Campaign/Audio/ID"
          , Audio.[Name]    "Campaign/Audio/Name"
...
          , Video.CampaignVideosControlbarEvent "Campaign/Video/ControlbarEvent"
          , Video.CampaignVideosEvent       "Campaign/Video/Event"
          , (SELECT cc.CharacterID  "Character/ID"
          , cc.[Name]       "Character/Name"
               FROM dbo.vwCampaignCharacters cc
              WHERE cc.CampaignID = Campaign.CampaignId
                FOR XML PATH ('')
            ) AS "Campaign/Characters"
          , (SELECT ce.ElementID        "Element/ID"
                  , ce.Editable         "Element/Editable"
                  , ce.Meta             "Element/Meta"
                  , ce.PositionLevel    "Element/Position/Level"
                  , ce.PositionX        "Element/Position/X"
                  , ce.PositionY        "Element/Position/Y"
                  , (SELECT cei.ImageID         "Image/ID"
                          , cei.[Path]          "Image/Path"
                          , cei.Link            "Image/Link"
                          , cei.Target          "Image/Target"
                          , cei.Meta            "Image/Meta"
                          , cei.SizeHeight      "Image/Size/Height"
                          , cei.SizeWidth       "Image/Size/Width"
                       FROM dbo.vwCampaignElementImage cei
                      WHERE cei.CampaignID = ce.CampaignId
                        AND cei.ElementID = ce.ElementID
                        FOR XML PATH ('')
                    ) AS "Element"
                  , (SELECT cet.TextID          "ID"
                          , cet.Value           "Value"
                          , cet.Link            "Link"
                          , cet.Target          "Target"
                          , cet.Meta            "Meta"
                          , cet.FontColor       "FontColor"
                          , cet.FontFamily      "FontFamily"
                          , cet.FontSize        "FontSize"
                       FROM dbo.vwCampaignElementText cet
                      WHERE cet.CampaignID = ce.CampaignId
                        AND cet.ElementID = ce.ElementID
                        FOR XML PATH ('Text')
                    ) AS "Element"
               FROM dbo.vwCampaignElements ce
              WHERE ce.CampaignID = Campaign.CampaignId
                FOR XML PATH ('Element')
            ) AS "Campaign/Elements"

    FROM    vwCampaign Campaign
    LEFT JOIN dbo.vwCampaignAudio Audio ON Campaign.CampaignId = Audio.CampaignId
    LEFT JOIN dbo.vwCampaignVideo Video ON Campaign.CampaignId = Video.CampaignId
    WHERE   Campaign.CampaignId = 10370
    FOR XML PATH ('Campaign'), ROOT ('Campaigns'), ELEMENTS XSINIL

除了子查询的标记 之外, XML 现在几乎完美无缺。

<Campaigns xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Campaign>
    <Campaign>
      <ID>10370</ID>
      <CompanyID>C2811EA3-361A-411A-BB4C-816A5D6C12DB</CompanyID>
      <Name>Keith01</Name>
...
      <Characters>&lt;Character&gt;&lt;ID&gt;19029FFC-C1C0-4134-B813-93A9FF17C7F6&lt;/ID&gt;&lt;Name&gt;Jenna&lt;/Name&gt; ...
      <Elements>&lt;Element&gt;&lt;Element&gt;&lt;ID&gt;D9B2A643-73EC-4D55-BA34-D643113CEDEA&lt;/ID&gt;&lt;Editable&gt;1&lt;/Editable&gt; ...
    </Campaign>
  </Campaign>
</Campaigns>

Character 和 Element 子查询正在生成 < /> 而不是</ >,怎么说呢,URL安全标记?我不想要这个。

Image 和 Text 子子查询正在生成 &< /&> 而不是 < / >。请注意,它已被设为 URL 安全两次!我也不想要这个。

关于如何获得实际标记而不是这种废话的任何想法。:)

TIA

4

1 回答 1

7

您可能应该将“广告系列”转移到 PATH 中:

SELECT  Campaign.CampaignId "ID"      
, Campaign.CompanyId "CompanyID"      
, Audio.AudioID  "Audio/ID"      
, Audio.[Name]  "Audio/Name"
...      
FROM    vwCampaign Campaign
LEFT JOIN dbo.vwCampaignAudio Audio 
...
WHERE   Campaign.CampaignId = 10370
FOR     XML PATH('Campaign'), ELEMENTS XSINIL

更新

对于 suqueries 问题,您需要使用FOR XML PATH(..),TYPE来创建类型化的 XML 值(而不是包含 xml 的字符串)。XML 类型的子查询将创建一个 XML 元素,一个字符串只会将结果作为 text() 插入并被转义。

select a, (select b from t for xml path("b"), type) as "*" 
from ... for xml path("a") 
于 2009-09-04T22:20:54.333 回答