Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/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 如何使用xpath或xquery根据父节点中的同级元素提取元素的值?_Xml_Xpath_Xquery - Fatal编程技术网

Xml 如何使用xpath或xquery根据父节点中的同级元素提取元素的值?

Xml 如何使用xpath或xquery根据父节点中的同级元素提取元素的值?,xml,xpath,xquery,Xml,Xpath,Xquery,我有一个这样的有效载荷 <Payload> <Identifier> <Type>typeA</Type> <Value>valueOfA</Value> </Identifier> <Identifier> <Type>typeB</Type> <Value>valueOfB</Value>

我有一个这样的有效载荷

<Payload> 
  <Identifier> 
    <Type>typeA</Type>  
    <Value>valueOfA</Value> 
  </Identifier>  
  <Identifier> 
    <Type>typeB</Type>  
    <Value>valueOfB</Value> 
  </Identifier> 
</Payload>

A型
价值
B型
价值b
我想使用xpath或xquery进一步提取元素“typeA”的值,以便转换后的负载如下所示

<transformedPayload>
<typeA>valueOfA</typeA>
<typeB><valueOfB</typeB>
</transformedPayload>

价值

这在XQuery中很容易做到。以下查询符合您的要求:

<transformedPayload>{
  for $ident in /Payload/Identifier
  return element { $ident/Type } { $ident/Value/text() }
}</transformedPayload>
您还可以使用
=
运算符及其存在语义使其更加紧凑:

<transformedPayload>{
  for $ident in /Payload/Identifier
  where $ident/Type = ('typeA', 'typeB')
  return element { $ident/Type } { $ident/Value/text() }
}</transformedPayload>
{
对于$ident in/Payload/Identifier
其中$ident/Type=('typeA','typeB')
返回元素{$ident/Type}{$ident/Value/text()}
}

谢谢,成功了。但我还有一个疑问。假设有N个元素,比如typeA值a typeB值b typeC值ofc,但我必须只提取typeA和typeB值,我如何才能做到这一点?你能看一下上面的评论吗?我已经为此添加了一些示例。非常感谢:)
<transformedPayload>{
  for $ident in /Payload/Identifier
  where $ident/Type = ('typeA', 'typeB')
  return element { $ident/Type } { $ident/Value/text() }
}</transformedPayload>