简体   繁体   中英

XML Validation against Schema and Database

I have an example XML like this :

<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.
Example :
1). A "book" must contain <author> , <title> , <genre> , <price> and so on. Their data type must also be checked.
2). Values of element <genre> must be validated against a table in database, and determine if they exists (valid) or not. 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). Which DTD markup should I use to identify that a certain element must be validated against Database?
2). What kind of Schema validation will be better suited for this scenario (DTD - XSD - XDR)?
3). Any suggestion on how to visualize the suggested value and the existing value in this case of <genre> element ?

Any hints or code snippets are appreciated..

Here are some sample XML schemas (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.

I'm not sure what is meant by 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? 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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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