If you want to play along with the lesson, use the following code to create the table I will be using:

```CREATE TABLE employee_lang (
emp_nm nvarchar(30) not null,
lang nvarchar(255),
PRIMARY KEY(emp_nm) );
INSERT INTO employee_lang
(emp_nm, lang)
VALUES
('Bob', 'Python, R, Java'),
('Lisa', 'R, Java, Ruby, JavaScript'),
('Priyanka', 'SQL, Python');```

## XML Parsing

XML parsing is a SQL method for separating string data found in a single field in a table. Look at the table below: This table has two columns, emp_nm (employee name), lang (programming languages the employee is proficient in). Notice that the column lang has multiple values for each record. While this is easily human readable, if you want it to more machine usable (think Pivot tables or R statistical analysis), you are going to want your data to look more this this: Notice now the table has the same two columns, but the lang column now only has 1 word per record. So the question is, how do you do this using SQL?

## XML parsing

The code used to “parse” out the data in the lang column is below:

```SELECT emp_nm
,SUBSTRING(LTRIM(RTRIM(m.n.value('.','varchar(8000)'))),1,75) AS lang
FROM
(
SELECT
emp_nm
,CAST('<XMLRoot><RowData>' + REPLACE([lang],',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM employee_lang
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)```

Let’s break it down a little first.

```SELECT
emp_nm
,CAST('<XMLRoot><RowData>' + REPLACE([lang],',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM  employee_lang```

What we are doing with the code about is using a CAST and REPLACE functions to convert the elements in column lang into a XML line.  See results below: For Bob, this is the result of the CAST/REPLACE code on the lang column

<XMLRoot><RowData>Python</RowData><RowData> R</RowData><RowData> Java</RowData></XMLRoot>

This is how the code works from the inside out.

REPLACE()

select REPLACE (‘SQL ROCKS’, ‘S’, ‘!’)

If you run the above code, it will return !QL ROCK!

Replace is saying — everywhere an S is in the string, replace it with a !

CAST()

The cast function is casting the string as an XML data point. This is needed for the next section, when we unpack the XLM string

Cross Apply / Value

```SELECT emp_nm
,m.n.value('.','varchar(8000)')
FROM
(
SELECT
emp_nm
,CAST('<XMLRoot><RowData>' + REPLACE([lang],',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM employee_lang
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)```

So without going into way too much detail, you can find pages dedicated to Cross Apply and Value(), I’ll give you the quick breakdown.

First notice we aliased our CAST() statement as x.  So if you look at the CROSS APPLY, you will see we are asking to look at x.nodes. Had we aliased our cast y, we would be looking at y.nodes.

Now look at m(n) at the end of the line. This is like an array or list in programming languages. Keep that in mind for the next step.

Inside the x.nodes() is ‘/XMLRoot/RowData’ , this is telling us to assign to m everything following /XMLRoot and to iterate n by everything following /RowData, so for Bob:

m(n=1) = Python

m(n=2) = R

m(n=3) = Java

Now we pass that array m(n) to our Value() method.  Hence m.n.value().  Note m and n were just letters I picked, you can use others.

Inside m.n.value(‘.’,’varchar(8000)’) was used as it should pretty much cover any size string you may have to deal with.

So the final iteration simply add as SUBSTRING to clean it up and get rid of white space
{"ft_lang_label":"__label__en","ft_lang_prob":0.73433477,"math_prob":0.9379921,"size":3476,"snap":"2022-40-2023-06","text_gpt3_token_len":912,"char_repetition_ratio":0.12672812,"word_repetition_ratio":0.070143886,"special_character_ratio":0.27474108,"punctuation_ratio":0.13448735,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9611978,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-28T00:47:41Z\",\"WARC-Record-ID\":\"<urn:uuid:7241823e-45bf-42fc-b015-f3fb5dcf239b>\",\"Content-Length\":\"43416\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:44aec12d-9e47-420c-8d49-2d6a9eadeecd>\",\"WARC-Concurrent-To\":\"<urn:uuid:10922c88-31a7-4998-8b26-cd3a754b43fc>\",\"WARC-IP-Address\":\"192.0.78.25\",\"WARC-Target-URI\":\"https://analytics4all.org/2018/03/05/xml-parsing-advanced-sql/?replytocom=28432&amp=1\",\"WARC-Payload-Digest\":\"sha1:43GLRP3ZN7BLNHT6UPQ3LNQBO4DUSOQS\",\"WARC-Block-Digest\":\"sha1:AXUQXOIGNK3T5XIJ5GA7Q4OZWL4IFLIM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499468.22_warc_CC-MAIN-20230127231443-20230128021443-00326.warc.gz\"}"}