我有以下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>
是否有任何阅读月份的功能?
答案 0 :(得分:1)
如果您要检查youth/dateofbirth
中的月份(请参阅上面的评论),您可以使用以下任一项:
month-from-date()
如果dateofbirth
为xs: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>