这是好的 XML 吗?

发布于 2024-08-12 03:26:35 字数 838 浏览 7 评论 0原文

我正在为一位客户工作,他一直在其网站内容中使用这种 XML 结构:

<section title="title1" layoutType="VideoViewer" xmlPath="xml/ita/title1.xml" pageTitle="extended title" previewImg=""/>

<section title="another title" layoutType="TimeLine" xmlPath="xml/ita/timeline.xml" textFile="" pageTitle="extended title for timeline"></section>

我对 XML 很陌生,但这对我来说看起来不太好。除了关闭标签的不同方式(使用 /> 自动关闭或使用 显式关闭),我认为 XML 应该更像

<section> 
  <title>title1</title> 
  <layoutType>"VideoViewer" </layoutType>
  <xmlPath>xml/ita/title1.xml </xmlPath> 
  <pageTitle>extended title</pageTitle> 
  <previewImg/>
</section>

<section>
 ...etc etc..
</section>

What do you思考?

如果它们都可以,是否有“最佳实践”?

I'm working for a client who's been using this XML structure for his website's content:

<section title="title1" layoutType="VideoViewer" xmlPath="xml/ita/title1.xml" pageTitle="extended title" previewImg=""/>

<section title="another title" layoutType="TimeLine" xmlPath="xml/ita/timeline.xml" textFile="" pageTitle="extended title for timeline"></section>

I'm quite new to XML, but this doesn't look good to me. Aside from the different way of closing tags (autoclosing with /> or explicitly closed with </section>) I would have thought XML should be more like

<section> 
  <title>title1</title> 
  <layoutType>"VideoViewer" </layoutType>
  <xmlPath>xml/ita/title1.xml </xmlPath> 
  <pageTitle>extended title</pageTitle> 
  <previewImg/>
</section>

<section>
 ...etc etc..
</section>

What do you think?

If they are both OK, is there a "best practice"?

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

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

发布评论

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

评论(8

北座城市 2024-08-19 03:26:35

只要所有元素都包含在单个根元素中,实际上就没有任何问题。

两种类型的结束标签都是可以接受的,尽管我建议所有内部没有其他标签的元素使用自结束标签。

至于结构(标签与属性),这是一个观点问题,根据定义,这两种选择都不是更好。

As long as all of the elements are enclosed within a single root element, there's nothing actually wrong with it.

Either style of closing tag is acceptable, although I would recommend self-closing tags for all elements that don't have other tags inside of them.

As for the structure (tags vs attributes), that's a matter of opinion, neither option is better by definition.

放赐 2024-08-19 03:26:35

为什么您认为 XML 应该更像这样?第一个是以属性为中心的;你所提议的是以元素为中心的。虽然现在以元素为中心已经变得更常用,但以属性为中心的方法当然没有错,而且它可以使一些事情变得更清晰。

只要 XML 有命名空间,就没有问题。

Why do you think XML should be more like that? The first one is attribute-centric; what you're proposing is element-centric. Although nowadays element-centric has become more commonly used, there's certainly nothing wrong with the attribute-centric approach, and it can make some things clearer.

As long as the XML has got a namespace, then there's nothing wrong with it.

献世佛 2024-08-19 03:26:35

两者都是正确的。这只是个人品味问题。

我想花一些话来阐述我的观点。

当我学习 XML 时,我了解到,当您必须存储简单的属性时,属性比子元素更可取。
简单来说,我的意思是单个非结构化属性。

非结构化部分很简单:属性可以表示为字符串。
对于单个我的意思是你不能为属性分配超过一个值
但您可以重复相同的子元素以在父节点内创建集合/集合。

我想更清楚,但我的英语不太熟练,无法更好地解释我对这个问题的想法。

Both of them are correct. It is just a matter of personal taste.

I would spend some words on my point of view.

When I was taught XML I've learned that an attribute is preferable to a child element when you have to store a simple property.
For simple I mean a single, non structured property.

The non structured part is simple: an attribute can be represented as a string.
For single I mean that you can't assign more than a value to a property
but you can repeat the same child element to create a set/collection inside the father node.

I would like to be more clear but I'm not so proficient in English to explain better my thought on the subject.

奈何桥上唱咆哮 2024-08-19 03:26:35

我认为在该示例 XML 中使用属性没有任何问题。属性有许多元素没有的限制,但也有一些显着的优点。

首先,局限性:

  • 属性可能只包含简单的内容。
  • 属性名称在元素内必须是唯一的。
  • 根据定义,属性的顺序在 XML 中并不重要;您不能依赖 DOM 以任何特定顺序迭代或保存属性。
  • 属性不是节点,这意味着在 XPath 中访问它们的方式(例如,XSLT 身份转换中使用的模式 node() | @*)和 DOM 中存在一些怪异之处(例如,在 .NET 中,XmlAttribute 派生自 XmlNode,即使属性不是节点,并且 XmlAttributeCollection 不是派生自 XmlNodeList,即使它是 XmlNode 对象的列表 - 这样做有充分的理由,但如果您是新手,这会很令人困惑)。

显着优点:

  • 由于属性名称在元素内必须是唯一的,并且只能包含简单内容,因此设置和获取属性值的 DOM 方法比在元素内设置和获取值的 DOM 方法使用起来要简单得多。
  • 由于属性排序并不重要,因此创建和使用属性的代码无需关心其排序。
  • 由于属性可能只包含简单的内容,因此为它们编写模式定义会更容易。
  • 属性的标记比元素的标记更简洁。

属性非常干净地同构到地图/字典上。您可以使用属性作为由名称/值对组成的任何数据结构的持久形式,只要可以将名称限制为有效的 XML 名称并将值限制为文本 - 并且只要在构造数据结构时不这样做无论您以什么顺序填充它。

(这可能会导致大问题。在 WPF 中,您可以使用 XAML 中的属性来存储对象属性的值,这些值在设置时会产生副作用。这样做会导致以下错误:诊断起来非常困难 - 仅仅因为您在设置 SelectedItem 之前在 XAML 中设置了 Binding 并不意味着 XamlReader 会执行此操作当它构造 XAML 表示的对象时,如果它尝试在没有 Binding 的对象上设置 SelectedItem,则会引发异常。可以整天查看 XAML,却看不出为什么会发生这种情况。)

在代码中,使用属性的好处是不言而喻的。使用 XmlDocument DOM 设置和获取元素上 foo 属性的值(在 C# 中):

elm.SetAttribute("foo", value);

value = elm.GetAttribute("foo");

设置和获取 foo 的值code> element 在元素上:

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
if (fooElm == null)
{
   elm.OwnerDocument.CreateElement("foo");
   elm.AppendChild(fooElm);
}
fooElm.InnerText = value;

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
value = fooElm != null ?? fooElm.InnerText : "";

当然有更有效的方法来执行上述操作(编写辅助方法,使用 XDocument 而不是 XmlDocument,或者完全避免 DOM 并使用序列化),但使用元素本质上更加复杂。

在您的示例中,使用属性可能没问题;看起来它们代表了一个简单的名称/值对映射,顺序无关紧要。标题可能是个例外;如果您希望允许它们包含标记,您将会陷入困境。

编辑:

实际上,我相信XamlReader会按照属性在XAML中出现的顺序处理属性,因此如果您在之前设置Binding XAML 中的 SelectedItem 可能不会导致异常 - 只要 XamlReader 正在读取 XAML 的实际文本。实际上,风险在于读取和写入 XML 文档的工具会改变 XAML 文档中属性的顺序。 KaXaml 是否保留其编辑的 XAML 文档中的属性顺序?它不应该必须

无论如何,XAML 中的解决方案很简单:不要这样做:而是

<ListBox Binding="{...}" SelectedItem="..." ... />

这样做:

<ListBox>
   <ListBox.Binding>...</ListBox.Binding>
   <ListBox.SelectedItem>...</ListBox.SelectedItem>
   ...

I see nothing wrong with the use of attributes in that sample XML. Attributes have a number of limitations that elements don't, and a couple of significant advantages.

First, the limitations:

  • Attributes may only contain simple content.
  • Attribute names must be unique within an element.
  • By definition, the ordering of attributes is not significant in XML; you can't rely on the DOM to iterate over or save attributes in any specific order.
  • Attributes aren't nodes, which means that there are some eccentricities in how you access them in XPath (e.g., the pattern node() | @* used in the XSLT identity transform) and in the DOM (e.g. in .NET, XmlAttribute derives from XmlNode even though attributes aren't nodes, and XmlAttributeCollection doesn't derive from XmlNodeList even though it's a list of XmlNode objects - there are good reasons for this, but it's pretty confusing if you're new to it).

The significant advantages:

  • Because attribute names must be unique within an element, and can only contain simple content, the DOM methods to set and get attribute values are significantly simpler to use than the DOM methods for setting and getting values within elements.
  • Because attribute ordering is not significant, there's no need for code that creates and uses attributes to be concerned with their ordering.
  • Because attributes may only contain simple content, it's easier to write schema definitions for them.
  • The markup for attributes is more concise than the markup for elements.

Attributes isomorphize onto maps/dictionaries very cleanly. You can use attributes as the persistable form of any data structure consisting of name/value pairs, so long as it's OK to restrict the names to valid XML names and the values to text - and so long as when you construct the data structure it doesn't matter what order you populate it in.

(This can cause big problems. In WPF, you can use attributes in XAML to store the values of object properties that have side effects when they're set. Doing this makes for bugs that are freakishly difficult to diagnose - just because you set Binding in your XAML before you set SelectedItem doesn't mean that the XamlReader is going to do that when it constructs the object that your XAML represents, and if it tries to set SelectedItem on an object that doesn't have a Binding yet it'll throw an exception. You can look at your XAML all day long and not see why that's happening.)

In code, the benefits of using attributes are self-evident. To set and get the value of a foo attribute on an element using the XmlDocument DOM (in C#):

elm.SetAttribute("foo", value);

value = elm.GetAttribute("foo");

To set and get the value of a foo element on an element:

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
if (fooElm == null)
{
   elm.OwnerDocument.CreateElement("foo");
   elm.AppendChild(fooElm);
}
fooElm.InnerText = value;

XmlElement fooElm = (XmlElement)elm.SelectSingleNode("foo");
value = fooElm != null ?? fooElm.InnerText : "";

There are certainly more efficient ways to do the above (write helper methods, use XDocument instead of XmlDocument, or avoid the DOM completely and use serialization), but it's inherently more complex to work with elements.

In your example, it's probably OK to use attributes; it looks like they represent a simple name/value pair mapping with insignificant ordering. The exception might be the titles; if it's ever desirable to allow them to contain markup, you'll come to grief.

Edit:

Actually, I believe the XamlReader will process attributes in the order they appear in the XAML, so if you set Binding before SelectedItem in your XAML it probably won't cause an exception - so long as the XamlReader is reading from the actual text of the XAML. The risk is really that tools that read and write XML documents will alter the ordering of attributes in a XAML document. Does KaXaml preserve the order of attributes in the XAML documents it edits? It shouldn't have to.

At any rate, the solution in XAML is simple: instead of doing this:

<ListBox Binding="{...}" SelectedItem="..." ... />

do this:

<ListBox>
   <ListBox.Binding>...</ListBox.Binding>
   <ListBox.SelectedItem>...</ListBox.SelectedItem>
   ...
你在我安 2024-08-19 03:26:35

我认为你的方法更好。 xml 属性(如 title="blah")用于定义该特定标记的属性,但是当您进入更复杂的数据结构时,您需要更多地考虑对象,其中标记的属性实际上只是定义了额外的属性数据(如数据类型、格式等)。

您的样本正朝着正确的方向前进,但这完全取决于您选择如何对数据进行建模。

例如,像“类型”这样的东西可能是该部分的适当属性(如果不确切知道你的模型是什么,就很难知道),但我假设类似的东西是合适的

Your method is, in my opinion nicer. The xml attributes (like title="blah") are used to define attributes for that specific tag, but as you get into more complex data structures you want to think more in terms of objects, where the attributes of a tag really just define extra data (like dataTypes, formats, etc).

Your sample is headed in the right direction, but it's all about how you choose to model your data.

Something like "type" for example might be an appropriate attribute of the section (it's hard to know without knowing exactly what your'e modelling) but I'd assuming something like would be appropriate

许一世地老天荒 2024-08-19 03:26:35

当您公开承认您是 XML 新手时,我将为您指明 W3 Schools。至于你的问题,确实没有正确或错误的答案,两者都是合理的。这取决于您自己的具体偏好。

As you openly admit you are new to XML I will point you in the direction of W3 Schools. As for your question, there really is no right or wrong answer both are plausible. It is down to your own particular preference.

清醇 2024-08-19 03:26:35

自动关闭标签很棒,但我不认为使用所有属性会被视为“最佳实践”。属性比元素有更多的限制。

来自 w3schools

关于何时使用属性和何时使用元素没有规则。属性在 HTML 中很方便。在 XML 中,我的建议是避免它们。请改用元素。

...

使用属性的一些问题是:

* 属性不能包含多个值(元素可以)
* 属性不能包含树结构(元素可以)
* 属性不易扩展(以供将来更改)

属性难以阅读和维护。使用数据元素。使用与数据不相关的信息的属性。

使用元素。

The auto-closing tag is great, but I don't think using all attributes would be considered 'best practice'. Attributes have more limitations than elements.

From w3schools:

There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.

...

Some of the problems with using attributes are:

* attributes cannot contain multiple values (elements can)
* attributes cannot contain tree structures (elements can)
* attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

Use elements.

凹づ凸ル 2024-08-19 03:26:35

我个人设计 XML 方言的风格涉及属性和元素的混合,特别注重元素。我的一般经验法则是,任何元素每行不应超过 80 个字符(不包括前导空格),因此永远不应要求换行才能舒适地阅读。

我对简短项目使用的属性非常特定于它们所附加的元素,并且没有机会扩展到多个值。我倾向于创建布尔值、数值和枚举值属性。例如,对于图像参考元素,我将设置宽度、高度和格式属性。

我将元素用于较长的值(特别是那些可以想象为任意长度的值),特别是那些稍后可能扩展到更复杂结构的元素。例如,对于图像引用元素,我会将标题、说明、描述、URL 和其他可能很长的自由文本值设为图像引用元素的子元素。

XML 应该易于人类阅读。 XML 方言应该是健壮的,并且在修改时不会破坏系统。如有疑问,请使用元素。

My personal style of designing XML dialects involves a mix of both attributes and elements, heavy on the elements. My general rule of thumb is that no element should exceed 80 characters per line (not including leading whitespace), and thus should never require line wrapping to read comfortably.

I use attributes for brief items that are very specific to the element they're attached to and have no chance of being extended into multiple values. I tend to make booleans, numerical values, and enumerated values attributes. For example, for an image reference element, I'd make the width, height, and format attributes.

I use elements for longer values (particularly ones that conceivably could be of any length), particularly those that may be extended later into a more complex structure. For example, for an image reference element, I'd make the title, caption, description, URL, and other potentially lengthy free-text values child elements of the image reference element.

XML should be easily human-readable. XML dialects should be robust, and not break the system when modified. When in doubt, use elements.

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