I have Sample Xml that looks something like this:

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

How can I just return "This is what I just want to show"?

Here is the tables and what I have tried.

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

and what I have tried:

This one only returns "This is" as expected.

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

This one is returning everything: "This is 1st. what I 2ndjust want to show"

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)

How can I exclude the comment nodes, but still get the correct results?

EDIT:

I want to return it like this:

enter image description here

Is there an easier way to do it than the following:

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)
有帮助吗?

解决方案

Try this:

 declare @xml xml;

 set @xml = convert(xml, 'your XML');

 with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
 select @xml.query('//stuff/text()')

Assuming you have a table with an XML column, it would look like this:

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

This is what my result looks like:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top