是否有创建 XML 文件的标准或指南?

发布于 2024-08-02 06:47:36 字数 1431 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

狂之美人 2024-08-09 06:47:36

这完全取决于您尝试用 XML 文档表示的内容的语义。

例如,如果您的 SomeBaseTag 代表市场摊位,Item1 代表苹果,Item2 代表橙子,那么第一种格式就完全合适了。

然而,如果这两个项目是不同的并且最好单独分组,那么第二种格式更有意义。如果 SomeBaseTag 表示基本粒子且 **Item1** 是费米子且 **Item2**s 是玻色子。

事实上,在您的示例中,两个不同的项目共享相同的属性名称,这使得它们更紧密地相关更符合逻辑。

It all depends on the semantics of what you are trying to represent with your XML document.

For example, if your SomeBaseTag represents a market stall and Item1 represents apples and Item2 represents oranges, then the first format is perfectly appropriate.

If, however, the two items are distinct and would be better grouped separately then the second format makes more sense. This would be the case if SomeBaseTag represented elementary particles and **Item1**s were fermions and **Item2**s were bosons.

The fact that in your example the two different items share the same attribute names, makes it more logical that they are more closely related.

‖放下 2024-08-09 06:47:36

正如前面所说,风格和品味是首要因素。还有其他的。

属性在其可以包含的内容方面受到限制。例如,它们不能包含元素。此外,某些字符如“<”不能出现在属性中。元素可以包含文本、其他元素或两者。

我还将提到一个具体的“风格”问题。您的 XML 应该保持一致。我不喜欢 WSDL 的一件事是,除了 messages 之外,大多数内容都包含在包装元素中:

<wsdl>
    <types/>

    <message/>
    <message/>
    <message/>

    <portTypes/>
    <bindings/>
    <service/>
</wsdl>

我一直对没有 < /代码> 元素。

As has been said, style and taste are the primary factors. There are others.

Attributes are restricted in terms of what they can contain. For instance, they cannot contain elements. Also, certain characters like "<" cannot appear in an attribute. An element may contain text, other elements, or both.

I'll also mention one specific "style" issue. Your XML should be consistent. One thing that I dislike about WSDL is that most of the contents are contained in wrapper elements, except for messages:

<wsdl>
    <types/>

    <message/>
    <message/>
    <message/>

    <portTypes/>
    <bindings/>
    <service/>
</wsdl>

I've always been annoyed that there is no <messages/> element.

假面具 2024-08-09 06:47:36

这是一个风格问题——属性使 xml 看起来比元素密集的对应部分更干净、更简洁。它还取决于您用于解析此类 xml 的工具 - 我过去使用过的一些工具当它是元素与属性时更容易编码。但这方面并没有什么大不了的。最好让 xml 较小,因为 xml 本身已经很冗长了

It is a style thing - attributes make xmls to be look cleaner and less verbose than the element heavy counterpart. Also it depends on the tools you use for parsing such xmls - some I have used in the past are easier to code when it is an element vs attribute. But this aspect is not a big deal. It is better to keep xml smaller since xml in itself is already verbose

合久必婚 2024-08-09 06:47:36

我不同意这是一个“品味问题”。 XML 元素和属性的语义不同:

  • 元素的顺序很重要,而属性的顺序则不重要。
  • 允许重复的元素,但不允许重复的属性。
  • 空格在属性中很重要,但通常在元素中并不重要。
  • 元素可以包含其他元素,属性不能包含属性或元素。
  • 无法识别的属性将被忽略,无法识别的元素通常会被传递。

例如,像这样的编号元素通常是不好的,因为元素已经排序。我会将您的架构更改为:

<SomeBaseTag>
    <SomeItemTag>
        <SomeAttributeTag>one</SomeAttributeTag>
        <AnotherAttributeTag>two</AnotherAttributeTag>
    </SomeItemTag>
    <SomeItemTag>
        <SomeAttributeTag>one</SomeAttributeTag>
        <AnotherAttributeTag>two</AnotherAttributeTag>
    </SomeItemTag>
</SomeBaseTag>

其中标签名称是有意义的单词,如“person”、“address”或“primeFactor”,而不是无意义的单词,如“Item”或“attribute”。

I do not agree that this is a "matter of taste". The semantics of XML elements and attributes are different:

  • Order of elements is significant, order of attributes is not.
  • Duplicate elements are allowed, duplicate attributes are not.
  • Whitespace is significant in attributes, and typically is not in elements.
  • Elements can contain other elements, attributes cannot contain attributes or elements.
  • Unrecognized attributes are ignored, unrecognized elements are typically passed on.

For example, numbered elements like are typically bad because elements are already ordered. I would change your schema to:

<SomeBaseTag>
    <SomeItemTag>
        <SomeAttributeTag>one</SomeAttributeTag>
        <AnotherAttributeTag>two</AnotherAttributeTag>
    </SomeItemTag>
    <SomeItemTag>
        <SomeAttributeTag>one</SomeAttributeTag>
        <AnotherAttributeTag>two</AnotherAttributeTag>
    </SomeItemTag>
</SomeBaseTag>

Where the tag names are meaningful words like "person", "address", or "primeFactor", not meaningless words like "Item" or "attribute".

半步萧音过轻尘 2024-08-09 06:47:36

我觉得 XML 模式的设计有些品味。但您提供的两种替代方案存在明显差异。

示例 1:

<SomeBaseTag>   
    <Item1/>
    <Item1/>
    <Item2/>

示例 2:

<SomeBaseTag>   
    <Set1>
      <Item1/>
      <Item1/>
    </Set1>
    <Set2>
        <Item2/>
    </Set2>

第一个对我来说就像一个大容器,里面混合了 Item1 和 Item2 实体,(我认为)是随机或可能混合的顺序。第二个是具有两个子容器的容器,每个子容器包含一组特定类型的实体。

对于您的目的来说,这种差异可能并不重要。但在某些情况下它很重要,特别是当模式变得更加复杂时。有关说明,请参阅 John Saunders 在 WSDL 上的示例。

WSDL 是这样的:

<wsdl>
    <types/>

    <message/>
    <message/>
    <message/>

    <portTypes/>
    <bindings/>
    <service/>
</wsdl>

假设“根据喜好”省略了第一级容器。然后

<wsdl>
    <schema/>        
    <schema/>        
    <schema/>        
    <message/>
    <message/>
    <message/>

    <operation/>
    <operation/>
    <operation/>
    <binding/>
    <binding/>
    <binding/>
    <service/>
</wsdl>

,由于缺少 portType,将服务与一组操作关联起来并不容易。

I feel that there is some taste to the design of an XML schema. But there are distinct differences in the two alternatives you offered.

example 1:

<SomeBaseTag>   
    <Item1/>
    <Item1/>
    <Item2/>

example 2:

<SomeBaseTag>   
    <Set1>
      <Item1/>
      <Item1/>
    </Set1>
    <Set2>
        <Item2/>
    </Set2>

The first reads to me like a big container with a mix of Item1 and Item2 entities in it, in (I presume) a random or potentially mixed order. The second is a container with two subcontainers, each of which contain a set of one particular type of entity.

That difference maybe unimportant for your purposes. But in some cases it IS important, especially as the schema becomes more complicated. See the example from John Saunders on WSDL for an illustration.

WSDL is this:

<wsdl>
    <types/>

    <message/>
    <message/>
    <message/>

    <portTypes/>
    <bindings/>
    <service/>
</wsdl>

Suppose the first-level containers were omitted "as a matter of taste". You'd then have

<wsdl>
    <schema/>        
    <schema/>        
    <schema/>        
    <message/>
    <message/>
    <message/>

    <operation/>
    <operation/>
    <operation/>
    <binding/>
    <binding/>
    <binding/>
    <service/>
</wsdl>

At that point, lacking a portType, it's not easy to relate the service to a set of operations.

嘿咻 2024-08-09 06:47:36

这是一个品味问题。一般来说,此类数据最好以 YAML 或 JSON 等不太详细的格式表示。

编辑:例如,

SomeBaseTag:
    Item1s:
        - {Attr11: one, Attr12: two}
        - {Attr11: one, Attr12: two}
    Item2s:
        - {Attr21: one, Attr22: two}
        - {Attr21: one, Attr22: two}

It's a matter of taste. Generally, this sort of data is best represented in a less verbose format like YAML or JSON anyway.

EDIT: e.g.,

SomeBaseTag:
    Item1s:
        - {Attr11: one, Attr12: two}
        - {Attr11: one, Attr12: two}
    Item2s:
        - {Attr21: one, Attr22: two}
        - {Attr21: one, Attr22: two}
亽野灬性zι浪 2024-08-09 06:47:36

听起来您想要创建一些定义来衡量您的 XML。如果是这种情况,我建议您学习 XML Schema。它是定义 XML 结构的绝佳工具,甚至可以用来创建完整的语言。在这种情况下,它与 DOCTYPE 类似,只是从 Schema 生成的 XML 能够自我感知其自身的结构定义。如果 XML 包含的数据是根据包含它的元素的层次结构定义的,这一点就很重要。

就属性而言,一般规则是使用元素而不是属性来包含数据。使用 Schema 定义的元素可以指定数据类型约束以及前面描述的结构质量。使用属性的好处是简洁。属性可以轻松取代原本需要 2 到 4 层嵌套元素才能准确描述的属性。

It sounds like you are wanting to create some definition by which your XML can be measured against. If that is the case I would suggest you learn XML Schema. It is a fantastic tool for defining XML structures that can even be used to create entirely languages. In that case it is similar to DOCTYPE, except that XML generated from Schema is self-aware of its own structural definitions. That is important if data contained by the XML is defined from the hierarchy of elements containing it.

As far as attributes go the general rule is to use elements to contain data instead of attributes. Elements defined with Schema can specify data type constraints as well as structure qualities described previously. The benefit to using attributes is brevity. An attribute can easily take the place of what would otherwise require nested elements 2 to 4 deep to describe just as accurately.

花期渐远 2024-08-09 06:47:36

我同意每个人关于“品味问题”的观点,但我想添加另一件事来考虑。毕竟,XML 是一种标记语言,因此您可能需要考虑一下,如果去掉所有标签及其属性,它会剩下什么。

I agree with everyone on the "matter of taste" thing, but I'd add another thing to consideration. After all, XML is a markup language, so you may want to think what would be left of it if you stripped off all tags and their attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文