简体   繁体   English

同一xml文件中有多个xml元素

[英]Multiple xml elements in the same xml file

In a xml file this ok: 在xml文件中可以:

<target name="test">
    <exec executable="app">
        <arg value="run"/>
        <arg value="stop"/>
    </exec>     
</target>

But having having this in a xml file is not (at least that is what validation says): 但是在xml文件中却没有(至少这是验证所表明的):

<target name="test">
    <exec executable="app">
        <arg value="run"/>
        <arg value="stop"/>
    </exec>     
</target>
<target name="test">
    <exec executable="app">
        <arg value="run"/>
        <arg value="stop"/>
    </exec>     
</target>

Why is it illegal to have multiple target elements in the same xml file after each other? 为什么在同一个xml文件中一个接一个地包含多个目标元素是非法的?

"Why" questions are hard to answer. “为什么”的问题很难回答。 Partly it is that way in XML because it was in SGML. 在XML中,部分原因是这种方式,因为它在SGML中。 Enforcing a single document container element does have some advantages, although the main possible advantage; 尽管可能的主要优点是执行单个文档容器元素确实具有一些优点。 that you know when you have got to the end, is somewhat spoiled by the fact that there can be comments and processing instructions after the end of the root element. 在根元素末尾可以有注释和处理指令的事实,会破坏您知道何时到达末尾。

It is somewhat a pain if you want to use XML format in streaming context such as log files where you want to keep adding an element to the end. 如果要在流上下文(例如日志文件)中使用XML格式(要在其中继续添加元素),这会有些痛苦。

A common solution to that problem is to observe that the file with multiple top level elements is not a well formed document but it is a well formed external parsed entity . 解决该问题的常用方法是观察到具有多个顶级元素的文件不是格式正确的文档,而是格式良好的外部解析实体 So if you generate a file log.xml with dozens of top level elements then you cannot parse that as a document but you can make one static small document that references the external parsed entity log.xml and you are back on specified territory. 因此,如果您生成包含数十个顶级元素的文件log.xml ,则无法将其解析为文档,但是可以制作一个引用外部已解析实体log.xml静态小型文档,并且您将返回指定的区域。

wrapper.xml

<!DOCTYPE wrapper [
<!ENTITY thelog SYSTEM "log.xml">
]>
<wrapper>
&thelog;
</wrapper>

then if you parse wrapper.xml the parser will construct a document with a root element wrapper containing all the elements that were in the file log.xml . 然后,如果您解析wrapper.xml则解析器将构造一个带有根元素wrapper的文档,该wrapper包含文件log.xml中的所有元素。

Because a valid xml file should have one (and only one) root element. 因为有效的xml文件应具有一个(只有一个)根元素。

See the root element article from wikipedia or this W3C recommandation paper from which I extracted this quote : 请参阅Wikipedia的根元素文章或我从中提取此引用的W3C推荐论文

[Definition: There is exactly one element, called the root, or document element, no part of which appears in the content of any other element.] [定义:只有一个元素,称为根元素或文档元素,其中任何部分都没有出现在任何其他元素的内容中。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM