Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xquery 我希望在一个xml文件中返回具有相同文本值的重复节点_Xquery_Marklogic - Fatal编程技术网

Xquery 我希望在一个xml文件中返回具有相同文本值的重复节点

Xquery 我希望在一个xml文件中返回具有相同文本值的重复节点,xquery,marklogic,Xquery,Marklogic,下面是我的xml: 文件名:relation.xml <relations> <relation> <normativelyReferencedBy>00-3880945351</normativelyReferencedBy> <normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy> </relation> <rel

下面是我的xml:

文件名:relation.xml

    <relations>
<relation>
<normativelyReferencedBy>00-3880945351</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy>
</relation>
<relation>
<normativelyReferencedBy>00-3880945351</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy>
</relation>
<relation>
<normativelyReferencedBy>01-1012946847</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 13291-2:2003</normativelyReferencedBy>
</relation>
</relations>

00-3880945351
BS EN 12899-1:2007
00-3880945351
BS EN 12899-1:2007
01-1012946847
BS EN 13291-2:2003
在这个xml中,前两个节点相同,文本也相同,因此我想返回它们的文件名,如下所示: 我想通过marklogic server中的xquery返回

Result:
abc.xml
<relation>
<normativelyReferencedBy>00-3880945351</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy>
</relation>
结果:
abc.xml
00-3880945351
BS EN 12899-1:2007

通过使用一些XSLT,您可以做类似的事情

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="//relation[preceding-sibling::relation
        [./normativelyReferencedBy/text() = current()/normativelyReferencedBy/text()]]
        ">
        <xsl:copy>
            <xsl:copy-of select="./*"></xsl:copy-of>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()"></xsl:template>

</xsl:stylesheet>


它只是一个样板,但它返回前面有一个副本的元素。您需要自己添加所需的信息。

埃里克的回答很好。但是,此解决方案将捕获重复元素并不总是在彼此之前的情况:

xquery version "1.0-ml";

let $data :=     
<relations>
<relation>
<normativelyReferencedBy>00-3880945351</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy>
</relation>
<relation>
<normativelyReferencedBy>00-3880945351</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 12899-1:2007</normativelyReferencedBy>
</relation>
<relation>
<normativelyReferencedBy>01-1012946847</normativelyReferencedBy>
<normativelyReferencedBy>BS EN 13291-2:2003</normativelyReferencedBy>
</relation>
</relations>

let $map := map:map()

let $uniqueList :=
for $each at $i in $data/relation
 for $every at $j in $data/relation
   where $i ne $j
     return 
       if(fn:deep-equal($each, $every)) then
         if(fn:exists(map:get($map, xs:string($j)))) then () else map:put($map, xs:string($i), xs:string($j)) 
       else ()

return
<relations>
{
for $each at $i in $data/relation
  return 
   if(fn:exists(map:get($map, xs:string($i)))) then $each else ()
}
</relations>
xquery版本“1.0-ml”;
let$data:=
00-3880945351
BS EN 12899-1:2007
00-3880945351
BS EN 12899-1:2007
01-1012946847
BS EN 13291-2:2003
让$map:=map:map()
让$uniqueList:=
在$数据/关系中的$i处,每个$
对于$data/relation中的$j中的$every
其中$i和$j
返回
如果(fn:deep equal($each,$every))那么
如果(fn:exists(map:get($map,xs:string($j))),则()否则map:put($map,xs:string($i),xs:string($j))
else()
返回
{
在$数据/关系中的$i处,每个$
返回
如果(fn:exists(map:get($map,xs:string($i))),那么$eather()
}

您尝试了什么,您的代码在哪里,我们不是来为您做工作的,因为您懒得自己尝试和搜索。您尝试遍历数据库中的所有文档是否正确,要返回包含重复节点的文件的名称吗?实际上,我们需要多个xml文件,每个文件都有关系元素。在关系元素“normalativelyreferencedby”中,元素多次出现在同一文本中。因此,我们只需要提取重复节点,而不需要提取不同节点。感谢@EriK Zander给出正确答案,xquery中是否可能。因为我将Marklogic server与Xquery一起使用。虽然你的答案是正确的。我检查过了。在marklogic中,您可以使用
xdmp:xslt invoke
xdmp:xslt eval
您好,您的代码提示出现以下错误,请检查或建议我如何处理。[1.0-ml]XDMP-UNEXPECTED:(err:XPST0003)意外的令牌语法错误,意外的Lbrace,期望函数30或百分比@AkbaraLI现在都已修复,粘贴时出错。谢谢,现在代码正在正常运行,仍然需要一些更改。上面的代码返回此代码:00-3880945351 BS EN 12899-1:2007 01-1012946847 BS EN 13291-2:2003,但我只想返回重复的代码:00-3880945351 BS EN 12899-1:2007@AkbaraLI modified,所以它会这样做。如果你决定回去的话,只需在最后一页翻动()和$。