【问题标题】:xml nodes within sql: how to force end daugther nodessql 中的 xml 节点:如何强制结束 daugther 节点
【发布时间】:2017-03-10 14:28:55
【问题描述】:

有没有办法在 sql 中强制结束 xml daugther 节点?我需要结束并重新打开一个特定的节点。我的代码目前看起来像:

USE [DBNAME]
Select (Select 
        'EK' as 'prices/price/group',
        XUVP as 'prices/price/price',
        '1'  as 'prices/price/from',
        'beliebig' as 'prices/price/to',
        'FH' as 'prices/price/group',
        XHEK as 'prices/price/price',
        '1'  as 'prices/price/from',
        'beliebig' as 'prices/price/to',
        'D' as 'prices/price/group',
        XDEK as 'prices/price/price',
        '1'  as 'prices/price/from',
        'beliebig' as 'prices/price/to',
        'P' as 'prices/price/group',
        XPEK as 'prices/price/price',
        '1'  as 'prices/price/from',
        'beliebig' as 'prices/price/to'             
        FOR XML Path('article'),Type,Elements)      
FROM [dbo].[TABLENAME] FOR XML PATH ('articles'), Elements, ROOT('Root')
GO

导致:

      <prices>
        <price>
          <group>EK</group>
          <price>459.000</price>
          <from>1</from>
          <to>beliebig</to>
  ---->   </price>
  ---->   <price> 
          <group>FH</group>
          <price>279.000</price>
          <from>1</from>
          <to>beliebig</to>            
        </price>
      </prices>

我需要在四个子注释之后“强制结束”每个价格层(第二层),并为四个节点的下一个开始一个新的价格层。缺少的结束和开始标签被添加并标记在上面发布的结果中。

此外,我想为任何语法错误道歉,但我不是本地人。即使这个问题对于习惯 sql 的人来说可能是一件容易的事情,但我真的很感谢您能帮上忙。

在此先感谢并致以最良好的祝愿

【问题讨论】:

    标签: sql xml tsql tags nodes


    【解决方案1】:

    不确定这是否是您正在寻找的确切结果,但您可以通过插入 NULL 看到,标签将重置

    Declare @YourTable table (XUVP int,XHEK int,XDEK int,XPEK int)
    Insert Into @YourTable values
    (459,279,-999,999)
    
    Select (Select 
            'EK' as 'price/group',
            XUVP as 'price/price',
            '1'  as 'price/from',
            'beliebig' as 'price/to',
            null,
            'FH' as 'price/group',
            XHEK as 'price/price',
            '1'  as 'price/from',
            'beliebig' as 'price/to',
            null,
            'D' as 'price/group',
            XDEK as 'price/price',
            '1'  as 'price/from',
            'beliebig' as 'price/to',
            null,
            'P' as 'price/group',
            XPEK as 'price/price',
            '1'  as 'price/from',
            'beliebig' as 'price/to'             
            FOR XML Path('prices'),Type,Elements, ROOT('article'))      
    FROM @YourTable FOR XML PATH ('articles'), Elements, ROOT('Root')
    

    退货

    <Root>
      <articles>
        <article>
          <prices>
            <price>
              <group>EK</group>
              <price>459</price>
              <from>1</from>
              <to>beliebig</to>
            </price>
            <price>
              <group>FH</group>
              <price>279</price>
              <from>1</from>
              <to>beliebig</to>
            </price>
            <price>
              <group>D</group>
              <price>-999</price>
              <from>1</from>
              <to>beliebig</to>
            </price>
            <price>
              <group>P</group>
              <price>999</price>
              <from>1</from>
              <to>beliebig</to>
            </price>
          </prices>
        </article>
      </articles>
    </Root>
    

    【讨论】:

    • 嗨,又是一个很好的答案。您可以访问this question。有人急需您的 read-my-XML-function :-)
    • 感谢您的回答!是否可以在文章节点中添加不在价格节点内的其他节点?
    • @Oto 当然,但我需要一个样本
    • 感谢您的快速答复。怎么寄给你?
    • 我需要在大价格节点之后的一些节点,一个新的类别母节点,里面有一些子节点,然后在那个块上再有几个节点。
    猜你喜欢
    • 2013-02-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多