我正在尝试准备一个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>
我首先写了一个关于我的期望的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='),'"','')
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>
返回:
<dmContainer>
<de title="Sicherheits- und Warnhinweise" language="de" name="223333_30660983">
<section title="Bevor Sie das Gerat in Betrieb nehmen" />
</de>
</dmContainer>
返回包含章节元素及其第一部分的JSON标题,但我必须将此部分添加到所有部分(部分也包含的部分)。
根据输入XML,这些部分可以递归地具有另一个部分(一个或多个)。您可以通过深入搜索来查看示例。问题是我如何以适当的递归方式将这些部分添加到我的输出中(我的意思不仅仅包括一级二级子级的子级别),我搜索了XQuery的递归函数的一些示例,但我无法做到这一点。得到任何一个。
预期产出:
<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>
我如何获得所有部分?
答案 0 :(得分:0)
如果您只想要该章中的所有部分(按文档顺序排列),请使用descendant-or-self-step
缩写为//
。
for $section in $embed_chapter/chapter//embed-section/section
return <section title="{data($section/embed-title_module/title_module/title)}"
如果文件顺序没有为您服务(例如,首先是当前部分的标题,那么当前级别的所有子部分,无论它们是否实际位于标题之前),您将不得不写你自己的函数遍历树:一个获取当前部分的函数,返回标题,并递归调用自己的直接子部分(如果有的话)。