English 中文(简体)
使用 SQL 服务器2008 运行时指定的 XSD 校验 XML 吗?
原标题:Validate XML using XSD specified at runtime in SQL Server 2008?

SQL SQL 服务器 2008 允许我们根据现有的 XML Schema 收藏来验证 XML, 定义了打印的 XML 列/ 可变的 : < code>DECLARE @ TypedXml XML (MyXmlSchema Collection)

然而,据我所知,在我们定义列/可变列时,必须知道XML Schema收藏。

是否有方法使用 runtime 指定的 XML 模式验证 XML?

例如:

DECLARE @Xml XML
SET @Xml = 
N <person>
    <firstname>Ming</firstname>
    <lastname>The Merciless</lastname>
</person> 

DECLARE @Xsd XML
SET @Xsd =
N <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
      <xs:element name="dateofbirth" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema> 

DECLARE @Valid BIT
SET @Valid = (SELECT dbo.f_ValidateXmlAgainstXsd(@Xml, @Xsd)) -- With a user-defined function?
EXEC @Valid = s_ValidateXmlAgainstXsd @Xml, @Xsd -- With a stored procedure?
最佳回答
问题回答

如果您对存储在数据库中的一套预定义的 XSD 进行验证,可以在存储程序中使用动态 SQL 进行验证。

如果您想要对照任何 XSD 校验, 最好使用 CLR, 如 Petru s 回答中的 CLR 。

以下脚本将有效 。 如果输入 XML 无效, 它会丢出一个例外 。 如果您想要验证结果, 而不是丢弃例外, 您可以使用试/ 抓获条款 。

Edit: You cannot use dynamic SQL from within a function, but you could wrap this script in a stored procedure.

    DECLARE @result int

    DECLARE @XsdValidationSQL nvarchar(max) = 
         DECLARE @xml xml(  + @xsdSchema +  .  + @xsdName +  ) =     + CONVERT(nvarchar(max), @xml) +     

    EXEC @result = sp_executesql @XsdValidationSQL

    RETURN @result




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml