How to get tags inside of CDATA with Xpath3 on Anypoint Mule? - xpath

I am trying to get the tag Decision inside a PDP response with Xpath3 on Anypoint mule.
This is the response of the PDP:
<ns:getDecisionResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://org.apache.axis2/xsd">
<ns:return><![CDATA[<Response xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"><Result><Decision>Indeterminate</Decision><Status><StatusCode Value="urn:oasis:names:tc:xacml:1.0:status:syntax-error"/><StatusMessage>Invalid request : DOM of request element can not be created from String</StatusMessage></Status></Result></Response>]]></ns:return>
</ns:getDecisionResponse>
The problem is that I can't access the tags which are inside of the CDATA.
This is what I was trying to do:
#[xpath3('/ns:getDecisionResponse/ns:return/(the problem is here...)',payload, 'STRING')]
Thanks in advance!
Juan Andrés

You can use the regex expression to remove the CDATA from your XPATH3 as following :-
<logger message="#[xpath3('/*:getDecisionResponse/*:return',payload, 'STRING').replaceAll("[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD\\x{10000}-\\x{10FFFF}]", "")]" level="INFO" doc:name="Logger"/>
reference :- http://www.rgagnon.com/javadetails/java-sanitize-xml-string.html
Now once you get the xml here from your CDATA you need to perform another XPATH3 extraction to get you required value from the the xml

Related

Issue on Camel route - parsing XML tags

I have a complex camel route, which starts with an initialization route, which tries to set the headers with the info from the XML used as input.
I wonder how the route is not being able to parse the XML content, using XPath.
Before calling the route, I print the xml information in my java JUNIT, and it prints correctly, with all xml tags.
So I know the information is being sent as I am expecting.
But that route, which should set the headers using XPath, returns empty to any expression I try to use! I even used a XPath tool to assist me (https://codebeautify.org/Xpath-Tester), to check if was some xpath coding mistake, but I get the results I want from there.
So, let's suppose, I have an XML as:
<bic:Test>
<bic:context>
<bic:memberCode>GOOGLE</bic:memberCode>
</bic:context>
</bic:Test>
So, with the line below:
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag>/text()</xpath>
</setHeader>
or
<setHeader headerName="myHeader">
<xpath resultType="java.lang.String">//<anyTag></xpath>
</setHeader>
I will see the header with empty content.
I tried so many different things, that finally I decided to print the all the content, using an XPath expression as /.
It will print only the content ("GOOGLE"), not the tags.
Could you please assist me?
Thank you in advance!
This is probably a namespace related issue.
You have to define the bic namespace in the camel context and then use it in the xpath expression.
Have a look at the documentation in https://github.com/apache/camel/blob/master/camel-core/src/main/docs/xpath-language.adoc and particularly in the example of "Using XML configuration"
Also look at "Namespace auditing to aid debugging" for further information about debugging namespace related issues in camel.

Schema validation component is mule esb

I'm using schema validation component for xml validation.
In my scenario xml comes from JMS queue and need to place in other JMS queue in between I need to validate incoming xml whether it is proper or not by XSD Schema.
but when I put logger after Schema Validation component it is not logging( logging as[#document: null]) xml even input xml is as per XSD Schema.
<mulexml:schema-validation-filter name="Schema_Validation" schemaLocations="C:\src\test\resources\Orders.xsd" returnResult="true" doc:name="Schema Validation" />
<logger message="content is #[payload]" level="INFO" doc:name="Logger"/>
Please suggest me on this., Thanks...,
Do you get a stacktrace? Can you set a logger directly after the JMS endpoint and validate whether it is a correct message or not? Is it a nested schema?
In order to allow a valid XML to pass the filter you need to set the
returnResult to false in your schema-validation-filter. This way the
payload of the message that is passed onwards to the next element in
your flow will retain the XML content.

Mule ESB: How to use xpath expression on session variable?

I have a requirement to extract some information form session variables. The session variables Type is String and contains XML. i tried XPATH expression like -
xpath('//tns:CreateUpdateRQ/tns:RequestInfo/sns:requestSourceID',sessionVars.originalMessage)
however I get exception stating "Could not find transformer NULL Payload to Document". I tried MEL like - #[sessionVars.originalMessage.CreateUpdateRQ.RequestInfo.requestID]. But it's not working either.
How can I extract information from session variable of Type string holding XML.
P.S. I can set the payload to sesion variable & use xpath but it's not suitable for my case because I need to extract the information from session variable 7 pass it as argument to datamapper.
Appreciate you help on this.
To facilitate reading the xml file, I recommend transforming String (Object) to xml, for example:
<xml:object-to-xml-transformer />
then
<logger level="WARN" message="#[xpath://soapenv:Envelope/soapenv:Body//...]" />
In short, make sure the payload is XML type, you can by setting it
<set-property propertyName="Content-Type" value="text/xml" />

Using Ternary Operator in Spring-Integration xpath-expression

I am using Spring Integration and have the following two kinds of xml inputs
First XML:
<businessArea>SomeValue</businessArea>
Second XML:
<?xml version="1.0" encoding="UTF-8"?>
<businessArea>
<Code>SomeValue</Code>
</businessArea>
Now, I want to store the value of business Area in the header based on the following conditions:
1) If the businessArea has no child nodes such as <Code> in the second xml, then the node value of businessArea should be stored in the header.
2) If the businessArea has any child node such as <Code> in the second xml, then null value should be stored in the header.
I am currently using the following expression:
<int-xml:header name="businessArea" xpath-expression="//businessArea" evaluation-type="STRING_RESULT" overwrite="true" />
But, the above expression will not store the null value in the header if the xml is similar to the second xml that I have shown above.
I think I need to use ternary operator in the above expression. Can any one please suggest an expression which addresses my issue?
That's true, the XPath doesn't such a operator, but we can overcome it with just SpEL. Thanks to Spring Integration we hava #xpath SpEL-function out of the box:
<int:header-enricher>
<int:header name="businessArea" expression="#xpath(payload, '//Code', 'boolean') ? null : #xpath(payload, '/businessArea')/>
</int:header-enricher>

parsing xml file in javascript

AM developing a auto completion or suggestion box using AJAX and servlets . My problem is how to parse the XML response in java script to show it in a div tag.My xml response is like contains one parent tag RESULTS , it contains number of children tags called RESULT.
How to get result values in java script variables.
You could do this using jQuery.parseXML
Take look on this thread: Parse XML from XMLHttpRequest
I think it contains information you need.

Resources