我在SQL中有VIEW
,其列类型为XML。 VIEW
的定义如下。
CREATE VIEW [dbo].[View_RequestDetails]
AS
SELECT
r.RequestID, r.CompTypeID, ct.Name AS CompType,
r.LibID, r.Status, r.StateID, r.History,
r.LogID, r.ClosedDate, s.Closed AS StateClosed,
r.SubmissionDate, r.SubmitterUID,
(submitter.LName + ', ' + submitter.FName + ' ' + submitter.MiddleInit) AS SubmitterName,
r.ModifiedDate, r.ModifiedByID, r.LockStatus AS Lock,
(modifier.LName + ', ' + modifier.FName + ' ' + modifier.MiddleInit) AS ModifierName,
(SELECT AttributeName AS '@key', AttributeValue AS '@value'
FROM dbo.View_RequestAttributes ra
WHERE ra.RequestId = r.RequestId
FOR XML PATH('Attribute'), ROOT('RequestAttributes')) AS RequestAttributes,
dbo.fnGetRequestProjectName(r.RequestID) AS ProjectName,
dbo.fnGetRequestProjectPriority(r.RequestID) AS ProjectPriority
FROM
dbo.Request AS r
LEFT JOIN
dbo.State AS s ON r.StateId = s.StateId
LEFT JOIN
dbo.ComponentType AS ct ON r.CompTypeID = ct.CompTypeId
LEFT JOIN
dbo.UserInfo AS submitter ON r.SubmitterUID = submitter.UID
LEFT JOIN
dbo.UserInfo AS modifier ON r.ModifiedByID = modifier.UID
为列生成的XML值包含一些无效字符,如(	
..等),因为生成此视图的表列中的值。
有没有什么办法可以限制XML类型列来生成格式良好的XML?
或者我们是否需要解析我们的代码以使XML有效并显示给用户?
答案 0 :(得分:0)
尝试更改此
(SELECT AttributeName AS '@key', AttributeValue AS '@value'
FROM dbo.View_RequestAttributes ra
WHERE ra.RequestId = r.RequestId
FOR XML PATH('Attribute'), ROOT('RequestAttributes')) AS RequestAttributes,
到此(您需要TYPE
扩展名让子选择回来时使用“真正的”XML)
(SELECT AttributeName AS '@key', AttributeValue AS '@value'
FROM dbo.View_RequestAttributes ra
WHERE ra.RequestId = r.RequestId
FOR XML PATH('Attribute'), ROOT('RequestAttributes'),TYPE) AS RequestAttributes,