xml - xQuery 更改节点层次结构(从一个节点中删除子节点并将其作为兄弟节点返回)

标签 xml xpath recursion xml-parsing xquery

我有一个如下所示的 xml 文档:

<dict>
    <word>
        <sense>
            <definition> This is the text of the definition. 
                <example>
                    <quote>This is the text of an example.</quote>
                </example>
                <source>
                    <place>This is the name of the place recorded</place>
                </source>. 
            </definition>
        </sense>
    </word>
</dict>

我需要使用 xQuery 来转换它,使得 <example>它的 child 成为 <definition> 的 sibling ,而<source>它的 child 应该成为 <example> 的 child 。换句话说,我需要这个作为输出:

<word>
    <sense>
        <definition> This is the text of the definition. </definition>
        <example>
            <quote>This is the text of an example.</quote>
            <source>
                <place>This is the name of the place recorded.</place>
            </source>
        </example>
    </sense>
</word>

正如您所看到的,原始 <source> 后面的句号也存在问题。需要成为 <place> 关闭之前的最后一个字符串的元素.

我已经创建了一个 xQuery 文件并弄清楚了如何从层次结构中删除元素,但我在递归处理节点和在同一函数中添加新元素时遇到了麻烦。

xquery version "3.0";
declare namespace saxon="http://saxon.sf.net/";
declare option saxon:output "indent=yes";
declare option saxon:output "saxon:indent-spaces=3";


declare function local:test($node as item()*) as item()* {
    typeswitch($node)
        case text() return normalize-space($node)
        case element(word) return <word>{local:recurse($node)}</word>
        case element(dict) return <dict>{local:recurse($node)}</dict>
        case element(sense) return <sense>{local:recurse($node)}</sense>
        case element(definition) return local:definition($node)
        case element(example) return local:example($node)
        case element(source) return local:source($node)
        case element(place) return <place>{local:recurse($node)}</place>
        default return local:recurse($node)
};

declare function local:definition($nodes as item()*) as item()*{

(: here I need to process children of definition - except <source> and its
children will become children of <example>; and <example> should be returned 
as a next sibling of definition. THIS IS THE PART THAT I DON'T KNOW HOW TO DO :)

<definition>
{
 for $node in $nodes/node()
    return
        local:test($node)
}
</definition>

};

declare function local:example($node as item()*) as item()* {
(: here i am removing <example> because I don't want it to be a child
of <definition> any more. THIS BIT WORKS AS IT SHOULD :)

if ($node/parent::definition) then ()
   else <example>{$node/@*}{local:recurse($node)}</example>
};

declare function local:source($node as item()*) as item()* {
(: here i am removing <source> because I don't want it to be a child
of <definition> any more.  :)

if ($node/parent::definition) then ()
   else <example>{$node/@*}{local:recurse($node)}</example>
};


declare function local:recurse($nodes as item()*) as item()* {
    for $node in $nodes/node()
    return
        local:test($node)
};


local:test(doc("file:test.xml"))

这应该不是一件非常困难的事情,但我在 xQuery 如何处理此类问题方面遇到了概念上的困难。我将非常感谢您的帮助。

XSLT 不是一个选项。

最佳答案

为了完整起见,这里提供了一个仅包含一个递归函数的递归 XQuery 1.0 解决方案。我同意 Jens 的观点,即给定的示例可以很容易地处理,无需递归,但如果实际示例更大,并且您没有 XQuery Update 可供使用,您可以尝试如下操作:

declare function local:recurse($node as item()*) as item()* {
    typeswitch($node)
        case text()
            return normalize-space($node)
        case element(definition)
            return element {node-name($node)} {
                $node/@*,
                local:recurse($node/node() except $node/(example|source))
            }
        case element(sense)
            return element {node-name($node)} {
                $node/@*,
                local:recurse($node/node()),
                <example>{
                    $node/definition/example/@*,
                    $node/definition/example/node(),
                    $node/definition/source
                }</example>
            }
        case element()
            return element {node-name($node)} {
                $node/@*,
                local:recurse($node/node())
            }
        default return $node
};


let $xml :=
<dict>
    <word>
        <sense>
            <definition> This is the text of the definition. 
                <example>
                    <quote>This is the text of an example.</quote>
                </example>
                <source>
                    <place>This is the name of the place recorded.</place>
                </source>
            </definition>
        </sense>
    </word>
</dict>
return local:recurse($xml)

呵呵!

关于xml - xQuery 更改节点层次结构(从一个节点中删除子节点并将其作为兄弟节点返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24235325/

相关文章:

xml - 两个 XML 的通用 XSD

java - Android:将自定义 View 添加到 XML

php强制下载xml

java - 如果 Java 中的 XML 中不存在节点或子节点,如何 XPath 返回空字符串

vim - 如何在 VIM 中递归使用 Global?

java - Java 中的 XML 模板

c# - 使用 Html Agility Pack,在循环中选择当前元素 (XPATH)

php - 优化远程页面检索和解析

javascript - 递归创建 Promise 时如何避免内存泄漏?

javascript - 递归函数过早退出for循环