个人记事本的 XML 结构

发布于 2024-07-07 14:31:46 字数 614 浏览 6 评论 0原文

我正在做一个出于学习目的的个人组织者,并且我从未使用过 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 技术交流群。

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

发布评论

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

评论(5

灼痛 2024-07-14 14:31:46

你的方法对我来说已经足够了。 然而,我更喜欢子标签而不是属性,所以我的方式更像是:

<activity>
  <name>Mike's Birthday</name>
  <time>2100</time>
  <address>Mike's Place</address>
  <urgency>10</urgency>
  <description>activity description</description>
</activity>

但就像我说的,你的方式很好。

不过,快速问一下——为什么不使用数据库呢?

Your way is quite adequate to me. However, I prefer child tags to attributes, so my way would be more like:

<activity>
  <name>Mike's Birthday</name>
  <time>2100</time>
  <address>Mike's Place</address>
  <urgency>10</urgency>
  <description>activity description</description>
</activity>

But like I said, your way is just fine.

Quick question, though - why not a database?

情仇皆在手 2024-07-14 14:31:46

您可能天真地想到了这一点,但 XML 设计的主要特征是它针对按日期搜索进行了优化。 如果您的 XML 文档很大,并且您按日期进行大量搜索(我怀疑这是个人记事本中最常见的用例),那么这是一件好事。

执行此 XPath 模式:

/calendar/year[@value='2008']/month[@value='10']/day[@value='7']/activity

将比使用 Kev 更简单的扁平化组织所需的模式检查更少的节点:

/calendar/activity[@year='2008' and @month='10' and @day='7']

它基本上必须查看文档中的每个节点。

顺便请注意,我假设 monthday 属性是数字。 这很重要,因为您几乎肯定希望在某个时候对这些数据进行排序,除非您要维护文档中的排序顺序(我承认,可以对此进行论证),否则您'您希望这些属性采用易于排序的形式。

在这些属性中存储数字数据的方式保持一致也很重要。 (如果您想在会议中显得聪明,您可以说您正在建立数据类型的规范表示。)例如,如果您有时使用前导零而不是其他,那么这些 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:

/calendar/year[@value='2008']/month[@value='10']/day[@value='7']/activity

will examine many fewer nodes than will using the pattern you'd need to use with Kev's simpler flattened-out organization:

/calendar/activity[@year='2008' and @month='10' and @day='7']

which basically has to look at every node in the document.

Note, by the way, that I'm assuming that the month and day 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 a day attribute set to "07". (You can get around that by converting the attributes to numbers in your XPath using the number() function, but avoiding the problem in the first place is better.)

鸠魁 2024-07-14 14:31:46

您可以将该层次结构扁平化为:

<calendar>
    <activity
        id="123456"
        name="mike's birthday" 
        year="2008"
        month="10"
        day="16"
        time="21:00" 
        address="mike's apartment" 
        urgency="10">
            activity description.
        </activity>
</calendar>

或..

<calendar>
    <activity id="12345">
        <name>mike's birthday</name>
        <year>2008</year>
        <month>10<month>
        <day>16</day>
        <time>21:00</time>
              <urgency>10</urgency>
        <address>mike's apartment<address>
        <description>activity description.</description>
    </activity>
</calendar>

这会让执行 XPath 查询时的痛苦减轻一些。 我还添加了一个 id 属性,以便您可以唯一地标识一个活动。

You could flatten that hierarchy down to:

<calendar>
    <activity
        id="123456"
        name="mike's birthday" 
        year="2008"
        month="10"
        day="16"
        time="21:00" 
        address="mike's apartment" 
        urgency="10">
            activity description.
        </activity>
</calendar>

or..

<calendar>
    <activity id="12345">
        <name>mike's birthday</name>
        <year>2008</year>
        <month>10<month>
        <day>16</day>
        <time>21:00</time>
              <urgency>10</urgency>
        <address>mike's apartment<address>
        <description>activity description.</description>
    </activity>
</calendar>

It'd make life a bit less painful doing XPath queries. I also added an id attribute so you can uniquely identify an activity.

反差帅 2024-07-14 14:31:46

我认为你的结构适合你正在做的事情。

如果您打算部分地使用它来了解 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).

柠檬心 2024-07-14 14:31:46

可能值得一看 xCal,它是 iCalendar 标准,用于一些可能经过深思熟虑的想法。

It might be worth looking at xCal, an XML-compliant representation of the iCalendar standard, for some potentially well-thought-out ideas.

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