[xsd-users] Element names from exceptions
Boris Kolpackov
boris at codesynthesis.com
Mon Feb 14 15:23:23 EST 2011
Hi Darren,
Darren Garvey <darren.garvey at gmail.com> writes:
> The actual error descriptions are quite nice in most cases and I can pass
> them on directly. However, while the line / column numbers are useful when
> debugging problem, they're indecipherable to users, who don't see XML at all
> but a user interface. I was hoping to get information about the element
> being parsed without having to dig back into the XML using the line / column
> info.
>
> For instance, when an empty element is found that is expected to be a
> number, you get: "error: value '' does not match regular expression facet
> '[+\-]?[0-9]+'".
>
> Is there some means of getting the context of the error, eg. the element
> name that was being parsed?
The information in the error messages is what's provided by Xerces-C++
and there is no built-in support for element names/paths there. But you
can achieve this yourself with a little bit of code. Here is the outline:
1. First parse the document with validation enabled. If there were any
errors, remember their line/column information (see the
xml_schema::parsing exception for details).
2. Parse the XML again but this time with validation disables and into
a DOM document. You will also need to assign line/column numbers
to the elements in this DOM document. For more information on how
to do this, see the 'linecol' example:
http://www.codesynthesis.com/~boris/tmp/linecol.tar.gz
3. Now traverse the DOM document looking for elements that match
(or are closest too) the line number of the error. You can also
use the column number to further narrow down the list to a
single element. This might be necessary if you have something
like this in your XML:
<foo>foo</foo><bar>bar</bar>
4. Once you have the element, you can get its name or even traverse
its parents chain to construct an XPath-like path.
Boris
More information about the xsd-users
mailing list