我有一个表格,其中的列中包含无类型的xml,我需要将其复制到随播数据库,然后进行操作和使用。我决定最好在伴随数据库上创建XML模式集合,并使用它来为伴随XML列提供类型,从而允许更快地使用XML和一定级别的验证。我需要定期从非类型化列中复制数据。
我的问题是,无类型的xml不包含命名空间或版本;我的理解是,如果我希望将无类型的xml插入到我的类型列中并验证它,我需要添加一个名称空间声明和版本。最好的方法是什么(这里最好的方法是对大量行最快的可靠方法)?
目前,我所拥有的只是将xml转换为varchar,然后使用STUFF()
插入命名空间声明等,并将结果转换回xml,这看起来有点像钩子。我已经尝试使用modify()
但是到目前为止还没有找到正确的语法(插入一个名为“xmlns”的属性会出错并使用.modify('declare default element namespace "http://tempuri.org/QAnswers.xsd";')
似乎需要以下xquery或其他东西......
这是一个例子:
DECLARE @Table TABLE (Id INT IDENTITY(1,1), Answers XML)
INSERT @Table
SELECT
N'<questionnaire-answers type="Medical">
<title lang="en">New Thing</title>
<sections>
<section id="4684f484-080a-4ac7-90cb-f8064a0ea1ae">
<title lang="en">First SubThing</title>
<description lang="en">MyThingDescription
</description>
<footer lang="en">PLEASE LOG CLEARLY </footer>
<elements>
<question-freetext id="d0a75e8f-9856-4eb4-9187-b112cb2e5edd" name="Thing Info" value="Something Happened">
<text lang="en" />
</question-freetext>
</elements>
</section>
</sections>
</questionnaire-answers>'
-- SOME ATTEMPTS --
--DECLARE @NS VARCHAR(50) = 'http://tempuri.org/QAnswers.xsd'
--UPDATE @Table
--SET Answers.modify('declare default element namespace "http://tempuri.org/QAnswers.xsd";')
--SET Answers.modify('insert attribute xmlns {sql:variable("@NS")} into (/questionnaire-answers)[1]')
-- The only thing I have been able to make work
UPDATE @Table
SET Answers =
STUFF(CONVERT(VARCHAR(MAX),Answers),1,23,'<?xml version ="1.0"?>
<questionnaire-answers xmlns ="http://tempuri.org/QAnswers.xsd" ')
我需要的结果看起来像这样(版本显然没有显示,但它就在那里!):
<questionnaire-answers xmlns="http://tempuri.org/QAnswers.xsd" type="Medical">
<title lang="en">New Thing</title>
<sections>
<section id="4684f484-080a-4ac7-90cb-f8064a0ea1ae">
<title lang="en">First SubThing</title>
<description lang="en">MyThingDescription
</description>
<footer lang="en">PLEASE LOG CLEARLY </footer>
<elements>
<question-freetext id="d0a75e8f-9856-4eb4-9187-b112cb2e5edd" name="Thing Info" value="Something Happened">
<text lang="en" />
</question-freetext>
</elements>
</section>
</sections>
</questionnaire-answers>