/usr/share/doc/libxml2-devel
NameSizeModeActions
examples/-0755rm
html/-0755rm
tutorial/-0755rm
APIchunk0.html302730644editdlrm
APIchunk1.html374930644editdlrm
APIchunk2.html406580644editdlrm
APIchunk3.html361930644editdlrm
APIchunk4.html361880644editdlrm
APIchunk5.html299040644editdlrm
APIchunk6.html290200644editdlrm
APIchunk7.html328470644editdlrm
APIchunk8.html302250644editdlrm
APIchunk9.html286980644editdlrm
APIchunk10.html643260644editdlrm
APIchunk11.html332270644editdlrm
APIchunk12.html878870644editdlrm
APIchunk13.html617870644editdlrm
APIchunk14.html454120644editdlrm
APIchunk15.html442360644editdlrm
APIchunk16.html363640644editdlrm
APIchunk17.html541530644editdlrm
APIchunk18.html428400644editdlrm
APIchunk19.html366570644editdlrm
APIchunk20.html327810644editdlrm
APIchunk21.html377910644editdlrm
APIchunk22.html579570644editdlrm
APIchunk23.html637970644editdlrm
APIchunk24.html945290644editdlrm
APIchunk25.html417300644editdlrm
APIchunk26.html321810644editdlrm
APIchunk27.html339820644editdlrm
APIchunk28.html580590644editdlrm
APIchunk29.html134130644editdlrm
APIconstructors.html595000644editdlrm
APIfiles.html3283290644editdlrm
APIfunctions.html2169820644editdlrm
APIsymbols.html3255690644editdlrm
architecture.html68630644editdlrm
bugs.html102450644editdlrm
catalog.gif61050644editdlrm
catalog.html236460644editdlrm
contribs.html76780644editdlrm
docs.html76250644editdlrm
DOM.gif31660644editdlrm
DOM.html66170644editdlrm
downloads.html82690644editdlrm
encoding.html194140644editdlrm
entities.html94430644editdlrm
example.html131050644editdlrm
FAQ.html211400644editdlrm
guidelines.html176620644editdlrm
help.html63210644editdlrm
index.html106690644editdlrm
interface.html82110644editdlrm
intro.html72070644editdlrm
library.html149950644editdlrm
libxml.gif76920644editdlrm
libxml2-api.xml.gz1617570644editdlrm
Libxml2-Logo-90x34.gif30700644editdlrm
Libxml2-Logo-180x168.gif81950644editdlrm
namespaces.html83370644editdlrm
news.html1673050644editdlrm
python.html199360644editdlrm
redhat.gif6970644editdlrm
searches.html75590644editdlrm
smallfootonly.gif27720644editdlrm
structure.gif55590644editdlrm
threads.html71130644editdlrm
tree.html79110644editdlrm
upgrade.html126640644editdlrm
w3c.png20280644editdlrm
xml.html3089760644editdlrm
xmlcatalog_man.html139230644editdlrm
xmldtd.html136450644editdlrm
XMLinfo.html67970644editdlrm
xmlio.html127950644editdlrm
xmllint.html232750644editdlrm
xmlmem.html144410644editdlrm
xmlreader.html201390644editdlrm
XSLT.html57750644editdlrm
Edit: /usr/share/doc/libxml2-devel/entities.html (9443B)
Entities or no entities
Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

The XML C parser and toolkit of Gnome

Entities or no entities

Developer Menu
API Indexes
Related links

Entities in principle are similar to simple C macros. An entity defines an abbreviation for a given string that you can reuse many times throughout the content of your document. Entities are especially useful when a given string may occur frequently within a document, or to confine the change needed to a document to a restricted area in the internal subset of the document (at the beginning). Example:

1 <?xml version="1.0"?>
2 <!DOCTYPE EXAMPLE SYSTEM "example.dtd" [
3 <!ENTITY xml "Extensible Markup Language">
4 ]>
5 <EXAMPLE>
6    &xml;
7 </EXAMPLE>

Line 3 declares the xml entity. Line 6 uses the xml entity, by prefixing its name with '&' and following it by ';' without any spaces added. There are 5 predefined entities in libxml2 allowing you to escape characters with predefined meaning in some parts of the xml document content: &lt; for the character '<', &gt; for the character '>', &apos; for the character ''', &quot; for the character '"', and &amp; for the character '&'.

One of the problems related to entities is that you may want the parser to substitute an entity's content so that you can see the replacement text in your application. Or you may prefer to keep entity references as such in the content to be able to save the document back without losing this usually precious information (if the user went through the pain of explicitly defining entities, he may have a a rather negative attitude if you blindly substitute them as saving time). The xmlSubstituteEntitiesDefault() function allows you to check and change the behaviour, which is to not substitute entities by default.

Here is the DOM tree built by libxml2 for the previous document in the default case:

/gnome/src/gnome-xml -> ./xmllint --debug test/ent1
DOCUMENT
version=1.0
   ELEMENT EXAMPLE
     TEXT
     content=
     ENTITY_REF
       INTERNAL_GENERAL_ENTITY xml
       content=Extensible Markup Language
     TEXT
     content=

And here is the result when substituting entities:

/gnome/src/gnome-xml -> ./tester --debug --noent test/ent1
DOCUMENT
version=1.0
   ELEMENT EXAMPLE
     TEXT
     content=     Extensible Markup Language

So, entities or no entities? Basically, it depends on your use case. I suggest that you keep the non-substituting default behaviour and avoid using entities in your XML document or data if you are not willing to handle the entity references elements in the DOM tree.

Note that at save time libxml2 enforces the conversion of the predefined entities where necessary to prevent well-formedness problems, and will also transparently replace those with chars (i.e. it will not generate entity reference elements in the DOM tree or call the reference() SAX callback when finding them in the input).

WARNING: handling entities on top of the libxml2 SAX interface is difficult!!! If you plan to use non-predefined entities in your documents, then the learning curve to handle then using the SAX API may be long. If you plan to use complex documents, I strongly suggest you consider using the DOM interface instead and let libxml deal with the complexity rather than trying to do it yourself.

Daniel Veillard