How to parse XHTML node value with TSQL?

All we need is an easy explanation of the problem, so here it is.

I have the following XHTML and would like to be able to parse out My Node Value. Is there a way to easily do this with TSQL?

<SPAN style="WHITE-SPACE: normal; WORD-SPACING: 0px; TEXT-TRANSFORM: none; FLOAT: none; COLOR: rgb(0,0,0); TEXT-ALIGN: left; FONT: 10px verdana; DISPLAY: inline !important; LETTER-SPACING: normal; TEXT-INDENT: 0px; -webkit-text-stroke-width: 0px">My Node Value</SPAN>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can extract the node text with XML methods as long as the value is well-formed XML.

If the XHTML value is a parameter or variable:

DECLARE @XHTML xml = N'<SPAN style="WHITE-SPACE: normal; WORD-SPACING: 0px; TEXT-TRANSFORM: none; FLOAT: none; COLOR: rgb(0,0,0); TEXT-ALIGN: left; FONT: 10px verdana; DISPLAY: inline !important; LETTER-SPACING: normal; TEXT-INDENT: 0px; -webkit-text-stroke-width: 0px">My Node Value</SPAN>'
SELECT @XHTML.value('.','varchar(100)');

If the XHTML value is in a table column:

SELECT YourColumn.value('.','varchar(100)')
FROM dbo.YourTable;

The node paths can be adjusted if the SPAN element is not the root node.

Method 2

The correct, and more performant, way to retrieve a node’s inner text is to use the text() function.

You also need [1] to statically guarantee a single value

SELECT YourColumn.value('(/SPAN/text())[1]', 'varchar(100)')
FROM dbo.YourTable;

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply