简体   繁体   English

针对架构和数据库的XML验证

[英]XML Validation against Schema and Database

I have an example XML like this : 我有一个这样的XML示例:

<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer # Sci-Fi</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy # Teen</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>

The objective is to perform a validation on XML structure and data integrity. 目的是对XML结构和数据完整性进行验证。
Example : 范例:
1). 1)。 A "book" must contain <author> , <title> , <genre> , <price> and so on. 一本“书”必须包含<author><title><genre><price>等。 Their data type must also be checked. 还必须检查其数据类型。
2). 2)。 Values of element <genre> must be validated against a table in database, and determine if they exists (valid) or not. 必须针对数据库中的表验证元素<genre>值,并确定它们是否存在(有效)。 In the case of valid, we will provide an alternative (generalized) value from that table so that we can choose whether to stick to old value or suggested value. 在有效的情况下,我们将从该表中提供一个替代(通用)值,以便我们可以选择保留旧值还是建议值。

Questions : 问题:
1). 1)。 Which DTD markup should I use to identify that a certain element must be validated against Database? 我应该使用哪个DTD标记来标识必须针对数据库验证某个元素?
2). 2)。 What kind of Schema validation will be better suited for this scenario (DTD - XSD - XDR)? 哪种模式验证更适合这种情况(DTD-XSD-XDR)?
3). 3)。 Any suggestion on how to visualize the suggested value and the existing value in this case of <genre> element ? 关于在<genre>元素的情况下如何形象化建议值和现有值的任何建议?

Any hints or code snippets are appreciated.. 任何提示或代码段均表示赞赏。

Here are some sample XML schemas (XSD). 是一些示例XML模式(XSD)。 You could basically just swap the shiporder element for you book element, it even goes into how to validate the datatypes and provide regular expressions for doing so. 您基本上可以只将shiporder元素替换为book元素,它甚至涉及如何验证数据类型并提供正则表达式。

I'm not sure what is meant by validated again a table in database . 我不确定validated again a table in database是什么意思。 Does the actual value have to be in the database in order to be valid? 实际值是否必须在数据库中才能有效? A suggestion for generalized genres would be to split the genre on the # and use the first token? 对于generalized类型的建议是将#上的类型拆分并使用第一个标记? That can then be suggested as a genre. 然后可以将其建议为一种体裁。

var dom = new XmlDocument();
dom.Load("catalog.xml");

var res = dom.SelectNodes("//book/genre", mgr);
foreach(XmlNode node in res){
    string s = node.InnerText();
    string[] tokens= s.Split('#');
    CheckAgainstDatabase(tokens[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM