DTD必需元素排序 [英] DTD required elements ordering

查看:67
本文介绍了DTD必需元素排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以dtd中定义的任何顺序获得必需元素的列表,但不知道如何执行此操作。



例如,我有以下定义:

 <!ELEMENT父对象(child1,child2,child3)> 

此dtd声明将成功验证以下xml:

 < parent> 
< child1>< / child1>
< child2>< / child2>
< child3>< / child3>
< / parent>

但是在以下情况下,xml无法通过验证:

 < parent> 
< child2>< / child2>
< child1>< / child1>
< child3>< / child3>
< / parent>

一种可能的解决方案是声明

 <!ELEMENT父(child1 | child2 | child3)> 

但是在这种情况下,虽然验证会成功,但其中一个孩子可能会失踪。



当所需元素列表可以以任何顺序出现时,我需要正确的dtd元素声明。

解决方案

ChrisF说您不能执行此操作是错误的(但是对于检查规范感到很荣幸!);



但是,有一个复杂之处:DevNull为 parent 违反XML的确定性规则。非正式地,这些规则说解析器必须不提前知道就知道文档中每个元素匹配的内容模型中的哪个标记。如果解析器在 parent 元素中看到一个 child1 ,则不提前就无法知道它是否与 child1 在DevNull的内容模型中。这是一个错误(但是发生这种错误是处理器没有义务报告的错误-这是一个错误,表示所有赌注都已关闭,处理器可能在这里做任何事情,包括没有注意到任何错误)。 / p>

可以通过排除公共前缀来形成 parent 遵循确定性规则的内容模型,因此:

  <!ELEMENT父对象(
(child1,(((child2,child3)
|(child3,child2)))
|(child2,(((child1,child3)
|(child3,child1))))
|(child3,(((child1,child2)
|(child2,child1)))
)>

与其他模式语言中的该模型声明相比,此方法不方便元素很容易出错,并且手工操作非常繁琐。但是,使用DTD声明所需的内容模型并非没有可能。或者也许我应该说,仅对于那些无法忍受任何不便的DTD作者是不可能的。



还请注意,除非孩子的序列携带某些信息,否则某些设计权威人士强烈认为,修复序列更好。


I want to have list of required elements in any order defined in dtd, but have no idea how I could do this.

For example, I have following definition:

<!ELEMENT parent (child1, child2, child3)>

This dtd declaration will successfully validate following piece of xml:

<parent>
   <child1></child1>
   <child2></child2>
   <child3></child3>
</parent>

But in following case xml will not pass validation:

<parent>
   <child2></child2>
   <child1></child1>
   <child3></child3>
</parent>

One of the possible solution is to declare

<!ELEMENT parent (child1 | child2 | child3)>

But in this case one of the childs might be missing though validation will be successful.

I need correct dtd element declaration for the case when list of required elements can be present in any order.

解决方案

ChrisF is wrong to say you can't do this (but kudos for checking the spec!); DevNull [now known as Daniel Haley] is right to say you can.

There is, however, one complication: the content model given by DevNull for parent violates the determinism rules of XML. Informally, these rules say the parser must know, without lookahead, which token in the content model each element in the document matches. If in a parent element the parser sees a child1, it can't know without lookahead whether it has just matched the first or second occurrence of child1 in DevNull's content model. This is an error (but as it happens it's an error that processors are not obligated to report -- it's an error that means "all bets are off, the processor may do anything here, including fail to notice anything is wrong").

A content model for parent that obeys the determinism rules can be formed by factoring out common prefixes, thus:

<!ELEMENT parent ( 
                   (child1, ( (child2,child3)
                            | (child3,child2)))
                 | (child2, ( (child1,child3)
                            | (child3,child1)))
                 | (child3, ( (child1,child2)
                            | (child2,child1)))
                 ) >

This is less convenient than declarations for this model can be in other schema languages, and for more than three elements it's error prone and extremely tedious to do by hand. But it's not impossible to declare the content model you want with DTDs. Or perhaps I should say it's impossible only for DTD authors who are incapable of putting up with any inconvenience.

Note also that unless the sequence of children carries some information, some design authorities argue strongly that it's better to fix a sequence.

这篇关于DTD必需元素排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆