XQuery Month函数

时间:2012-06-14 20:01:01

标签: xml xquery

我有以下DTD:

<!DOCTYPE guests [
<!ELEMENT Guest (Gname, youth+, car+)>
<!ELEMENT youth (Yname, dateofbirth)>
<!ELEMENT car (carType, excursion+)>
<!ELEMENT excursion (week, location)>
<!ELEMENT Gname (#PCDATA)>
<!ELEMENT Yname (#PCDATA)>
<!ELEMENT dateofbirth (#PCDATA)>
<!ELEMENT carType (#PCDATA)>
<!ELEMENT week (#PCDATA)>
<!ELEMENT location (#PCDATA)>
>]

我希望使用XQuery查找所有拥有8月生日的年轻客人的姓名。这就是我提出的:

for $x in Guest/youth
let $GuessN := $x/@Gname
where $x/month:=8
return <Gname> $GuessN </Gname>

For $x in Guest/youth[month=8]
Return <Gname>$x/@Gname </Gname>

是否有任何阅读月份的功能?

1 个答案:

答案 0 :(得分:1)

如果您要检查youth/dateofbirth中的月份(请参阅上面的评论),您可以使用以下任一项:

{p> month-from-date()如果dateofbirthxs:date
如果month-from-dateTime()dateofbirth

,则xs:dateTime

xs示例:日期:2012-06-14
xs:dateTime:2012-06-14T22:34:52.682-06:00

的示例

如果dateofbirth可投弃为xs:date或xs:dateTime,则必须手动提取月份(最有可能使用tokenize())。

以下是使用month-from-date()的示例。我必须添加guests的元素声明,我必须正确关闭内部子集(]>而不是>])。

XML输入

<!DOCTYPE guests [
<!ELEMENT guests (Guest+)>
<!ELEMENT Guest (Gname, youth+, car+)>
<!ELEMENT youth (Yname, dateofbirth)>
<!ELEMENT car (carType, excursion+)>
<!ELEMENT excursion (week, location)>
<!ELEMENT Gname (#PCDATA)>
<!ELEMENT Yname (#PCDATA)>
<!ELEMENT dateofbirth (#PCDATA)>
<!ELEMENT carType (#PCDATA)>
<!ELEMENT week (#PCDATA)>
<!ELEMENT location (#PCDATA)>
]>
<guests>
    <Guest>
        <Gname>Guest 1</Gname>
        <youth>
            <Yname>Youth A</Yname>
            <dateofbirth>2012-06-14</dateofbirth>
        </youth>
        <car>
            <carType/>
            <excursion>
                <week/>
                <location/>
            </excursion>
        </car>
    </Guest>
    <Guest>
        <Gname>Guest 2</Gname>
        <youth>
            <Yname>Youth B</Yname>
            <dateofbirth>2012-08-14</dateofbirth>
        </youth>
        <car>
            <carType/>
            <excursion>
                <week/>
                <location/>
            </excursion>
        </car>
    </Guest>
    <Guest>
        <Gname>Guest 3</Gname>
        <youth>
            <Yname>Youth C</Yname>
            <dateofbirth>2000-12-25</dateofbirth>
        </youth>
        <car>
            <carType/>
            <excursion>
                <week/>
                <location/>
            </excursion>
        </car>
    </Guest>
</guests>

<强>的XQuery

<guests>
{
for $guest in /guests/Guest[month-from-date(youth/dateofbirth) = 8]
    return $guest/Gname
}
</guests>

XML输出

<guests>
   <Gname>Guest 2</Gname>
</guests>

真的可以将XQuery简化为:

<guests>{/guests/Guest[month-from-date(youth/dateofbirth) = 8]/Gname}</guests>