如何在从 SQL Server 中的 XML 数据中提取值时修改值

发布于 2024-11-02 23:15:51 字数 1850 浏览 0 评论 0原文

这是一个关于 SQL 中的 XQuerying 的问题(SQL Server 2008)

最初,我有一个 XML,我需要以不同的方式输出,以供参考:

declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (   
'
 <Root>
    <Elements>
      <Items>
        <OldItem>

          <ID>1</ID>
          <Show Pointer="yes" />
          <Display Pointer="Display">
            <Detail1>some Details</Detail1>
          </DisplayDetails>

        </OldItem>
      </Items>
    </Elements>
    </Root>'
)

    SELECT  a.value('(ID)[1]','int') as ID,
    a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
    a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
    a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
    FROM    @XMLNODE t
    cross apply
    t.doc.nodes('//OldItem') x(a)
    FOR XML PATH ('Items'),
    ROOT('Elements')

所以,现在我有一个像这样的 XML:

<Elements>
  <Items>
    <ID>1</ID>
    <ShowItem>yes</ShowItem>
    <DisplayDetails>true</DisplayDetails>
    <Detail1>some Details</Detail1>
  </Items>
</Elements>

Want to add some edit this to 1或没有值,基于是否为 true/yes 等。

即所需:

<Elements>
  <Items>
    <ID>1</ID>
    <ShowItem>1</ShowItem>
    <DisplayDetails>1</DisplayDetails>
    <Detail1>some Details</Detail1>
  </Items>
</Elements>

另外,如果 false 我只想将其更新为

有没有办法直接比较查询中的输入(例如是或否)并将其设置为 1 或 0 而不是 true 或 false?我正在使用 .query 来使用这些值并根据需要添加其他信息。

Here is a question on XQuerying in SQL (SQL Server 2008)

Originally, I have an XML that I need to need to output somewhat differently, for reference:

declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (   
'
 <Root>
    <Elements>
      <Items>
        <OldItem>

          <ID>1</ID>
          <Show Pointer="yes" />
          <Display Pointer="Display">
            <Detail1>some Details</Detail1>
          </DisplayDetails>

        </OldItem>
      </Items>
    </Elements>
    </Root>'
)

    SELECT  a.value('(ID)[1]','int') as ID,
    a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
    a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
    a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
    FROM    @XMLNODE t
    cross apply
    t.doc.nodes('//OldItem') x(a)
    FOR XML PATH ('Items'),
    ROOT('Elements')

So, now I have an XML like this:

<Elements>
  <Items>
    <ID>1</ID>
    <ShowItem>yes</ShowItem>
    <DisplayDetails>true</DisplayDetails>
    <Detail1>some Details</Detail1>
  </Items>
</Elements>

Want to add some modify this to 1 or no value, based on if it was true/yes etc.

i.e. desired:

<Elements>
  <Items>
    <ID>1</ID>
    <ShowItem>1</ShowItem>
    <DisplayDetails>1</DisplayDetails>
    <Detail1>some Details</Detail1>
  </Items>
</Elements>

Also, if <DisplayDetails>false</DisplayDetails> I just want to update it to <DisplayDetails />.

Is there a way to directly compare the inputs in the query (for example, yes or no) and set it to 1 or 0 instead of true or false? I am using a .query to use these values and add additional information as needed..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-11-09 23:15:51

我不太从 SQL 查询 XML,但如果我对任何非 XML 的内容执行此操作,我会在 SELECT 语句中执行类似的操作:

SELECT  a.value('(ID)[1]','int') as ID,
case 
  when a.value('(Show/@Pointer)[1]', 'varchar(5)') = 'yes' then '1'
  else '0'
End case as 'ShowItem',
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM    @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)

注意:尚未尝试过此操作,而且我不在数据库客户端上以确保我的语法完全正确。然而,检查数据库中的值并根据该值显示其他内容相对简单。

I don't query XML from SQL very much, but if I were doing this for anything that wasn't XML, I'd do something like this in the SELECT statement:

SELECT  a.value('(ID)[1]','int') as ID,
case 
  when a.value('(Show/@Pointer)[1]', 'varchar(5)') = 'yes' then '1'
  else '0'
End case as 'ShowItem',
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM    @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)

Note: this hasn't been tried, and I'm not on a DB client to make sure my syntax is exactly correct. However, checking a value from the DB and showing something else based on that value is relatively straight-forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文