While validating against XSD, find the exact missing element in the XML using any of DOM, StAX, SAX parsers

前端 未结 1 909
太阳男子
太阳男子 2021-01-26 06:58

I have a XML file and its corresponding XSD file. While validating using StAX parser, I\'ve attached an error handler. Basically, I encounter two types of errors in a well-forme

相关标签:
1条回答
  • 2021-01-26 07:17

    The Saxon XSD validator gives you a message like this when a required element is missing:

    Validation error on line 12 column 17 of books.xml:
      FORG0001: In content of element <ITEM>: The content model does not allow element <PRICE>
      to appear immediately after element <PUB-DATE>. It must be preceded by <LANGUAGE>. 
      See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
    

    You could try pattern-matching the error message and extracting the name of the missing element.

    The reason most schema processors don't give you this information is because of the way they work internally. Typically the schema processor constructs a finite state machine which indicates, for each element in the input, which elements are allowed to come next. If the next element isn't one of those permitted, it's not immediately obvious from the FSM why this is this case. Saxon does some extra analysis to try and improve the diagnostics: if the input contains a disallowed transition from A to C, then it searches the FSM to discover that there are permitted transitions from A to B and from B to C, and constructs an error message to say that B was missing.

    0 讨论(0)
提交回复
热议问题