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