Reputation: 6831
Now I have a dita composite like:
<root>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
</root>
And I simply need to write an xquery that basically will create a ditamap for each topic, so the resutling ditamap should look like:
<map>
<topicref>....</topicref>
<topicref>....</topicref>
<topicref>....</topicref>
<topicref>....</topicref>
<topicref>....</topicref>
</map>
My current Xquery isn't quite doing the right thing, it is able to catch each topic, but instead of creating one ditamp, it creates multiple ditamap, one for each topic:
$isFoSaved := for $b in $mapNode/*[local-name() = 'topic']
let
$topicWithPI := let $holder:=1
return (
<topicref href="1.xml#Begin" navtitle="Begin" scope="local" type="topic"/>
),
Could experts help? Thanks
Upvotes: 1
Views: 308
Reputation: 5071
If you'd like to preserve the hierarchy of nested topics it is a little bit more complex. I think it is best to use a recursive function for this:
declare function local:topicref($topics)
{
for $b in $topics
return
<topicref href="1.xml#Begin"
avtitle="Begin"
scope="local"
type="topic">{
local:topicref($b/*[local-name() = 'topic'])
}</topicref>
};
<map>{
let $mapNode :=
<root>
<topic><topic>....</topic></topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
</root>
return
local:topicref(
$mapNode/*[local-name() = 'topic']
)
}</map>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<map>
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic">
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/>
</topicref>
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/>
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/>
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/>
<topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/>
</map>
Upvotes: 1
Reputation: 5071
I can only see that you are embedding multiple flwor expressions.
Whenever you use $x := let $y ...
or $x := for $y ...
you start a new flwor expression which must be closed by a return
clause. Therefore, your code snipped is invalid/incomplete: You have two opened flwor expressions, but only one return
clause.
If you try to keep it flat it will be much easier.
For example:
<map>{
let $mapNode :=
<root>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
<topic>....</topic>
</root>
for $b in $mapNode/*[local-name() = 'topic']
return
<topicref href="1.xml#Begin"
avtitle="Begin"
scope="local"
type="topic"/>
}</map>
this query works on try.zorba-xquery.com, but I'm not sure if that's what you are looking for?
Upvotes: 1