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>
Related
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
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" />
I am expecting multiple operation in one request. I need to to loop the xml to to do the following using Apache camel route.
1) get the total opertions in request xml and put in variable.
2) get total number of expression using xpath on xml and put in list
3) loop with (total number of operation ) times to evaluate the expression
First step would be list nodeList = /tractscation/operations
<loop>
<constant>nodeLIst.length</xpath>
compare and execute operation
</loop>
Above lines are just psuedo code, i want anybody help me with exact code using camel Xpath and loop. .
I am new to xpath and camel. we are using camelxpath spring DSL
if you want to loop through each node matching the xpath and process it individually, then use camel-splitter EIP...
<route>
<from uri="direct:a"/>
<split>
<xpath>/transaction/operations</xpath>
<to uri="direct:b"/>
</split>
</route>
otherwise, there is a camel-loop EIP that can be used to execute the same process a variable number of times...but the splitter is generally used for parsing/looping type of operations
from("direct:c").loop().xpath("/hello/#times").to("mock:result");
I hope it helps you =D
Inside route
<to uri="direct:WSCall" />
<split strategyRef="groupExchangeAggregationStrategy">
<xpath>//response/operation</xpath>
</split>
Velocity template
<tag>
#foreach( $exchangeItem in ${body} )
${exchangeItem.in.body}
#end
</tag>
Include
<beans>
<bean id="groupExchangeAggregationStrategy" class="org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy" />
</beans>
New to Camel, and I'm trying to parse a response error xml. Within the camel-context I need to determine if a specific value exists in the error file, and handle it differently than other errors.
The other errors use a series of when statements:
<when>
<xpath>/abc:ErrorResponse/abc:Error/abc:Message/.</xpath>
<setHeader headerName="RESPONSE_STRING">
<xpath resultType="java.lang.String">/abc:ErrorResponse/abc:Error/abc:Message/.</xpath>
</setHeader>
<setHeader headerName="MY_DATA_FIELD"><constant>Error</constant></setHeader>
<to uri="def:doErrorStuff" pattern="InOnly"/>
</when>
<when>
<xpath>/ghi:ErrorResponse/ghi:Error/ghi:Message/.</xpath>
<setHeader headerName="RESPONSE_STRING">
<xpath resultType="java.lang.String">/ghi:ErrorResponse/ghi:Error/ghi:Message/.</xpath>
</setHeader>
<setHeader headerName="MY_DATA_FIELD"><constant>Error</constant></setHeader>
<to uri="def:doErrorStuff" pattern="InOnly"/>
</when>
My error XML file has an outer error element with child "Code" element. I need to parse the value of the code element
UPDATE: HERE IS THE XML I AM PARSING
<ErrorResponse xmlns="http://myhost/location1/">
<Error>
<Type>reserved</Type>
<Code>TEXT_I_NEED_TO_PARSE_IN_WHEN_STATEMENT</Code>
</Error>
<RequestId>some_id</RequestId>
</ErrorResponse>
I used a combination of xpath and 'simple' to make the check. Like this:
<when>
<xpath>/ghi:ErrorResponse/ghi:Error/ghi:Message/.</xpath>
<when>
<simple>${in.body.code} == 'StringIAmSearchingFor'</simple>
<!-- Do Stuff --!>
</when>
</when>
However, I am not getting the response I expect.
1. Is there something wrong with this approach or the syntax?
2. Is there a way to combine the double when layout so they are and-ed together. Otherwise, if I add my "when" statement just ahead of the existing two, the existing "ghi" when statement will never get executed (the xpath statements match).
You are using xpath on your input suggesting that it's XML, then simple (${in.body.code}) which in that case also would be XML. Simple is used to traverse java bodies and not other formats such as XML. Stick to XPATH all the way - your code above can easily be implemented in xpath. Another way, of course, would be to unmarshal the XML into java objects using xstream or jaxb, then you can use only simple/OGNL/groovy or whatnot.
Since I recommend you to solve this very case with xpath alone, you can of course use the xpath and operator to and several xpath expressions together. All logic and power in camel choice/when reside in the expression language you are using (be it simple or xpath), so if you want to mix expression languages, you have to build up sort of a decision tree. That could actually be something good if you are trying to implement very complex routing logic. For a single special case - it's, IMHO, just messy.
Can someone help me with this:
I am using xpath extractor of jmeter to retrieve sessionId value from the below response.
I need the value of the sessionId to be stored in some file, so that i can use that value in succeeding calls..My basic response is as below:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:createUserResponse xmlns:ns2="http://www.musicthp.com"
isNewUser="false"
profileId="32109"
sessionId="ryIlb+E5yj7FReA2w96uag=="
success="true">
<duration>316</duration>
</ns2:createUserResponse>`
In order to use the results in a future call, you simply need to use the "Reference Name" as a variable.
If you configure the Xpath Extractor reference name as sessionID, subsequent calls would use ${sessionID}