Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

extract variable issue

Not applicable

Used assign message policy and assigned text/xml payload say like below.<a><b>hi</b></a> .then used extract variable with xml load xpath expression and assigned to variable x.eg: /a/b.But while testing it gave error saying unable to extract x.Please advice.Does Extract variable extracts only from from real target response or why it does not work for xml payload assigned from Assign messgae policy.

Solved Solved
0 8 1,272
1 ACCEPTED SOLUTION

The problem is with the lack of appropriate namespaces in your xpath.

In your comment you wrote "cannot paste whole as comment length excceds, but it is definitely well formed xml". But the part you omitted is the key part. We cannot diagnose for certain without that part.

But, I have a good guess. It looks like a WSDL file that you are embedding into the AssignMessage policy. And the root element in a WSDL file is the "definitions" element in the xml namespace of "http://schemas.xmlsoap.org/wsdl/". Example:

<definitions 
  targetNamespace="something-something"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns="http://schemas.xmlsoap.org/wsdl/"
>
  much more stuff here

Therefore your XMLPayload element in the ExtractVariables policy must specify the WSDL namespace and use the appropriate prefix in the xpath. Like this:

  <XMLPayload>
    <Namespaces>
      <Namespace prefix='wsdl'>http://schemas.xmlsoap.org/wsdl/</Namespace>
      <Namespace prefix='soap'>http://schemas.xmlsoap.org/wsdl/soap/</Namespace>    </Namespaces>
    <Variable name='name' type='string'>
      <XPath>/wsdl:definitions/wsdl:service/wsdl:port/soap:address/@location</XPath>
    </Variable>

Do you understand why? Try and see, I bet it will work.

Here's a quick summary of XML Namespaces. Namespaces qualify elements. Therefore this XML document:

<a>
  <b>Hi</b>
</a>

...is not equivalent to this XML document.

<a xmlns="dino-was-here">
  <b>Hi</b>
</a>

The latter has specified that the a element and all of its children are scoped in the XML Namespace called "dino-was-here". (BTW, this is a TERRIBLE XML Namespace name. In fact, it should be a URN, so for example: http://mycompany.com/schema/messages is a good URN, or you could also use urn:my-company:schema:201703 . but I digress!)

The point is that the element a's are different in document 1 and document 2. An Xpath of

/a/b/text()

...will resolve to "Hi" when applied to the first document, whereas it will resolve to the empty set when applied to the second document. To get "Hi" from the second document, you need an xpath like this:

 /ns1:a/ns1:b/text()

...where "ns1" can be any simple string. In the parlance of XML Namespaces, it is known as a prefix. The actual prefix is not important, but what it gets mapped to is important. The prefix has to be mapped to the string "dino-was-here".

The ns1 in this example is like the prefix "wsdl" in the solution I gave above, and the string "dino-was-here", which again, denotes an XML Namespace, corresponds to "http://schemas.xmlsoap.org/wsdl/". One thing to be careful of: the namespace string is compared EXACTLY. Therefore "http://schemas.xmlsoap.org/wsdl/" (with the trailing slash) is not the same as "http://schemas.xmlsoap.org/wsdl" (without). They are not equivalent.

I hope this clarifies.

Some people say "ok, great, I understand XML namespaces, but I just don't want to deal with that. Can I specify an XPath that works regardless of the XML Namespace?" and the answer to that is "No, You can't." Sorry. there's no way to tell an XPath resolver "just ignore the namespaces on the elements." It would make life simpler in many cases, but it's not possible.

View solution in original post

8 REPLIES 8
Top Solution Authors