I have an issue with parsing text with special characters from XML in SQL Server.
Let's say I have a XML file Sample.xml which has the following data:
<People>
<Person FirstName="Adam"
LastName="Smith"
Age="44"
Weight="178">
<Texts>
<Text Country="US"
Language="EN"
TextType="1"><div>First sentence to retrieve.</div></Text>
<Text Country="GB"
Language="EN"
TextType="2"><div>Second sentence to retrieve.</div></Text>
</Texts>
</Person>
</People>
I prepared the following SQL script which can parse everything except two sentences in the <TextType> attribute:
- First sentence to retrieve
- Second sentence to retrieve
DECLARE @x XML
SELECT @x = f FROM OPENROWSET(BULK 'C:\Sample.xml', single_blob) AS C(f)
DECLARE @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
SELECT * FROM OPENXML (@hdoc, '/People/Person/Texts/Text')
WITH (
FirstName varchar(max) '../../@FirstName'
, LastName varchar(max) '../../@LastName'
, Age varchar(max) '../../@Age'
, [Weight] varchar(max) '../../@Weight'
, Country varchar(max) '@Country'
, [Language] varchar(max) '@Language'
, TextType varchar(max) '@TextType'
)
EXEC sp_xml_removedocument @hdoc
Could you please help me to add the column with the sentences mentioned above?