简体   繁体   English

XQuery递归函数调用内部标签

[英]XQuery Recursive Function Call for Inner Tag

I'm trying to prepare an XML file to parse it JSON and its context is such as: 我正在尝试准备一个XML文件来解析JSON,其上下文如下:

<user_manual>
  <embed-language_part id="SL14686180">
    <language_part id="1" role="-" lang="de">
      <embed-user_manual_part id="1">
        <user_manual_part id="1" role="-" document-type="IU">
          <embed-chapter id="1">
            <?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a30660983"?>
            <chapter id="1" role="-" toctitle="yes" footrowtitle="no" type="security">
              <embed-title_module id="1">
                <title_module id="1" role="-">
                  <title id="1">Sicherheits- und Warnhinweise</title>
                </title_module>
              </embed-title_module>
              <embed-section id="1">
                <section id="1" footrowtitle="no" role="-" toctitle="yes">
                  <embed-section id="2">
                    <section id="2">
                      <embed-title_module id="2">
                        <title_module id="2" role="-">
                          <title id="2">Eisschale</title>
                        </title_module>
                      </embed-title_module>
                    </section>
                  </embed-section>
                  <embed-title_module id="3">
                    <title_module id="31" role="-">
                      <title id="3">Bevor Sie das Gerat in Betrieb nehmen</title>
                    </title_module>
                  </embed-title_module>
                </section>
              </embed-section>
            </chapter>
          </embed-chapter>
        </user_manual_part>
      </embed-user_manual_part>
    </language_part>
  </embed-language_part>
</user_manual>

I wrote an XQuery script regarding to my expectations first (assume that $doc is document, $matnr is 22333), 我首先编写了一个符合我期望的XQuery脚本(假设$doc是文档, $matnr是22333),

declare variable $doc external;
declare variable $matnr external;
<dmContainer>{
for $language in $doc/user_manual/embed-language_part/language_part
let $lang_code := data($language/@lang)
for $embed_chapter in $language/embed-user_manual_part/user_manual_part/embed-chapter
let $objectid := data($embed_chapter/processing-instruction('ecls-start-embedded-resource'))[1]  
let $fileattr := string($objectid)
let $filename := translate(substring-after($objectid,'resource='),'&quot;','')
let $postfix :=  substring(tokenize($filename,'_')[last()], 2)    

let $name := concat($matnr, '_', $postfix)                              
 return (element {$lang_code} {    
    attribute title {data($embed_chapter/chapter/embed-title_module/title_module/title)},
    attribute language {$lang_code},   
    attribute name {$name},        
    for $section in $embed_chapter/chapter/embed-section/section
        return <section title="{data($section/embed-title_module/title_module/title)}"></section>
})
}</dmContainer>

This returns: 返回:

<dmContainer>
   <de title="Sicherheits- und Warnhinweise" language="de" name="223333_30660983">
      <section title="Bevor Sie das Gerat in Betrieb nehmen" />
   </de>
</dmContainer>

Return contains the chapter element and its first section's title for the JSON but I have to add this one to all sections (the sections included by sections too). 返回包含JSON的Chapter元素及其第一部分的标题,但我必须将此元素添加到所有部分(各部分也包括的部分)中。

According to the input XML the sections can have another sections (one or more) recursively. 根据输入的XML,这些部分可以递归地包含另一个(一个或多个)部分。 You can look the example by searching it deeply. 您可以通过深入搜索示例来查找它。 The question is that how i can add these sections to my output with a proper recursive way(i mean not just the child level one level two children are included too) , i searched for some examples recursive functions of XQuery but i couldn't get any one. 问题是我如何以适当的递归方式将这些部分添加到我的输出中(我的意思是不仅也包括一级子级,也包括二级子级),我搜索了XQuery递归函数的一些示例,但是我无法获得任何人。

Expected output: 预期产量:

 <dmContainer>
   <de title="Sicherheits- und Warnhinweise" language="de" name="223333_30660983">
      <section title="Bevor Sie das Gerat in Betrieb nehmen">
      <section title="Eisschale"/>
      </section>
   </de>
</dmContainer>

How can I get all sections? 如何获得所有部分?

If you just want all sections in that chapter (in document order), go with the descendant-or-self-step , abbreviated // . 如果只需要该章中的所有部分(按文档顺序),请使用descendant-or-self-step (缩写为//

for $section in $embed_chapter/chapter//embed-section/section
    return <section title="{data($section/embed-title_module/title_module/title)}"

If document order doesn't work out for you (eg., first the title of the current section, then all subsections on the current level, no matter if they actually precede the title), you will have to write your own function traversing the tree: a function that gets the current section, returns the title, and recursively calls itself for the direct subsections (if there are any). 如果文档顺序不适合您(例如,首先是当前节的标题,然后是当前级别上的所有子节,无论它们实际上是否在标题之前),您都必须编写自己的遍历tree:获取当前节,返回标题并递归调用直接子节(如果有)的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM