【问题标题】:Query Not Returning Correct Results -- Sql Server Xquery查询没有返回正确的结果——Sql Server Xquery
【发布时间】:2013-11-03 01:23:18
【问题描述】:

我的示例 Xml 看起来像这样:

<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1">
  <tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff>
</tst:Root>

我如何才能返回“这就是我只想展示的内容”?

这是表格和我尝试过的。

CREATE TABLE [dbo].[XmlTable](
    [XmlId] [int] IDENTITY(1,1) NOT NULL,
    [XmlDoc] [xml] NOT NULL,
 CONSTRAINT [PK_XmlTable] PRIMARY KEY CLUSTERED 
(
    [XmlId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[XmlTable] ON 

INSERT [dbo].[XmlTable] ([XmlId], [XmlDoc]) VALUES (1, N'<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"><tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff></tst:Root>')
SET IDENTITY_INSERT [dbo].[XmlTable] OFF

以及我尝试过的:

这个只按预期返回“This is”。

--WITH XMLNAMESPACES (Default 'http://tempuri.org/some.xsd')
--SELECT     

--  a.value('text()[1]', 'nvarchar(100)') as SampleXml
--  From
--  XmlTable As x
--  Cross Apply XmlDoc.nodes('Root/stuff') a(a)

这个正在返回所有内容:“这是第一个。我第二个想要展示的内容”

WITH XMLNAMESPACES (Default 'http://tempuri.org/some.xsd')
SELECT     

    a.value('.', 'nvarchar(100)') as SampleXml
    From
    XmlTable As x
    Cross Apply XmlDoc.nodes('Root/stuff') a(a)

我怎样才能排除评论节点,但仍然得到正确的结果?

编辑:

我想这样退货:

有没有比以下更简单的方法:

WITH XMLNAMESPACES (Default 'http://tempuri.org/some.xsd')
SELECT     

a.value('text()[1]', 'nvarchar(100)') + a.value('text()[2]', 'nvarchar(100)') + a.value('text()[3]', 'nvarchar(100)')  as SampleXml

From
XmlTable As x
Cross Apply XmlDoc.nodes('Root/stuff') a(a)

【问题讨论】:

    标签: sql-server-2008 tsql xquery xquery-sql


    【解决方案1】:

    试试这个:

     declare @xml xml;
    
     set @xml = convert(xml, 'your XML');
    
     with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
     select @xml.query('//stuff/text()')
    

    假设您有一个带有 XML 列的表,它看起来像这样:

     -- Set up the XML table to demonstrate
     declare @table table
     (
          ID int,
          xmlDoc  xml
     )
    
     -- insert your example XML
     insert into @table 
     Values (1, '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"><tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff></tst:Root>');
    
     -- Do the query
     with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
     select ID, xmlDoc.query('//Root/stuff/text()') as SampleXml
     from @table
    

    这是我的结果:

    【讨论】:

    • 感谢您的回复,我希望它是单行而不是 3!我能够通过我发布的更新查询获得它,我只是想知道是否有更好的方法!这似乎不是最好的方法。为良好的反应点赞。再次感谢!
    • 它可以比你做的更简单。假设您的 XML 文档是记录中的一个字段,您根本不需要使用 Cross Apply(除非您想做其他未提及的事情)。有关示例,请参阅我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多