Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Xml 如何遍历父元素及其子元素并打印元素名xquery_Xml_Xsd_Xquery - Fatal编程技术网

Xml 如何遍历父元素及其子元素并打印元素名xquery

Xml 如何遍历父元素及其子元素并打印元素名xquery,xml,xsd,xquery,Xml,Xsd,Xquery,我希望遍历所有父元素和子元素,并打印出元素名称 比如说 <Asdf> <parentnode1> <childnode1>...</childnode1> <childnode2>...</childnode2> </parentnode1> <parentnode2> <childnode3>..</childn

我希望遍历所有父元素和子元素,并打印出元素名称

比如说

<Asdf>
   <parentnode1>
        <childnode1>...</childnode1>
        <childnode2>...</childnode2>
    </parentnode1>
    <parentnode2>
        <childnode3>..</childnode3>
        <childnode4>..</childnode4>
    </parentnode2>
</Asdf>
现在我得到的是:

let $a := fn:doc('asdf.xml')/Asdf/*

return 

for $z in $a
return $z/name()
  for $x in $a/*
  return $x/name()

我缺少什么?为什么嵌套for循环不起作用?

只需使用以下XQuery:

let $xdoc := doc('asdf.xml')/Asdf//*
return $xdoc/name()
输出是字符串

parentnode1 childnode1 childnode2 parentnode2 childnode3 childnode4


上面的XQuery迭代从/Asdf开始的所有子元素。

只需使用此XQuery:

let $xdoc := doc('asdf.xml')/Asdf//*
return $xdoc/name()
输出是字符串

parentnode1 childnode1 childnode2 parentnode2 childnode3 childnode4


上面的XQuery迭代从/Asdf开始的所有子元素。

代码不工作的原因是语法错误。在FLWOR语句的返回中,有两个要返回的序列

所以,您需要将其括在括号中并添加逗号,我认为您希望在for循环中引用$z而不是$a:

let $a := fn:doc('asdf.xml')/Asdf/*
return 
  for $z in $a
  return ($z/name(),
    for $x in $z/*
    return $x/name()
  )
或更简短的版本:

for $z in $a
return ($z/name(), $z/*/name())
@zx485提供了一种更简单的方法来实现您的目标。更简单、更短的是:

parentnode1 childnode1 childnode2 parentnode2 childnode3 childnode4
doc('asdf.xml')/Asdf//*/name()

代码无法工作的原因是语法错误。在FLWOR语句的返回中,有两个要返回的序列

所以,您需要将其括在括号中并添加逗号,我认为您希望在for循环中引用$z而不是$a:

let $a := fn:doc('asdf.xml')/Asdf/*
return 
  for $z in $a
  return ($z/name(),
    for $x in $z/*
    return $x/name()
  )
或更简短的版本:

for $z in $a
return ($z/name(), $z/*/name())
@zx485提供了一种更简单的方法来实现您的目标。更简单、更短的是:

parentnode1 childnode1 childnode2 parentnode2 childnode3 childnode4
doc('asdf.xml')/Asdf//*/name()

“//”的含义是什么?…/*选择同一级别上的所有元素,包括其本身或子级。它是子体或self::axis的快捷方式,除非在像//*这样的XPath表达式的开头使用。“//”的含义是什么?…//*选择同一级别的所有元素,包括其本身或子体级别。它是子体或self::axis的快捷方式,除非在像//*这样的XPath表达式的开头使用。