如何使用XQuery从输入的XML中删除重复的节点

huangapple go评论58阅读模式
英文:

How to drop the duplicate node from input xml using xquery

问题

以下是XQuery中的XML输入,我想要删除重复的元素

let $doc := <a>
    <b>
        <b1/>
    </b>
    <c>
        <c1/>
        <c1/>
    </c>
    <b>
        <b1/>
    </b>
    <d>
        <d1>
            <d11/>     
            <d11/>
        </d1>
    </d>
</a>

预期输出:我的预期输出如下,意思是从输入中删除重复的节点

<a>
    <b>
        <b1/>
    </b>
    <c>
        <c1/>
    </c>
    <d>
        <d1>
            <d11/>
        </d1>
    </d>
</a>

谢谢。

英文:

Below are the Xml input in xquery, i want to drop the duplicate element

    let $doc := <a>
    	<b>
    		<b1/>
    	</b>
    	<c>
    		<c1/>
    		<c1/>
    	</c>
    	<b>
    		<b1/>
    	</b>
    	<d>
    		<d1>
    			<d11/>     
    			<d11/>
    		</d1>
    	</d>
    </a>

return distinct-values($doc)

expected output: my expected output is below means drop the duplicate node from input

<a>
	<b>
		<b1/>
	</b>
	<c>
		<c1/>
	</c>
	<d>
		<d1>
			<d11/>
		</d1>
	</d>
</a>

Thanks..

答案1

得分: 1

distinct-values函数创建一个包含不同原子值(如字符串、数字、日期)的序列。要识别和消除重复节点,首先需要理解如何定义一个节点是另一个节点的重复项,无论是仅基于子元素结构还是值。

您似乎还希望递归消除重复项,因此,为了给您一个示例,使用了一个递归函数,该函数将所有子元素按照调用XQuery 3中的serialize函数的结果进行分组。以下是示例代码:

declare function local:eliminate-duplicates($element as element())
{
    element { $element => node-name() }
    {
        for $child at $child-pos in $element/*
        group by $xml := serialize($child)
        order by head($child-pos)
        return
            $child => head() => local:eliminate-duplicates()
    }
};

let $a := <a>
        <b>
            <b1/>
        </b>
        <c>
            <c1/>
            <c1/>
        </c>
        <b>
            <b1/>
        </b>
        <d>
            <d1>
                <d11/>
                <d11/>
            </d1>
        </d>
    </a>
return 
    local:eliminate-duplicates($a)

结果可以在以下链接中找到:https://xqueryfiddle.liberty-development.net/gWmuPrX/2

结果是:

<a>
   <b>
      <b1/>
   </b>
   <c>
      <c1/>
   </c>
   <d>
      <d1>
         <d11/>
      </d1>
   </d>
</a>
英文:

The function distinct-values creates a sequence of distinct atomic values like strings, numbers, dates. To identify and eliminate duplicate nodes, we first need to understand how you define that a node is a duplicate of another, whether that is based solely on the child element structure or the values as well.

You also seem to want to eliminate duplicates recursively, so to give you an example that uses a recursive function that groups all children by the result of calling the serialize function in XQuery 3 here is a sample:

declare function local:eliminate-duplicates($element as element())
{
    element { $element =&gt; node-name() }
    {
        for $child at $child-pos in $element/*
        group by $xml := serialize($child)
        order by head($child-pos)
        return
            $child =&gt; head() =&gt; local:eliminate-duplicates()
    }
};

let $a := &lt;a&gt;
        &lt;b&gt;
            &lt;b1/&gt;
        &lt;/b&gt;
        &lt;c&gt;
            &lt;c1/&gt;
            &lt;c1/&gt;
        &lt;/c&gt;
        &lt;b&gt;
            &lt;b1/&gt;
        &lt;/b&gt;
        &lt;d&gt;
            &lt;d1&gt;
                &lt;d11/&gt;     
                &lt;d11/&gt;
            &lt;/d1&gt;
        &lt;/d&gt;
    &lt;/a&gt;
return 
    local:eliminate-duplicates($a)

Result at https://xqueryfiddle.liberty-development.net/gWmuPrX/2 is

&lt;a&gt;
	&lt;b&gt;
		&lt;b1/&gt;
	&lt;/b&gt;
	&lt;c&gt;
		&lt;c1/&gt;
	&lt;/c&gt;
	&lt;d&gt;
		&lt;d1&gt;
			&lt;d11/&gt;
		&lt;/d1&gt;
	&lt;/d&gt;
&lt;/a&gt;

huangapple
  • 本文由 发表于 2020年1月3日 14:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定