You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by igna <ig...@hotmail.com> on 2009/06/06 09:09:25 UTC

Keep undefined entity references while parsing

Hi,

I read in the Xerces API documentation: 


"entities" 

true [required] Keep EntityReference and Entity nodes in the document. 

false [required] (default) Remove all EntityReference and Entity nodes from
the document, putting the entity expansions directly in their place. Text
nodes are into "normal" form. Only EntityReference nodes to non-defined
entities are kept in the document. 

(http://xerces.apache.org/xerces-c/apiDocs-3/classDOMConfiguration.html) 


Therefore entity reference nodes to undefined entities should be kept in the
document. But try the next program: 


#include <iostream> 
using namespace std; 

#include <xercesc/dom/DOMNode.hpp> 
#include <xercesc/parsers/XercesDOMParser.hpp> 
#include <xercesc/util/PlatformUtils.hpp> 
using namespace xercesc; 

namespace { 
    bool entity_reference_node_found(DOMNode const* const node) 
    { 
        if (DOMNode::ENTITY_REFERENCE_NODE == node->getNodeType()) 
            return true; 
        for (DOMNode const* child = node->getFirstChild(); child; child =
child->getNextSibling()) 
            if (entity_reference_node_found(child)) 
                return true; 
        return false; 
    } 
} 

int main() 
{ 
    XMLPlatformUtils::Initialize(); 
    { 
        XercesDOMParser parser; 
        parser.parse("undefined_entity.xml"); 
        cout << entity_reference_node_found(parser.getDocument()) << '\n'; 
    } 
    XMLPlatformUtils::Terminate(); 
} 


with the next XML file: 


<?xml version="1.0"?> 
<!DOCTYPE test> 
<test>&abcd;</test> 


It will print 0. Why?

-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23899329.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Igor Ignatyuk <ig...@hotmail.com>.
Sorry, one more question. Is there a way to handle references to undefined
entities with Xercesc? Maybe with the SAXParser or somehow else?

Igor
-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23997515.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Igor Ignatyuk <ig...@hotmail.com>.
Thank you!

Igor

-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23965263.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Alberto Massari <am...@datadirect.com>.
After I changed your XML and code to be this one, it works for me.

XML file:

<?xml version="1.0"?>
<!DOCTYPE test [
<!ENTITY abcd "xyz">
]>
<test>&abcd;</test>

C++ code:

int main()
{
    XMLPlatformUtils::Initialize();
    {
        DOMLSParser* 
parser=DOMImplementation::getImplementation()->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
0);
        parser->getDomConfig()->setParameter(L"entities", true);
        DOMDocument const* const document = 
parser->parseURI("undefined_entity.xml");
        std::cout << entity_reference_node_found(document) << '\n';
        parser->release();
    }
    XMLPlatformUtils::Terminate();
}

The "entities" property has to be set on the parser, not on the final 
DOMDocument; and, if you want to set the "entities" property, you have 
to use the DOMLSParser class as dictated by the W3C specs. If you want 
to use the non-standard XercesDOMParser, you should use

        XercesDOMParser parser;
        parser.setCreateEntityReferenceNodes(true);
        parser.parse("undefined_entity.xml");
        DOMDocument const* const document = parser.getDocument();
        std::cout << entity_reference_node_found(document) << '\n';

Alberto


Igor Ignatyuk wrote:
> With this file too:
>
>
> <?xml version="1.0"?>
> <!DOCTYPE test
> <!ENTITY abcd "xyz">
>   
> <test>&abcd;</test>
>
>
> The entity is defined but the program prints 0.
>
> Igor
>
>   


Re: Keep undefined entity references while parsing

Posted by Igor Ignatyuk <ig...@hotmail.com>.
With this file too:


<?xml version="1.0"?>
<!DOCTYPE test
<!ENTITY abcd "xyz">
>
<test>&abcd;</test>


The entity is defined but the program prints 0.

Igor

-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23959870.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Alberto Massari <am...@datadirect.com>.
That's because it's undefined; as I wrote, that is an error, and Xerces 
does not supports creating a reference to a non-existent entity.

Alberto

Igor Ignatyuk wrote:
> Hi Alberto,
>
> Thank you. I added setting "entities" to "true", but the program prints 0 as
> before:
>
>
> #include <iostream>
> using namespace std;
>
> #include <xercesc/dom/DOMConfiguration.hpp>
> #include <xercesc/dom/DOMNode.hpp>
> #include <xercesc/parsers/XercesDOMParser.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> using namespace xercesc;
>
> namespace {
>     bool entity_reference_node_found(DOMNode const* const node)
>     {
>         if (DOMNode::ENTITY_REFERENCE_NODE == node->getNodeType())
>             return true;
>         for (DOMNode const* child = node->getFirstChild(); child; child =
> child->getNextSibling())
>             if (entity_reference_node_found(child))
>                 return true;
>         return false;
>     }
> }
>
> int main()
> {
>     XMLPlatformUtils::Initialize();
>     {
>         XercesDOMParser parser;
>         parser.parse("undefined_entity.xml");
>         DOMDocument const* const document = parser.getDocument();
>         document->getDOMConfig()->setParameter(L"entities", true);
>         cout << entity_reference_node_found(document) << '\n';
>     }
>     XMLPlatformUtils::Terminate();
> }
>
>
> Igor
>
>   


Re: Keep undefined entity references while parsing

Posted by Igor Ignatyuk <ig...@hotmail.com>.
Hi Alberto,

Thank you. I added setting "entities" to "true", but the program prints 0 as
before:


#include <iostream>
using namespace std;

#include <xercesc/dom/DOMConfiguration.hpp>
#include <xercesc/dom/DOMNode.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/PlatformUtils.hpp>
using namespace xercesc;

namespace {
    bool entity_reference_node_found(DOMNode const* const node)
    {
        if (DOMNode::ENTITY_REFERENCE_NODE == node->getNodeType())
            return true;
        for (DOMNode const* child = node->getFirstChild(); child; child =
child->getNextSibling())
            if (entity_reference_node_found(child))
                return true;
        return false;
    }
}

int main()
{
    XMLPlatformUtils::Initialize();
    {
        XercesDOMParser parser;
        parser.parse("undefined_entity.xml");
        DOMDocument const* const document = parser.getDocument();
        document->getDOMConfig()->setParameter(L"entities", true);
        cout << entity_reference_node_found(document) << '\n';
    }
    XMLPlatformUtils::Terminate();
}


Igor

-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23959377.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Alberto Massari <am...@datadirect.com>.
Hi Igor,
this seems more a documentation problem; the text in the original W3C 
specs says (see 
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-parameter-entities)|
|

"entities"

    true
        [required] (default)
        Keep EntityReference nodes in the document.
    false
        [required]
        Remove all EntityReference nodes from the document, putting the
        entity expansions directly in their place. Text nodes are
        normalized, as defined in Node.normalize. Only unexpanded entity
        references are kept in the document.

The "unexpanded entity references" is a link for 
http://www.w3.org/TR/2004/REC-xml-infoset-20040204/#infoitem.rse where 
it specifies

"A unexpanded entity reference information item serves as a placeholder 
by which an XML processor can indicate that it has not expanded an 
external parsed entity. There is such an information item for each 
unexpanded reference to an external general entity within the content of 
an element. A validating XML processor, or a non-validating processor 
that reads all external general entities, will never generate unexpanded 
entity reference information items for a valid document."

So such an entity reference should appear only if Xerces decides not to 
load an external entity, but it doesn't have such a feature; if instead 
the XML uses an entity that is not defined altogether, that is an error 
that you should see reported in the ErrorHandler (if validation is enabled).

Alberto

Igor Ignatyuk wrote:
> Yes, but entity reference nodes to undefined entities should be kept in the
> document in both case, if "entities" is set to "true" and if it is set to
> "false". (See the excerpt from the documentation in my first message.)
>   


Re: Keep undefined entity references while parsing

Posted by Igor Ignatyuk <ig...@hotmail.com>.
Yes, but entity reference nodes to undefined entities should be kept in the
document in both case, if "entities" is set to "true" and if it is set to
"false". (See the excerpt from the documentation in my first message.)
-- 
View this message in context: http://www.nabble.com/Keep-undefined-entity-references-while-parsing-tp23899329p23957495.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Keep undefined entity references while parsing

Posted by Alberto Massari <am...@datadirect.com>.
In your test program you are not setting the "entities" property to "true"

Alberto

igna wrote:
> Hi,
>
> I read in the Xerces API documentation: 
>
>
> "entities" 
>
> true [required] Keep EntityReference and Entity nodes in the document. 
>
> false [required] (default) Remove all EntityReference and Entity nodes from
> the document, putting the entity expansions directly in their place. Text
> nodes are into "normal" form. Only EntityReference nodes to non-defined
> entities are kept in the document. 
>
> (http://xerces.apache.org/xerces-c/apiDocs-3/classDOMConfiguration.html) 
>
>
> Therefore entity reference nodes to undefined entities should be kept in the
> document. But try the next program: 
>
>
> #include <iostream> 
> using namespace std; 
>
> #include <xercesc/dom/DOMNode.hpp> 
> #include <xercesc/parsers/XercesDOMParser.hpp> 
> #include <xercesc/util/PlatformUtils.hpp> 
> using namespace xercesc; 
>
> namespace { 
>     bool entity_reference_node_found(DOMNode const* const node) 
>     { 
>         if (DOMNode::ENTITY_REFERENCE_NODE == node->getNodeType()) 
>             return true; 
>         for (DOMNode const* child = node->getFirstChild(); child; child =
> child->getNextSibling()) 
>             if (entity_reference_node_found(child)) 
>                 return true; 
>         return false; 
>     } 
> } 
>
> int main() 
> { 
>     XMLPlatformUtils::Initialize(); 
>     { 
>         XercesDOMParser parser; 
>         parser.parse("undefined_entity.xml"); 
>         cout << entity_reference_node_found(parser.getDocument()) << '\n'; 
>     } 
>     XMLPlatformUtils::Terminate(); 
> } 
>
>
> with the next XML file: 
>
>
> <?xml version="1.0"?> 
> <!DOCTYPE test> 
> <test>&abcd;</test> 
>
>
> It will print 0. Why?
>
>