You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Gareth Reakes <ga...@decisionsoft.com> on 2003/09/23 14:14:09 UTC
Re: DOMLocator does not return a DOMElement
Hi,
On Mon, 22 Sep 2003, Larry Rosenzweig wrote:
> Hello,
>
> I am having a problem with Xerces C++ 2.3.0 where I am getting back a DOMText node from
> DOMLocator::getErrorNode(), instead of a DOMElement that I was expecting. I need to store all of the
> DOMElement nodes that generate errors when parsing (using a DOMBuilder), so I can refer to them later
> in my application. I am setting up my own error handler, which catches all of the errors as I
> expected, but then when I call getErrorNode() from the DOMLocator object that I get from the DOMError
> object passed into handleError(), it always returns a DOMTEXT node instead of a DOMElement node.
The spec says to return a DOMNode. There is not to much more information
about what should be returned apart from that. For example, if your
invalid element was caused due to the content of the element not matching
the pattern from the datatype, should the text node be returned or the
element node? We may well need to decide on that behaviour. If you do have
the text node you can always get its parent which, if it was the content
of an element, would be that element.
Take a quick look at the test data below:
<base xmlns="http://theborg.org"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation="http://theborg.org test.xsd">
<foo1 />
<foo2 >sdas</foo2>
</base>
<xs:element name="base" >
<xs:complexType >
<xs:sequence >
<xs:element name="foo1" type="xs:integer" />
<xs:element name="foo2" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The foo elements are obviously invalid. In the first the DOMElement itself
is returned as it is the node available that is invalid. In the second
example the DOMText is returned as it has failed validation. I agree that
this behaviour may seem a touch unintuitive. Maybe we should think about a
consistent policy, or at least thoroughly document the problem.
Gareth
--
Gareth Reakes, Head of Product Development +44-1865-203192
DecisionSoft Limited http://www.decisionsoft.com
XML Development and Services
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOMLocator does not return a DOMElement
Posted by Larry Rosenzweig <lr...@chisystems.com>.
Hi Gareth,
Sorry it has been a while, I was pulled away on another project at work.
I did as you suggested, subclassing the DOMBuilderImpl class and
overriding the virtual error() function call. However, it seems that I
am still in the same boat in this function as I was in the
DOMErrorHandler callback. I saw in the header file for the
AbstractDOMParser class that it stores both the current node
(fCurrentNode) and the parent node (fCurrentParent). However, while I
can get access to the current node via getCurrentNode(), there is no
equivalent function to get the parent. Unless calling
getCurrentNode()->getParentNode() would give me the same node as the
fCurrentParent member variable?
Since it has been a while since I last responded to you, I quickly recap
the problem I am having. Below is an exmaple schema and xml file that
shows the problem I am having:
<base xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:/Test/syntax.xsd">
<foo1>
<foo3/>
<foo2 ttt="ttt"/>
</foo1>
</base>
<xsd:element name="base">
<xsd:complexType >
<xsd:sequence >
<xsd:element name="foo1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="foo2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Using Xerces-C++ 2.3.0 on WIndows XP Pro. Using Visual C++ 6.0 (service
pack 5).
I do receieve the two errors I expect from the above example. One for
the invalid element (foo3) and one for the invalid attribute (ttt in
foo2). In the first case of the invalid element the getCurrentNode()
returns a DOMText node, not foo3 like I expected. If I get the parent
node via getCurrentNode()->getParentNode(), I get back foo1. In the
second case, getCurrentNode() returns a DOMText node, instead of foo2
like I expected. If I get the parent node via
getCurrentNode()->getParentNode(), I get back foo1. In both cases the
DOMText node does not have any siblings.
I looked at the area you pointed me to in SGXMLScanner.cpp for the case
of an invalid element, but was a little confused on how I could use it
to help me. Sorry if I am missing something simple.
I also looked at the XMLValidator class to see if that could help me,
but it didn't seem like it could.
Basically I want to store all of the nodes that caused errors according
to the schema when the file loads, so that I can then highlight the
visual representation of those nodes in my custom editor. I thought just
storing the nodes I got back in the DOMErrorHandler or the error()
function and going through them after the visual side has been created
would work. But I'm not getting the type of nodes I need to make this work.
If you need any more details or code samples, please let me know.
I really appretiate your help. Thank you for your time and effort.
Thanks
Larry Rosenzweig
Gareth Reakes wrote:
>Hi,
>
>
>
>>Do you think you could give me a little guidance into how to go about
>>creating this custom error method?
>>
>>
>
>Sure I can.
>
>
>
>
>>I have seen some information in the mail archives about setting up an
>>error handler for a XercesDOMParser, but I am using a DOMBuilder to
>>parse my file. I do set my own DOMErrorHandler and override the
>>handleError() call. But that is where I get the DOMLocator that returns
>>the DOMText node that isn't useful to me.
>>
>>
>
>DOMBuilderImpl implements the XMLErrorHandler. Take a look at the error
>method. It calls getCurrentNode. I would suggest you implement your own
>one of these that does something a bit more complex. It should look at the
>error code coming in and decide if it should getCurrentNode, or perhaps
>the parent etc.
>
>This is still going to cause a problem with you child not being in the
>schema case as you will not know which child to select. If you cannot live
>with this then I think the easiest way is to add a new error method to
>XMLErrorHandler that takes a more information. Take a look at
>SGXMLScanner.cpp:1406. You could call your new error method with info that
>enable the implementation to select the correct node.
>
>
>Give us a shout if any of this is not clear.
>
>
>Gareth
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOMLocator does not return a DOMElement
Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
> Do you think you could give me a little guidance into how to go about
> creating this custom error method?
Sure I can.
> I have seen some information in the mail archives about setting up an
> error handler for a XercesDOMParser, but I am using a DOMBuilder to
> parse my file. I do set my own DOMErrorHandler and override the
> handleError() call. But that is where I get the DOMLocator that returns
> the DOMText node that isn't useful to me.
DOMBuilderImpl implements the XMLErrorHandler. Take a look at the error
method. It calls getCurrentNode. I would suggest you implement your own
one of these that does something a bit more complex. It should look at the
error code coming in and decide if it should getCurrentNode, or perhaps
the parent etc.
This is still going to cause a problem with you child not being in the
schema case as you will not know which child to select. If you cannot live
with this then I think the easiest way is to add a new error method to
XMLErrorHandler that takes a more information. Take a look at
SGXMLScanner.cpp:1406. You could call your new error method with info that
enable the implementation to select the correct node.
Give us a shout if any of this is not clear.
Gareth
--
Gareth Reakes, Head of Product Development +44-1865-203192
DecisionSoft Limited http://www.decisionsoft.com
XML Development and Services
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOMLocator does not return a DOMElement
Posted by Larry Rosenzweig <lr...@chisystems.com>.
Hi Gareth,
Do you think you could give me a little guidance into how to go about
creating this custom error method?
I have seen some information in the mail archives about setting up an
error handler for a XercesDOMParser, but I am using a DOMBuilder to
parse my file. I do set my own DOMErrorHandler and override the
handleError() call. But that is where I get the DOMLocator that returns
the DOMText node that isn't useful to me.
Any help would be appreciated.
Thank you for your help
Larry Rosenzweig
Gareth Reakes wrote:
>Hi,
>
>
>
>
>><base xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/Test/syntax.xsd">
>> <foo1>
>> <foo3/>
>> <foo2/>
>> </foo1>
>></base>
>>
>>
>><xsd:element name="base">
>> <xsd:complexType >
>> <xsd:sequence >
>> <xsd:element name="foo1">
>> <xsd:complexType>
>> <xsd:sequence>
>> <xsd:element name="foo2"/>
>> </xsd:sequence>
>> </xsd:complexType>
>> </xsd:element>
>> </xsd:sequence>
>> </xsd:complexType>
>></xsd:element>
>>
>>
>>Obviously, foo3 is invalid since it isn't even in the schema. In this
>>case it returns me a DOMText node. When I get the parent of that DOMText
>>node, it returns me a DOMElement that represents foo1, where I was
>>expecting it to return the DOMElement that represented foo3.
>>
>>
>
>I think this comes down to semantics. The way I would view this is that
>the element that has an error it the first element that is invalid that is
>declared in the schema.
>
>
>
>
>>Is there a way to get the foo3 DOMElement when I get these errors for foo3?
>>
>>
>
>You can do this but it would require a small amount of customisation. You
>would have to call a custom error method that did not access the
>current node from the parser, but used the one you passed in. This is not
>to bad as long as it was only a few errors you wanted different nodes
>reported for. Give us a shout if you need more info.
>
>
>Gareth
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOMLocator does not return a DOMElement
Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
> <base xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/Test/syntax.xsd">
> <foo1>
> <foo3/>
> <foo2/>
> </foo1>
> </base>
>
>
> <xsd:element name="base">
> <xsd:complexType >
> <xsd:sequence >
> <xsd:element name="foo1">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="foo2"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
>
> Obviously, foo3 is invalid since it isn't even in the schema. In this
> case it returns me a DOMText node. When I get the parent of that DOMText
> node, it returns me a DOMElement that represents foo1, where I was
> expecting it to return the DOMElement that represented foo3.
I think this comes down to semantics. The way I would view this is that
the element that has an error it the first element that is invalid that is
declared in the schema.
> Is there a way to get the foo3 DOMElement when I get these errors for foo3?
You can do this but it would require a small amount of customisation. You
would have to call a custom error method that did not access the
current node from the parser, but used the one you passed in. This is not
to bad as long as it was only a few errors you wanted different nodes
reported for. Give us a shout if you need more info.
Gareth
--
Gareth Reakes, Head of Product Development +44-1865-203192
DecisionSoft Limited http://www.decisionsoft.com
XML Development and Services
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOMLocator does not return a DOMElement
Posted by Larry Rosenzweig <lr...@chisystems.com>.
Hi Gareth,
Your explanation makes sense. And I initially tried to get the parent of the DOMText node that was returned, figuring that it would be the DOMElement that I was looking for. But for some reason, the parent was the wrong DOMElement. It was one level higher than I expected. Here is an example of what I am using:
<base xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/Test/syntax.xsd">
<foo1>
<foo3/>
<foo2/>
</foo1>
</base>
<xsd:element name="base">
<xsd:complexType >
<xsd:sequence >
<xsd:element name="foo1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="foo2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Obviously, foo3 is invalid since it isn't even in the schema. In this
case it returns me a DOMText node. When I get the parent of that DOMText
node, it returns me a DOMElement that represents foo1, where I was
expecting it to return the DOMElement that represented foo3.
Here are the errors I get from Xerces 2.3.0:
Message: Unknown element 'foo3'
Message: Element 'foo3' is not valid for content model: '(foo2)'
Both of these result is getting a DOMText node from the DOMLocator, and
the parent of the DOMText node is the DOMElement foo1.
Is there a way to get the foo3 DOMElement when I get these errors for foo3?
thanks
Larry Rosenzweig
Gareth Reakes wrote:
>Hi,
>
>On Mon, 22 Sep 2003, Larry Rosenzweig wrote:
>
>
>
>>Hello,
>>
>>I am having a problem with Xerces C++ 2.3.0 where I am getting back a DOMText node from
>>DOMLocator::getErrorNode(), instead of a DOMElement that I was expecting. I need to store all of the
>>DOMElement nodes that generate errors when parsing (using a DOMBuilder), so I can refer to them later
>>in my application. I am setting up my own error handler, which catches all of the errors as I
>>expected, but then when I call getErrorNode() from the DOMLocator object that I get from the DOMError
>>object passed into handleError(), it always returns a DOMTEXT node instead of a DOMElement node.
>>
>>
>
>
>The spec says to return a DOMNode. There is not to much more information
>about what should be returned apart from that. For example, if your
>invalid element was caused due to the content of the element not matching
>the pattern from the datatype, should the text node be returned or the
>element node? We may well need to decide on that behaviour. If you do have
>the text node you can always get its parent which, if it was the content
>of an element, would be that element.
>
>Take a quick look at the test data below:
>
><base xmlns="http://theborg.org"
>xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
>xsi:schemaLocation="http://theborg.org test.xsd">
><foo1 />
><foo2 >sdas</foo2>
></base>
>
><xs:element name="base" >
> <xs:complexType >
> <xs:sequence >
> <xs:element name="foo1" type="xs:integer" />
> <xs:element name="foo2" type="xs:integer"/>
> </xs:sequence>
> </xs:complexType>
></xs:element>
>
>
>
>The foo elements are obviously invalid. In the first the DOMElement itself
>is returned as it is the node available that is invalid. In the second
>example the DOMText is returned as it has failed validation. I agree that
>this behaviour may seem a touch unintuitive. Maybe we should think about a
>consistent policy, or at least thoroughly document the problem.
>
>
>Gareth
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org