Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

standard for "loops" in XML?

Tags:

loops

xml

I am designing a simple XML file for animations. I often get in a 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 hoped XML allowed something like 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.

like image 491
Nathan Avatar asked Dec 28 '22 08:12

Nathan


2 Answers

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>
</frameset>

Basically your second approach.

like image 157
schoetbi Avatar answered Dec 30 '22 21:12

schoetbi


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 content_type="constant">./image<image/>
    <image part=2 content_type="iterator_value"><image/>
    <image part=3 content_type="constant">.png<image/>
</frameset>
like image 29
DVK Avatar answered Dec 30 '22 23:12

DVK