Reusing XML objects
January 17, 2008 6:11 PM   Subscribe

Can I somehow reuse elements in an XML doc (e.g. - like the way you can use a ref tag to resuse elements in an XML Schema)?

I'm new-ish to XML and this seems like an obvious, simple thing to want to do, but I can't work out how to do it. There are a few element instantiations in my documents that I find myself re-doing over and over again. Is there anyway to define it once and then just refer to that one definition everywhere it needs to occur? You can do this with in a Schema, so it seems like you ought to be able to do it in a doc too... Bonus points if I can also override the values of some of its children.
posted by badstone to Computers & Internet (3 answers total) 2 users marked this as a favorite
 
You can do this in a dialect built with XML, because XML provides the concept of a node identifier. In many XML dialects, including XHTML and XML Schema, the id attribute specifies this identifier. Some XML tools, like an XSLT processor, will give you a way to generate unique identifiers programmatically.

The reuse you want really all depends on your XML dialect and your parsing and processing code to understand that when it sees an element with an attribute like, e.g. ref="#my-generic-stuff" then it should look up a node with the id my-generic-stuff and make the appropriate substitution.

XML itself isn’t really much more than a specification of a syntax; the semantics of the language depend entirely on your tools.
posted by ijoshua at 7:02 PM on January 17, 2008


What program is reading the XML? If your XML parser supports XInclude, you can put your common elements in their own file, and then xinclude them from the real document, like so:

toaster.xml

realdoc.xml

This won't allow you to override particular parts, though. I'd implement functionality like that using an XSLT stylesheet based on the identity pattern, but if you're new to XML, that's probably way too fancy for your needs.
posted by gsteff at 8:36 PM on January 17, 2008 [1 favorite]


Response by poster: the "program" is Java code written by me so I can do whatever. Never used XInclude before, but it looks like there's a sourceforge project. I'll look into it, thanks!
posted by badstone at 9:15 PM on January 17, 2008


« Older People search engine   |   How to not be treated like a criminal at a... Newer »
This thread is closed to new comments.