XQuery和distinct-values

时间:2015-04-12 11:48:09

标签: xml xquery distinct-values

我有这个XML:

<university>
<departaments>
    <departament sl="AA">
        <subject>Numbers</subject>
        <categories>
            <categorie>
                <name type="one">Mathematics</name>
            </categorie>
            <categorie>
                <name type="one">Physics</name>
            </categorie>
            <categorie>
                <name type="two">Other</name>
            </categorie>
        </categories>
    </departament>
    <departament sl="BB">
        <subject>Letters</subject>
        <categories>
            <categorie>
                <name type="one">Philosophy</name>
            </categorie>
            <categorie>
                <name type="two">Language</name>
            </categorie>
            <categorie>
                <name type="two">Other</name>
            </categorie>
        </categories>
    </departament>
</departaments>
</university>

我需要获取具有其属性的名称,按长度排序并且没有重复名称。即,这个xml:

<name type="two">Other</name>
<name type="one">Physics</name>
<name type="two">Language</name>
<name type="one">Philosophy</name>
<name type="one">Mathematics</name>

我有这段代码,但不行。

for $name in distinct-values(doc ("uni.xml")//categorie/name)
order by string-length($name)
return $name

有人可以帮助我吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试:

for $name in distinct-values(doc("uni.xml")//categorie/name)
order by string-length($name)
return (doc("uni.xml")//categorie/name[.=$name])[1]