XML 记录计数属性或元素
虽然我对 XML 和 XML Schema 了解一点,但我已经很久没有使用它了,而且在使用它方面也从来没有特别擅长。 因此,如果您能在以下方面得到一些帮助,我们将不胜感激。
我们当前收到分隔数据文件,并希望切换到通过 XML 模式验证的 XML 文件。 当前分隔文件包含包含记录计数的尾部记录。
例如:TRL0000155
在 XML 版本中,是否应该将此记录计数作为主
元素的属性
<data_file record_count="155">
<record>
record XML...
</record>
<record>
record XML...
</record>
...
</data_file>
或作为
<data_file>
<record_count>155</record_count>
<record>
record XML...
</record>
<record>
record XML...
</record>
...
<data_file>
或者也许我的做法完全错误,您可以给我一些有关完全替换记录计数的正确方法的提示。
我确实认识到元素和属性之间的区别,但在这种情况下,我希望这是许多人以前遇到过的问题,并且可以很好地解释为什么其中一个是首选解决方案。
FWIW 我更倾向于属性解决方案,因为计数是有关
元素的元数据,但很高兴遵循更多专家的指导。
提前致谢...
Though I know a little about XML and XML Schema I haven't used it in ages and have never been particularly competent in its use.
Therefore a little help with the following would be really appreciated.
We currently receive delimited data files and would like to switch to XML files validated by an XML Schema.
The current delimited file contains a trailer record that includes a record count.
e.g.: TRL0000155
In the XML version, should this record count be included as an attribute of the main <data_file>
element
<data_file record_count="155">
<record>
record XML...
</record>
<record>
record XML...
</record>
...
</data_file>
or as a child element of <data_file>
<data_file>
<record_count>155</record_count>
<record>
record XML...
</record>
<record>
record XML...
</record>
...
<data_file>
or perhaps I am going about it all wrong and you could give me a tip on the correct method for replacing the record count entirely.
I do recognise the difference between elements and attributes but in this case I am hoping it's an issue that many have come across before and can offer a good explaination of why one is the preferred solution.
FWIW I am leaning more towards the attribute solution as the count is metadata about the <data_file>
element but am happy to follow more expert instruction.
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于每条记录都有一个
元素,因此从技术上讲,记录计数不需要明确。当然,您仍然可以拥有它,例如它有助于提高性能。但另一方面,它引入了必须与
元素的数量保持一致的问题。如果您确实决定存储记录计数,那么您的两种方法(属性和元素)都可以。主要区别在于,当您(如果)构建文件的字符串内容时,属性将被忽略,而元素中的文本节点则不会。因此,计数会出现在第二种情况下,但不会出现在第一种情况下。
如果您可以将 XPath 应用于 XML 文件,那么确定记录编号就很容易;这就是
count(/data_file/record)
。在这种情况下,实际上没有必要显式存储该值。Since you have a
<record>
element for each record, there's technically no need for the record count to be explicit. You can still have it, of course, for example if it helps performance. But on the other hand, it introduces the issue that it has to be kept consistent with the number of<record>
elements.If you do decide to store the record count, both of your approaches (attribute and element) are ok. The main difference is that when you (if you) build the string content of the file, attributes are ignored, while the text nodes in the elements are not. So the count would show up in the second case but not in the first.
If you can apply XPath to the XML file, determining the record cound is easy; that would just be
count(/data_file/record)
. In that case there's really no need to store the value explicitly.属性或元素...通常您会发现它们是完全可以互换的。主要区别在于元素可以重复,而元素上只能出现一个具有该限定名称的属性。
从语义上讲,当您想要关于元素或结构时,我更喜欢使用属性。名字本身就说明了这一点:它是包含元素的一个属性。由于您打算说明有关 data_file 的信息,即记录数,因此使用属性很有意义。
除此之外,这是我在尝试做出决定时使用的其余限定条件:
Attributes or elements... often you'll find they're completely interchangeable. A major difference is that elements can repeat, while only one attribute with that qualified name may appear on an element.
Semantically, I prefer using attributes when you want to say something about an element or structure. The name itself says it: it's an attribute of the containing element. Since you intend to say something about your data_file, namely the number of records, using an attribute makes a lot of sense.
Other than that, here's the remaining qualifications I use when trying to make a decision: