Question

I have some generic XML snippets that need to use different entities files depending on the context (project) they are using in. I don't want to copy/paste them all over the place, duplicating the code.

XML entities don't propagate to XIncluded elements and it's not possible to dynamically specify the location either (which, well, wouldn't work for the same reason that entities don't propagate).

What would be a feasible way to feed different entity files to a generic XML snippet, depending on the context, that works with all/most XML tools?

Was it helpful?

Solution

Depends on your processing environment. Many XML parsers allow you to specify an EntityResolver or XmlResolver as a callback to process and resolve external entity references. Layered on top of this, you will find parsers or other utilities that support OASIS catalogs, which allow you to define indirections for entity references in an XML catalog file.

Personally, I prefer not to use entity references for this kind of application, but to use custom markup instead. (Some people prefer XLink/XInclude, but I prefer to roll my own.) It's then very easy to process the links/transclusions using XSLT. A particular benefit is that it's hard to transform XML while leaving the entity references intact, but it becomes very easy to leave links intact if they use logical (element-level) markup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top