XML 中“循环”的标准?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3462418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
standard for "loops" in XML?
提问by Nathan
I am designing a simple XML file for animations. I often get in the situation, that I write something like this:
我正在为动画设计一个简单的 XML 文件。我经常遇到这样的情况,我写的是这样的:
<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!
谢谢!
采纳答案by DVK
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.
没有通用模型,但您要寻找的本质上是一个模板解决方案——模板语言加上模板评估引擎,通过执行嵌入在模板中的代码将模板转换为最终的 XML。
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...
如果您可以访问现有的模板引擎解决方案,那么最好的方法当然是利用;所以你不必写一个。例如 Java 的 JSP、Perl 的 EmbPerl、XSLT 等...
Here's what a sample EmbPerl template would look like:
下面是一个示例 EmbPerl 模板的样子:
[$ 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)
许多模板解决方案看起来非常相似,标记略有不同,逻辑语法明显不同(Perl vs. Java vs. xxx)
If you go with homebrew solution, the templating language can of course be XML itself, something like
如果您使用自制解决方案,模板语言当然可以是 XML 本身,例如
<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>
回答by schoetbi
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:
XML 是数据而不是指令。如果您需要在 xml 中使用某种指令,请将其设为数据。您可以像这样更改数据结构:
<frameset>
<start>1</start>
<end>3</end>
<prefix>./image</prefix>
<suffix>.png</prefix>
Basically your second approach.
基本上你的第二种方法。
回答by Jon Hanna
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.
在 XML 更适合表达的声明性语言中,“for all these”比循环本身更正常,尽管人们经常谈论 XSLT 的 for-each 就好像它是一个循环一样。
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.
您的第一个 XML 文件是框架的描述,第二个是与框架有关的描述。这些是独立的事物,将它们混合在一起令人担忧。
回答by jutky
You can use XSLT that would convert yours XML to the needed format.
您可以使用 XSLT 将您的 XML 转换为所需的格式。
Espesially for your's example <frameset min=1 max=5>you can use this XSLT example
特别是对于您的示例,<frameset min=1 max=5>您可以使用此 XSLT 示例
<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">
回答by John Saunders
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.
请记住,程序将读取 XML,而不是人类。该程序将需要进行测试,并且需要对其进行维护。
The more complexity you add to your XML format, the harder it will be to test and maintain the code that uses it.
向 XML 格式添加的复杂性越高,测试和维护使用它的代码就越困难。
In other words, leave the "for loop" alone, and just stick with the basics.
换句话说,不要理会“for 循环”,只关注基础知识。
回答by Bruno
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.
您可以使用 XSLT 来实现这一点,但这需要一个转换引擎。它们中的许多都存在(例如,包括在大多数浏览器中)。如果需要,您可以在给定文档的预处理指令中包含转换声明。
(It depends on the context where you use your XML documents.)
(这取决于您使用 XML 文档的上下文。)

