这是好的 XML 吗?
我正在为一位客户工作,他一直在其网站内容中使用这种 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只要所有元素都包含在单个根元素中,实际上就没有任何问题。
两种类型的结束标签都是可以接受的,尽管我建议所有内部没有其他标签的元素使用自结束标签。
至于结构(标签与属性),这是一个观点问题,根据定义,这两种选择都不是更好。
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.
为什么您认为 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.
两者都是正确的。这只是个人品味问题。
我想花一些话来阐述我的观点。
当我学习 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.
我认为在该示例 XML 中使用属性没有任何问题。属性有许多元素没有的限制,但也有一些显着的优点。
首先,局限性:
node() | @*
)和 DOM 中存在一些怪异之处(例如,在 .NET 中,XmlAttribute
派生自XmlNode
,即使属性不是节点,并且XmlAttributeCollection
不是派生自XmlNodeList
,即使它是XmlNode
对象的列表 - 这样做有充分的理由,但如果您是新手,这会很令人困惑)。显着优点:
属性非常干净地同构到地图/字典上。您可以使用属性作为由名称/值对组成的任何数据结构的持久形式,只要可以将名称限制为有效的 XML 名称并将值限制为文本 - 并且只要在构造数据结构时不这样做无论您以什么顺序填充它。
(这可能会导致大问题。在 WPF 中,您可以使用 XAML 中的属性来存储对象属性的值,这些值在设置时会产生副作用。这样做会导致以下错误:诊断起来非常困难 - 仅仅因为您在设置
SelectedItem
之前在 XAML 中设置了Binding
并不意味着XamlReader
会执行此操作当它构造 XAML 表示的对象时,如果它尝试在没有Binding
的对象上设置SelectedItem
,则会引发异常。可以整天查看 XAML,却看不出为什么会发生这种情况。)在代码中,使用属性的好处是不言而喻的。使用
XmlDocument
DOM 设置和获取元素上foo
属性的值(在 C# 中):设置和获取
foo
的值code> element 在元素上:当然有更有效的方法来执行上述操作(编写辅助方法,使用
XDocument
而不是XmlDocument
,或者完全避免 DOM 并使用序列化),但使用元素本质上更加复杂。在您的示例中,使用属性可能没问题;看起来它们代表了一个简单的名称/值对映射,顺序无关紧要。标题可能是个例外;如果您希望允许它们包含标记,您将会陷入困境。
编辑:
实际上,我相信
XamlReader
会按照属性在XAML中出现的顺序处理属性,因此如果您在之前设置
可能不会导致异常 - 只要Binding
XAML 中的 SelectedItemXamlReader
正在读取 XAML 的实际文本。实际上,风险在于读取和写入 XML 文档的工具会改变 XAML 文档中属性的顺序。 KaXaml 是否保留其编辑的 XAML 文档中的属性顺序?它不应该必须。无论如何,XAML 中的解决方案很简单:不要这样做:而是
这样做:
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:
node() | @*
used in the XSLT identity transform) and in the DOM (e.g. in .NET,XmlAttribute
derives fromXmlNode
even though attributes aren't nodes, andXmlAttributeCollection
doesn't derive fromXmlNodeList
even though it's a list ofXmlNode
objects - there are good reasons for this, but it's pretty confusing if you're new to it).The significant advantages:
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 setSelectedItem
doesn't mean that theXamlReader
is going to do that when it constructs the object that your XAML represents, and if it tries to setSelectedItem
on an object that doesn't have aBinding
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 theXmlDocument
DOM (in C#):To set and get the value of a
foo
element on an element:There are certainly more efficient ways to do the above (write helper methods, use
XDocument
instead ofXmlDocument
, 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 setBinding
beforeSelectedItem
in your XAML it probably won't cause an exception - so long as theXamlReader
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:
do this:
我认为你的方法更好。 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
当您公开承认您是 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.
自动关闭标签很棒,但我不认为使用所有属性会被视为“最佳实践”。属性比元素有更多的限制。
来自 w3schools:
使用元素。
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:
Use elements.
我个人设计 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.