简体   繁体   English

如何在 xpath marklogic 中的变量中传递元素名称?

[英]how to pass the element name in variable in xpath marklogic?

My我的

let $doc :=
  <Test>
    <Abc>value of the element</Abc>

  </Test>

Code -代码 -

for $i in $doc
let $x := 'Abc'
return $i/$x

result expected 'value of the element'结果预期“元素的值”

please help!!请帮忙!!

TIA TIA

In your code example, you let the $x variable and assigned a string 'Abc' .在您的代码示例中,您让$x变量并分配了一个字符串'Abc' If you wanted to use that string to find elements that have that name, you could adjust your XPath to select child elements and then in a predicate filter, test whether the name() or local-name() .如果您想使用该字符串来查找具有该名称的元素,您可以将 XPath 调整为 select 子元素,然后在谓词过滤器中测试name()local-name() Either can work if you know there are no namespaces and namespace-prefixes to deal with, but probably best to use local-name() .如果您知道没有要处理的名称空间和名称空间前缀,则两者都可以工作,但最好使用local-name()

$doc/*[local-name()=$x] 

That would select the Abc element, but if you wanted just the value, you could then select the text() node那将是 select Abc元素,但如果您只想要值,则可以 select text()节点

for $i in $doc
let $x := 'Abc'
return $i/*[local-name()=$x]/text()

or apply the string() function:或应用string() function:

for $i in $doc
let $x := 'Abc'
return $i/*[local-name()=$x]/string()

If you know that it will be the Abc element, then you can eliminate the $x variable:如果您知道它将是Abc元素,那么您可以消除$x变量:

for $i in $doc/Abc
return $i/string()

And you could eliminate the for loop completely and just use an XPath statement like this:您可以完全消除 for 循环,只需使用 XPath 语句,如下所示:

$doc/Abc/string()

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

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