个人记事本的 XML 结构
我正在做一个出于学习目的的个人组织者,并且我从未使用过 XML,所以我不确定我的解决方案是否是最好的。 以下是我附带的 XML 文件的基本结构:
<calendar>
<year value="2008">
<month value="october">
<day value="16">
<activity name="mike's birthday" time="21:00" address="mike's apartment" urgency="10">
activity description.
</activity>
</day>
</month>
</year>
</calendar>
紧急属性的范围应为 1 到 10。
我在谷歌上进行了快速搜索,但找不到一个很好的例子。 也许这不是最好的解决方案,我想知道它是否足够。 如果有任何相关性的话,我正在用 PHP 做这个应用程序。
I'm doing a personal organizer for learning purposes, and i've never worked with XML so i'm not sure if my solution is the best. Here's the basic structure for the XML file i came with:
<calendar>
<year value="2008">
<month value="october">
<day value="16">
<activity name="mike's birthday" time="21:00" address="mike's apartment" urgency="10">
activity description.
</activity>
</day>
</month>
</year>
</calendar>
The urgency attribute should be on a scale of 1 to 10.
I did a quick search on google and couldn't find a good example. Maybe that's not the best solution, and i'd like to know if its adequate. I'm doing the application in PHP if that has any relevance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的方法对我来说已经足够了。 然而,我更喜欢子标签而不是属性,所以我的方式更像是:
但就像我说的,你的方式很好。
不过,快速问一下——为什么不使用数据库呢?
Your way is quite adequate to me. However, I prefer child tags to attributes, so my way would be more like:
But like I said, your way is just fine.
Quick question, though - why not a database?
您可能天真地想到了这一点,但 XML 设计的主要特征是它针对按日期搜索进行了优化。 如果您的 XML 文档很大,并且您按日期进行大量搜索(我怀疑这是个人记事本中最常见的用例),那么这是一件好事。
执行此 XPath 模式:
将比使用 Kev 更简单的扁平化组织所需的模式检查更少的节点:
它基本上必须查看文档中的每个节点。
顺便请注意,我假设
month
和day
属性是数字。 这很重要,因为您几乎肯定希望在某个时候对这些数据进行排序,除非您要维护文档中的排序顺序(我承认,可以对此进行论证),否则您'您希望这些属性采用易于排序的形式。在这些属性中存储数字数据的方式保持一致也很重要。 (如果您想在会议中显得聪明,您可以说您正在建立数据类型的规范表示。)例如,如果您有时使用前导零而不是其他,那么这些 XPath 模式都不会可靠地工作,因为
@day='7'
与设置为"07"
的day
属性不匹配。 (您可以通过使用number()
函数将属性转换为 XPath 中的数字来解决这个问题,但首先避免这个问题更好。)You may have arrived at this naively, but the primary feature of your XML design is that it is optimized for searching by date. If your XML document is large, and you do a lot of searching by date (which I suspect is the most common use case in a personal organizer), this is a good thing.
Executing this XPath pattern:
will examine many fewer nodes than will using the pattern you'd need to use with Kev's simpler flattened-out organization:
which basically has to look at every node in the document.
Note, by the way, that I'm assuming that the
month
andday
attributes are numeric. This is important because you'll almost certainly want to sort this data at some point, and unless you're going to maintain the sort order in the document (which, I'll admit, an argument can be made for), you'll want those attributes in a form that it's easy to sort them in.It's also important that you're consistent in how you store numeric data in those attributes. (If you want to sound smart in meetings, you can say that you're establishing canonical representations of your data types.) If you use leading zeroes some times and not others, for instance, none of those XPath patterns will work reliably, because
@day='7'
won't match aday
attribute set to"07"
. (You can get around that by converting the attributes to numbers in your XPath using thenumber()
function, but avoiding the problem in the first place is better.)您可以将该层次结构扁平化为:
或..
这会让执行 XPath 查询时的痛苦减轻一些。 我还添加了一个 id 属性,以便您可以唯一地标识一个活动。
You could flatten that hierarchy down to:
or..
It'd make life a bit less painful doing XPath queries. I also added an id attribute so you can uniquely identify an activity.
我认为你的结构适合你正在做的事情。
如果您打算部分地使用它来了解 XML,您可以考虑混合使用属性和元素,以便练习使用每个属性和元素的集合。 一旦您对 XML 更加熟悉,您可能会开始定义规则,用于确定哪些属性成为属性以及哪些属性成为元素。
使用正确的代码,您可以在 XML 文件和数据库表之间来回移动信息。 您还可以开始学习XSL,这样您就可以练习在不改变原始内容的情况下移动东西XML 文件(或者,一旦数据位于表中,甚至没有原始 XML 文件)。
I think your structure will be fine for what you are doing.
If you're planning to use this partly to learn about XML, you might consider using a mix of attributes and elements so that you get practice working with collections of each. Once you're more comfortable with XML, you will probably start to define rules that you'll use to determine which properties become attributes and which properties become elements.
With the right code, you can move information back and forth between XML files and database tables. You could also start learning XSL so that you can practice moving things around without changing the original XML file (or, once the data is in a table, not even have an original XML file).
可能值得一看 xCal,它是 iCalendar 标准,用于一些可能经过深思熟虑的想法。
It might be worth looking at xCal, an XML-compliant representation of the iCalendar standard, for some potentially well-thought-out ideas.