开发者

How to make correct syntax for XQuery Looping

开发者 https://www.devze.com 2023-04-11 03:01 出处:网络
Now I have a dita composite like: <root> <topic>....</topic> <topic>....</topic>

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                                     
               $topicW开发者_开发知识库ithPI := let $holder:=1
               return (
                      <topicref href="1.xml#Begin" navtitle="Begin" scope="local" type="topic"/>
                ),  

Could experts help? Thanks


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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号