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/7/css/41.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
用xpath表达式提取xml内容_Xpath_Xpath 2.0 - Fatal编程技术网

用xpath表达式提取xml内容

用xpath表达式提取xml内容,xpath,xpath-2.0,Xpath,Xpath 2.0,我有以下要求: <request> <sender>A</sender> <id>01</id> <parameters> <parameter value="1" /> <parameter value="2" /> </parameters> </request> A. 01 我想发送回复: <response

我有以下要求:

<request>
   <sender>A</sender>
   <id>01</id>
   <parameters>
       <parameter value="1" />
       <parameter value="2" />
   </parameters>
</request>

A.
01
我想发送回复:

<response>
   <id>01</id>
   <parameters>
       <parameter value="1" />
       <parameter value="2" />
   </parameters>
   <result>3</result>
</response>

01
3.
所以大部分响应都是相同的:id,参数。有没有办法用xpath表达式获取xml全文的子集? 我想得到:

   <id>01</id>
   <parameters>
       <parameter value="1" />
       <parameter value="2" />
   </parameters>
01
可选问题:是否可以只获取一个字符串? 基本上,我希望XPath计算的结果是:

   "<id>01</id>
   <parameters>
       <parameter value="1" />
       <parameter value="2" />
   </parameters>"
“01
"

谢谢

XPath无法创建新节点或修改现有节点:它只能选择输入中已经存在的节点。您需要XQuery或XSLT。它非常简单(XSLT 2.0):


3.

否,XPath是错误的工具。(XPath2.0可能能够拼凑出一些类似于输出的转换,但您不会乐意这样做。)XPath主要用于选择,但您的任务需要转换。请改用XSLT。
<xsl:template match="request">
  <response>
    <xsl:copy-of select="id, parameters"/>
    <result>3</result>
  </response>
</xsl:template>