1

After I did some internet research I found out that nodes function for xml processing of Sql Server does not accept a variable as parameter. If I try to use a variable a get the following error:

The argument 1 of the XML data type method "nodes" must be a string literal.

This is what I am trying to do:

SET @xPath = REPLACE(@xPath,'|','/')
SET @xPath = '/' + @xPath + '/text()'
SELECT @ParsedHtml = @ParsedHtml + CONVERT(nvarchar(max),Col.query('.')) + ';' FROM @Html.nodes(@xPath)AS T(Col)

Is there any hack i can do to make nodes function accept a variable as parameter???

1 Answer 1

2

You can use a variable in the path like this:

SET @xPath = REPLACE(@xPath,'|','/')

set @ParsedHtml = ''
SELECT @ParsedHtml = @ParsedHtml + CONVERT(nvarchar(max),Col.query('.')) + ';' 
FROM @Html.nodes('/*[local-name()=sql:variable("@xPath")]/text()')AS T(Col)

I don't think you can do multiple levels in one variable in the path, which it looks like you are trying to do. You'd have to specify each level with a parameter of it's own. If you need to do a dynamic number of path levels, you might look into using dynamic sql.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.