首先,我对Xquery完全陌生,对不起,我的问题非常愚蠢:/。
这只是我的XML的一部分:
<books>
<book id="b1">
<title>Set theory and the continuum problem</title>
<category>Mathematics</category>
<location>
<area>hall1</area>
<case>1</case>
<shelf>2</shelf>
</location>
<description>A lucid, elegant, and complete survey of set theory.</description>
<history>
<borrowed by="m4"/>
<borrowed by="m2" until="2018-04-05"/>
</history>
</book>
<book id="b2">
<title>Computational Complexity</title>
<isbn>978-0201-530-827</isbn>
<category>Computer Science</category>
<location>
<area>hall1</area>
<case>3</case>
<shelf>3</shelf>
</location>
<description>.</description>
</book>
<book id="b3">
<title>To mock a mockingbird</title>
<isbn>1-292-09761-2</isbn>
<category>Logic</category>
<category>Mathematics</category>
<location>
<area>hall1</area>
<case>1</case>
<shelf>3</shelf>
</location>
<description>.</description>
</book>
</books>
<libraryArea floor="1" name="hall1">
<bookcases>
<woodenCase id="3" shelves="5"/>
<woodenCase id="2" shelves="5"/>
<steelCase id="1" shelves="5"/>
</bookcases>
</libraryArea>
<libraryArea name="hall2">
<bookcases>
<lockedCase id="1" shelves="9"/>
<steelCase id="4" shelves="3"/>
<lockedCase id="3" shelves="2"/>
<steelCase id="2" shelves="4"/>
</bookcases>
</libraryArea>
<libraryArea name="hall3">
<bookcases>
<woodenCase id="2" shelves="3"/>
<steelCase id="1" shelves="8"/>
</bookcases>
</libraryArea>
<libraryArea name="archive">
<bookcases>
<lockedCase id="1" shelves="1"/>
</bookcases>
</libraryArea>
</library>
我想在自己的标签中列出特定的书名”
类似这样的东西:
<specialSteelCases>
<area name="hall1">
<steelCase id="1">
<bookCount>2</bookCount>
<book>Set theory and the continuum problem</book>
<book>To mock a mockingbird</book>
<shelves>5</shelves>
</steelCase>
</area>
</specialSteelCases>
但是我明白了,所有的书名都只有一个书本标签:
有没有办法将它们分开,所以每个书名都有自己的
这是我的Xquery:
<specialSteelCases>
{
for $a in //libraryArea
where $a/bookcases[count(woodenCase) > 1 ]
order by $a/@id
return
<area name="{$a/@name}">
{
for $s in $a//bookcases/steelCase
let $bbNew := (//books/book[location/case=$s/@id][location/area=$a/@name])
return
<steelCase id="{$s/@id}">
<bookCount>***code***</bookCount>
<book>{$bbNew/title/text()}</book>
<shelves>***code**</shelves>
</steelCase>
}
</area>
}
</specialSteelCases>
答案 0 :(得分:0)
使用let
子句选择相关书籍,然后对它们进行计数,并使用嵌套的for
表达式输出书名:
<specialSteelCases>
{
for $a in //libraryArea
where $a/bookcases[count(woodenCase) > 1 ]
order by $a/@id
return
<area name="{$a/@name}">
{
let $books := //books/book[location/case=$a//bookcases/steelCase/@id][location/area=$a/@name]
return
<steelCase id="{$a//bookcases/steelCase/@id}">
<bookCount>{count($books)}</bookCount>
{
for $title in $books/title
return
<book>{data($title)}</book>
}
</steelCase>
}
</area>
}
</specialSteelCases>
https://xqueryfiddle.liberty-development.net/jyyiVhw/1
如果您可以移至XQuery 3并进行简单分组:
for $book in //books/book
group by $case := //library/libraryArea[@name = $book/location/area]/bookcases/*[@id = $book/location/case]/local-name()
return
element {$case } {
<bookCount>{count($book)}</bookCount>
,
$book/title ! <book>{data()}</book>
}