You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by "Costello, Robert" <rc...@searshc.com> on 2008/06/13 20:04:08 UTC

xquery against xmlerror

First, I want to commend this group for the general helpfulness given to
new users.  Many groups will dismiss questions from newbies as
bothersome.  This group seems to be above that. 

 

Now that I've greased my way in...

 

Here's a problem I'm trying to solve.  I have a service that takes an
xml file in.  I validate it against the schema.  The data has a few
errors.  I want to send only the good data along for more processing.
All I need to do is delete the bad elements.  Sounds easy.  My trouble
is knowing how to relate the error with the original file. 

 

I have an XmlObject from parsing, and an XmlError object from
validating.  The data looks something like this:

 

<?xml version="1.0" encoding="UTF-8"?> 

<somedoc xmlns:xsi="com.somewhere.somedoc" 

xsi:noNamespaceSchemaLocation='somedoc.xsd'> 

            <fileId>1234</fileId>

            <userId>joeuser</userId>

            <foobars>

                        <foo>

                                    <somekey>123</somekey> 

                                    <someotherkey>456</someotherkey>

                                    <someflag>Y</someflag>

                                    <somedata>something</somedata>

                                    <descriptions>

                                       <description>desc 1</description>

                                       <description>desc 2</description>

                                    </descriptions>

                        </foo>

                        <foo>

                                    <somekey>789</somekey> 

                                    <someotherkey>101112</someotherkey>

                                    <someflag>Y</someflag>


 
<somedata>thisfieldistoolong</somedata>

                                    <descriptions>

                                       <description>desc 1</description>

                                       <description>desc 2</description>

                                    </descriptions>

                        </foo>

            </foobars>

</somedoc>

 

I need to delete the foo element (and it's underlying children) since I
have one field with errors (<somedata>thisfieldistoolong</somedata>)

If I knew the index of foo that would be easy.  I don't see how I can
get that from the XmlError.  

 

Does anyone have an idea on this?

 

Thanks in advance,

 

Robert Costello

 

 

 


RE: xquery against xmlerror

Posted by "Costello, Robert" <rc...@searshc.com>.
For the sake of others who find the same problem, I have figured this
one out.  What held me back was that the XmlCursor obtained from the
XmlError could not move above the offending element.  In the example
below, it would be the description element.  A toParent call would not
ascend to <foo>.  I tried removing the reference to
noNameSpaceSchemaLocation, but it still would not work.  Then I put a
namespace in both the schema and the input xml doc and it worked.  So,
for anyone else who tries to use an XmlError object to relate back to
the XmlObject from the parse, it can be done, but you need to have a
namespace in order to traverse the XmlCursor.  

 

Snippets here:

 

           XmlCursor xmlCursor = error.getCursorLocation();

      while (xmlCursor.toParent()) {

                              if
(xmlCursor.getDomNode().getNodeName().equalsIgnoreCase("foo"))

                              { dosomething }

 

 

<xs:schema

   xmlns:xs="http://www.w3.org/2001/XMLSchema"

   xmlns:md="mydoc"

   targetNamespace="mydoc"

   elementFormDefault="qualified">

 

Robert Costello

 

 

-----Original Message-----
From: Costello, Robert 
Sent: Friday, June 13, 2008 1:04 PM
To: user@xmlbeans.apache.org
Subject: xquery against xmlerror

 

First, I want to commend this group for the general helpfulness given to
new users.  Many groups will dismiss questions from newbies as
bothersome.  This group seems to be above that. 

 

Now that I've greased my way in...

 

Here's a problem I'm trying to solve.  I have a service that takes an
xml file in.  I validate it against the schema.  The data has a few
errors.  I want to send only the good data along for more processing.
All I need to do is delete the bad elements.  Sounds easy.  My trouble
is knowing how to relate the error with the original file. 

 

I have an XmlObject from parsing, and an XmlError object from
validating.  The data looks something like this:

 

<?xml version="1.0" encoding="UTF-8"?> 

<somedoc xmlns:xsi="com.somewhere.somedoc" 

xsi:noNamespaceSchemaLocation='somedoc.xsd'> 

            <fileId>1234</fileId>

            <userId>joeuser</userId>

            <foobars>

                        <foo>

                                    <somekey>123</somekey> 

                                    <someotherkey>456</someotherkey>

                                    <someflag>Y</someflag>

                                    <somedata>something</somedata>

                                    <descriptions>

                                       <description>desc 1</description>

                                       <description>desc 2</description>

                                    </descriptions>

                        </foo>

                        <foo>

                                    <somekey>789</somekey> 

                                    <someotherkey>101112</someotherkey>

                                    <someflag>Y</someflag>


 
<somedata>thisfieldistoolong</somedata>

                                    <descriptions>

                                       <description>desc 1</description>

                                       <description>desc 2</description>

                                    </descriptions>

                        </foo>

            </foobars>

</somedoc>

 

I need to delete the foo element (and it's underlying children) since I
have one field with errors (<somedata>thisfieldistoolong</somedata>)

If I knew the index of foo that would be easy.  I don't see how I can
get that from the XmlError.  

 

Does anyone have an idea on this?

 

Thanks in advance,

 

Robert Costello