tags:

views:

68

answers:

6

Hi,

I am designing a simple XML file for animations. I often get in the situation, that I write something like this:

<frame>
    <image>./image1.png<image/>
</frame>
<frame>
    <image>./image2.png<image/>
</frame>
<frame>
    <image>./image3.png<image/>
</frame>
...

Now, this of course can get annoying. So I would I thought was allowing this:

<frameset min=1 max=5>
    <image>./image#.png<image/>
</frameset>

Making # a placeholder for a counter variable. What I am wondering: is there already a standardized way of doing something like this? It just makes things easier if one uses methods people are already used to.

Thanks!

A: 

Remember that a program will be reading the XML, not a human. The program will need to be tested, and it will need to be maintained.

The more complexity you add to your XML format, the harder it will be to test and maintain the code that uses it.

In other words, leave the "for loop" alone, and just stick with the basics.

John Saunders
A: 

You could use XSLT to achieve this, but this requires a transformation engine. Many of them are around (including in most browsers, for example). You can include the transform declaration in a pre-processing directive of a given document if you need.

(It depends on the context where you use your XML documents.)

Bruno
+2  A: 

There is no universal model, but what you're looking for is essentially a templating solution - a template language plus a template evaluation engine that transforms a template into final XML by executing the code embedded in the template.

So, you can take, for a model, ANY templating language you're familiar with.

The very best approach is of course to leverage an existing templating engine solution if you have access to one; so you don't have to write one. E.g. JSP for Java, EmbPerl for Perl, XSLT etc...

Here's what a sample EmbPerl template would look like:

[$ foreach my $i (1..5) $]
<frame>
    <image>./image[+$i+].png<image/>
</frame>
[$ endforeach $]

A lot of templating solutions would look very similar, with slightly different markup and obviously different logic syntax (Perl vs. Java vs. xxx)


If you go with homebrew solution, the templating language can of course be XML itself, something like

<frameset min=1 max=5>
    <image part=1 conent_type="constant">./image<image/>
    <image part=2 conent_type="iterator_value"><image/>
    <image part=3 conent_type="constant">.png<image/>
</frameset>
DVK
+1  A: 

XML is data not instructions. If you need a kind of instruction in the xml then make it data. You could change your datastructure like this:

<frameset>
<start>1</start>
<end>3</end>
<prefix>./image</prefix>
<suffix>.png</prefix>

Basically your second approach.

schoetbi
+1  A: 

You can use XSLT that would convert yours XML to the needed format.

Espesially for your's example <frameset min=1 max=5> you can use this XSLT example

<xsl:template match="iframe">
    <xsl:variable name="from" select="./@min"/>
    <xsl:variable name="to" select="./@max"/>
    --> loop from the ${from} to the ${to} variable, 
</xsl:template match="iframe">

the looping in the XSLT technique could be found here

jutky
+1  A: 

The problem with this is idea is that "loop" can mean so many different things.

In a procedural language it's clear enough that a loop means you do something and then you (maybe) do it again. The procedural language will already have a way of expressing how "do something" is expressed.

In a declarative language, which XML is a bit more suited to expressing, it's more normal to have a "for all of these" than a loop per-se, though people often talk about XSLT's for-each as if it is a loop.

Both procedural and declarative (and other) languages can be used to perform the sort of repetitive tasks you are talking about here, but putting it straight into the markup itself mixes up domains messily, unless it makes sense in terms of what the mark up is expressing to declare a "loop through" or "for each of these" in it.

Your first XML file is a description of frames, your second a description of something to do with frames. These are separate things, and mixing them together is fraught.

Jon Hanna