0

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!

1
  • No, XPath is the wrong tool here. (XPath 2.0 might be able to cobble together some transformation-like output, but you'll not be happy going that route.) XPath is primarily for selection, but your task requires transformation. Use XSLT for this instead. Commented Sep 7, 2017 at 13:35

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.