2011-05-08 42 views
-1

我想構建一個XML文檔驗證器。一個程序,通過XML文檔並根據定義的模式(不是XML與標準一致,但屬性符合特定規則)查找屬性重複和一致性。用於XML檢查的語言建議

我有經驗,有工作:

  • 的Java
  • 的Perl
  • Groovy的
  • C#
  • Ç

語言/圖書館/擴展,你會推薦這樣的任務是什麼?

在此先感謝

+0

它將如何託管和在什麼平臺上? – 2011-05-08 21:35:49

+0

這是一個客戶端應用程序。可以在Windows或Linux上運行。 – xain 2011-05-08 21:41:50

+0

你有沒有聽說過XML Schema? – 2011-05-08 21:44:53

回答

0

我會用的libxml2或它的一個實現在任何你喜歡的語言。如何驗證特定的文檔取決於它使用的XML方言。目前有三種常見的驗證機制:DTD,RelaxNG和XML-Schema,並且每個自尊的方言至少使用方言規範產生其中的一種。

下一個C版本驗證使用RelaXNG一個MATHML文件:

static const xmlChar 
mml_rng_uri[] = "http://www.w3.org/Math/RelaxNG/mathml3/mathml3.rng"; 

/** 
* @brief Validate the MathML document located at the given URI. 
*/ 
/* 
* -- Implementation notes -- 
* 
* The goal is xmlRelaxGNValidateDoc. 
* For that we need a xmlDocPtr for the document and xmlRelaxNGValidCtxtPtr 
* for the RelaxNG schema. 
* Given a uri we can use xmlCtxtReadFile for the document. 
* We will also need a validation schema, which is always the result of a 
* RelaxNG parse operation. 
* The parse operation requires a parser context obtained from either 
* xmlRelaxNGNewParserCtxt, which takes an URI or xmlRelaxNGNewMemParserCtxt 
* which takes a pointer and size. 
* 
* -- Short hand -- 
* xmlRelaxNGValidateDoc() 
* | 
* |- xmlDocPtr = xmlCtxtReadFile() 
* | | 
* | |- xmlParserCtxtPtr = xmlNewParserCtxt() 
* | 
* |- xmlRelaxNGValidCtxtPtr = xmlRelaxNGNewValidCtxt() 
* | | 
* | |- xmlRelaxNGPtr = xmlRelaxNGParse() 
* | | | 
* | | |- xmlRelaxNGParserCtxtPtr = xmlRelaxNGNewParserCtxt() 
* | | |- xmlRelaxNGParserCtxtPtr = xmlRelaxNGNewMemParserCtxt() 
*/ 
int MML_validate(const char *uri) 
{ 
    xmlDocPtr doc; 
    xmlParserCtxtPtr docparser; 
    xmlRelaxNGValidCtxtPtr validator; 
    xmlRelaxNGPtr schema; 
    xmlRelaxNGParserCtxtPtr rngparser; 
    int retval; 

    /* RelaxNG schema setup */ 
    rngparser = xmlRelaxNGNewParserCtxt(mml_rng_uri); 

    if((schema = xmlRelaxNGParse(rngparser)) == NULL) 
     errx(1, "Failed to parse MathML RelaxNG schema"); 
    if((validator = xmlRelaxNGNewValidCtxt(schema)) == NULL) 
     errx(1, "Failed to create a RelaxNG validator"); 

    /* MathML document setup */ 
    if((docparser = xmlNewParserCtxt()) == NULL) 
     errx(1, "Failed to create a document parser"); 
    if((doc = xmlCtxtReadFile(docparser, uri, NULL, XML_PARSE_XINCLUDE)) == 
      NULL) 
     errx(1, "Failed to parse document at %s", uri); 

    /* Validation */ 
    retval = xmlRelaxNGValidateDoc(validator, doc); 

    /* Clean up */ 
    xmlRelaxNGFreeValidCtxt(validator); 
    xmlRelaxNGFreeParserCtxt(rngparser); 
    xmlRelaxNGFree(schema); 

    return(retval); 
} 
0

這是太短了一個需求語句作出明確的說法,但它聽起來像一個Schematron的問題給我。