Joel
Joel

Reputation: 689

Extracting xml content with xpath expression

I have the following request:

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

I want to send the response:

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

So most of the response is the same : the id, the parameters. Is there a way to get a subset of xml full text with an xpath expression? I would like to get :

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

Optional question: is it possible to get only one string ? Basically I want the result of my XPath evaluation to be :

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

Thanks!

Upvotes: 0

Views: 126

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

XPath can't create new nodes, or modify existing nodes: it can only select nodes that are already present in your input. You need XQuery or XSLT. It's simple enough (XSLT 2.0):

<xsl:template match="request">
  <response>
    <xsl:copy-of select="id, parameters"/>
    <result>3</result>
  </response>
</xsl:template>

Upvotes: 1

Related Questions