Can i use complex condition using XPath in Filter Mediator WSO2? - xpath

I have a condition written in SQL Language that i would like to use it in Filter Mediator XPath but it didn't work and it give me an error .
NB : I got variable value from JSON payload .
Actual SQL Condition
(!ISNULL(a) && a== 2 && b== 1) || c== 5
Expected XPath Condition
(//a=2 AND //b=1) OR //c=5

Try the following.
<filter xpath="boolean(//a/text() = '1' and //b/text() = '2') or //c/text() = '3'">
<then>
<log>
<property value="===== TRUE" name="MSG"/>
</log>
</then>
<else>
<log>
<property value="===== FALSE" name="MSG"/>
</log>
</else>
</filter>

Related

XPath 1.0 fetch the child record

Below is the XML
<on-error-continue type="APIKIT:BAD_REQUEST" enableNotifications="true" logException="true">
<set-variable value="200" doc:name="httpStatus" variableName="httpStatus" />
<set-variable value="Bad request" doc:name="logDescription" variableName="logDescription" />
<flow-ref doc:name="global-prepare-error-response-sub-flow" name="global-prepare-error-response-sub-flow"/>
</on-error-continue>
<on-error-continue type="APIKIT:TOO_MANY_REQUEST" enableNotifications="true" logException="true">
<set-variable value="200" doc:name="httpStatus" variableName="httpStatus" />
<set-variable value="Many request" doc:name="logDescription" variableName="logDescription" />
<flow-ref doc:name="global-prepare-error-response-sub-flow" name="global-prepare-error-response-sub-flow"/>
</on-error-continue>
Wanted to get the single record
"set-variable value="200" doc:name="httpStatus" variableName="httpStatus"
using xPath 1.0 expression: Parent is -->on-error-continue type="APIKIT:BAD_REQUEST" and child is -->set-variable value = "200".
Have tried below expression. It is working fine with Xpath2.0 but not working with 1.0
//*[local-name()='on-error-continue'][#*[local-name()='type' and .='APIKIT:BAD_REQUEST']]/set-variable[#value='200' and #variableName='httpStatus']
Using this handy website, I took the xml and put it in a root element, <root>YOUR XML</root>.
With this XPath:
//root/on-error-continue[#type='APIKIT:TOO_MANY_REQUEST']/set-variable[#value='200' and #variableName='httpStatus']
I was able to extract the matching record. Try it yourself and replace the root with * in the above XPath. You should see the records that you're seeking.
The wildcard operator can be used like any element in the path.

XPath 2 expressions interfering with each other

I have a XML like this:
<Values>
<Value AttributeID="asset.extension">pdf</Value>
<Value AttributeID="asset.size">10326</Value>
<Value AttributeID="ATTR_AssetPush_Webshop">1</Value>
<Value AttributeID="asset.format">PDF (Portable Document Format application)</Value>
<Value AttributeID="asset.mime-type">application/pdf</Value>
<Value AttributeID="asset.filename">filename.pdf</Value>
<Value AttributeID="asset.uploaded">2018-01-10 17:05:39</Value>
<Value AttributeID="ATTR_Verwendungsort" Derived="true">WebShop,</Value>
</Values>
I have 2 (or more) XPath-expressions like this:
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(#AttributeID='asset.mime-type')]" />
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(#AttributeID='asset.size')]" />
For some reason though, If I have 2 of them together, all information are being stripped. If I use only 1 expressoin, I get my desired output. Can't I use 2 expressions like this?
I also tried combining them like this:
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values/Value[not(#AttributeID='asset.mime-type') and (#AttributeID='asset.size')]" />
But that didn't do it, either.
The desired output would be like this:
<Values>
<Value AttributeID="asset.size">10326</Value>
<Value AttributeID="asset.mime-type">application/pdf</Value>
</Values>
I think in XSLT 2/3 you could express it as
<xsl:template match="Values/Value[not(#AttributeID = ('asset.mime-type', 'asset.size'))]"/>
In XSLT/XPath 1.0 you would need Values/Value[not(#AttributeID = 'asset.mime-type' or #AttributeID = 'asset.size')].
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values
/Value[not(#AttributeID='asset.mime-type')]" />
<xsl:template match="/STEP-ProductInformation/Assets/Asset/Values
/Value[not(#AttributeID='asset.size')]" />
This is a logical error -- has nothing to do with XPath.
It is like saying:
From all days of the week I will work only on Mondays
From the days selected above I will work only on Tuesdays
The first statement above selects only Mondays. The 2nd statement selects all Tuesdays from these Mondays -- that is the empty set.
A correct statement:
From all days of the week I will work only on Mondays or Tuesdays

Need to finding overlapping dates in XML code

I need help finding the overlapping dates in this XML code. I need to make sure that the End date is not less than or equal to the proceeding Start Date.
<Inventory>
<StatusApplicationControl Start="2019-07-18" End="2019-07-18" InvTypeCode="STDX" />
<InvCounts>
<InvCount CountType="2" Count="9" />
</InvCounts>
</Inventory>
<Inventory>
<StatusApplicationControl Start="2019-07-18" End="2019-07-19" InvTypeCode="STDX" />
<InvCounts>
<InvCount CountType="2" Count="8" />
</InvCounts>
</Inventory>
I have tried the following code.
<rule context="Inventory">
<report test="translate(StatusApplicationControl/#Start, '-', '') <= translate(preceding::Inventory/preceding::StatusApplicationControl/#End, '-', '')">The #Start="<value-of select="#Start"/>" and #End="<value-of select="#End"/>" dates are overlaping</report>
</rule>
I expect this message to be printed -
The Start="2019-07-18" is less than or equal to the End="2019-07-18" date
Any help is greatly appreciated!
It looks like the comments are not helping you.
The rule should be:
<rule context="Inventory">
<report
test="translate(StatusApplicationControl/#Start, '-', '')
<=translate(preceding::Inventory[1]/StatusApplicationControl/#End,'-','')"
>The #Start="<value-of select="#Start"/>" and #End="<value-of
select="preceding::Inventory[1]/StatusApplicationControl/#End"
/>" dates are overlaping</report>
</rule>
EDIT
This Schematron
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<title>Test dates</title>
<rule context="Inventory">
<assert
test="translate(StatusApplicationControl/#Start, '-', '')
> translate(preceding::Inventory[1]/StatusApplicationControl/#End,'-','')"
>The #Start="<value-of
select="StatusApplicationControl/#Start"
/>" and #End="<value-of
select="preceding::Inventory[1]/StatusApplicationControl/#End"
/>" dates are overlaping</assert>
</rule>
</pattern>
</schema>
With this input:
<root>
<Inventory>
<StatusApplicationControl Start="2019-07-18" End="2019-07-18" InvTypeCode="STDX" />
<InvCounts>
<InvCount CountType="2" Count="9" />
</InvCounts>
</Inventory>
<Inventory>
<StatusApplicationControl Start="2019-07-18" End="2019-07-19" InvTypeCode="STDX" />
<InvCounts>
<InvCount CountType="2" Count="8" />
</InvCounts>
</Inventory>
</root>
Output:
Pattern 'Test dates' Failed : The #Start="2019-07-18" and #End="2019-07-18" dates are overlaping.
Check in https://www.liquid-technologies.com/online-schematron-validator
Here is the SchemaTron rule that worked for me. The issue was passing preceding:: to the translate() function. When doing that I got a SchemaTron Exception.
<rule context="Inventory/StatusApplicationControl">
<report test="translate(#Start, '-', '') <= preceding::StatusApplicationControl/translate(#End, '-', '') ">The #Start="<value-of select="#Start"/>" and #End="<value-of select="#End"/>" dates are overlaping</report>
</rule>

Spring Batch Paging Issue

I am working on Spring Batch and I am using paging for fetching data from DB. for the first time Its firing Query as expected but for second page its adding one more unexpected parameter. I don't understand why.
My Query in XML file is
<bean id="summaryAppReader" scope="step" autowire-candidate="false"
class="org.springframework.batch.item.database.JdbcPagingItemReader">
<property name="dataSource" ref="dataSource" />
<property name="rowMapper">
<bean class="com.majesco.nyl.batch.mapper.SummaryApplicationRowMapper" />
</property>
<property name="queryProvider">
<bean
class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="fromClause" value="VIEW_SUMMARY_APPLICATION" />
<property name="selectClause"
value="application_id, upper(application_status) as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('#{jobParameters['runDate']}', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays" />
<property name="sortKeys">
<map>
<entry key="transaction_num" value="ASCENDING" />
</map>
</property>
<property name="whereClause"
value="(upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('#{jobParameters['runDate']}', 'yyyy-mm-dd'))) and TRANSACTION_NUM >= :minId and TRANSACTION_NUM <= :maxId" />
</bean>
</property>
<property name="parameterValues">
<map>
<entry key="minId" value="#{stepExecutionContext[minValue]}" />
<entry key="maxId" value="#{stepExecutionContext[maxValue]}" />
</map>
</property>
</bean>
for fetching data for first page its firing query something like this and its expected
Reading page 0
SQL used for reading first page: [SELECT * FROM (SELECT application_id, upper(application_status) as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('2016-06-29', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays FROM VIEW_SUMMARY_APPLICATION WHERE (upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('2016-06-29', 'yyyy-mm-dd'))) and TRANSACTION_NUM >= :minId and TRANSACTION_NUM <= :maxId ORDER BY transaction_num ASC) WHERE ROWNUM <= 10]
Using parameterMap:{minId=100000002630, maxId=100000002663}
But for the second page its adding one more parameter in Query _TRANSACTION_NUM which is unexpected like
Reading page 1
SQL used for reading remaining pages: [SELECT * FROM (SELECT application_id, upper(appication_status) as application_status,relationship_code, create_date,submission_date,transmission_date,TRANSMISSION_STATUS,tpa_notes,site_details_id,transaction_num,PROCESS_STATUS,process_date,application_status_date,first_name,middle_name,last_name,suffix,prefix,APPROVED_REJECTED_DATE,action_by,ass_acronym,user_name,applicant_name,site_name,ASSOCIATION_ID, (TO_DATE('2016-06-29', 'yyyy-mm-dd') - trunc(create_date)) as elapsedDays FROM VIEW_SUMMARY_APPLICATION WHERE (upper(application_status)='PENDING' OR (upper(application_status) IN ('APPROVED','DECLINED') AND trunc(application_status_date) = TO_DATE('2016-06-29', 'yyyy-mm-dd'))) and TRANSACTION_NUM >= :minId and TRANSACTION_NUM <= :maxId ORDER BY transaction_num ASC) WHERE ROWNUM <= 10 AND ((transaction_num > :_transaction_num))]
Using parameterMap:{minId=100000002596, maxId=100000002629, _transaction_num=100000002622}
FYI. TRANSACTION_NUM is not unique.
It's due to your sortKeys. You defined the following:
<property name="sortKeys">
<map>
<entry key="transaction_num" value="ASCENDING" />
</map>
</property>
Spring Batch is using the highest transaction_num from page 0 and saying the next page must have a transaction_num higher than that.
The framework will increment the bind variable with each page to ensure you keep getting unique results. The problem here is that you realistically need a Primary Key to page correctly otherwise your pageSize (default = 10 and seen where it adds WHERE ROWNUM <= 10 to your query) will cut off some of your records.

Is there a bug in the Advanced Find in MS CRM 2015 online, when in recursive queries? Example: Find Account Branches that belong to Account Branches

No matter what I have tried, I was unsuccessful in getting the Advanced Find feature of MS CRM 2015 online to show me Active Accounts that are of type Branch that have as their parent have an Account type again of Branch.
I have checked and re-checked the data but perhaps there is something wrong with my query in the Advanced Find feature.
Or there is a bug to it.
I have run this query in SQL as well, we have a BigData db that holds our CRM data and there it reveals the correct results (of course).
Here is my query so that you can see:
--BRANCH OF A BRANCH
select [dbo].[fn_getDescFromCRMguid](a.[Parentaccountid]) as [ParentAccountName],
--[fn_getDescFromCRMguid] udf is just a look-up for descriptions and names
( select left(a3.[etf_accountlevel],6)
from [Stg_CRMAccount] a3
where a3.[accountid] = left(a.[parentaccountid],36)
) as [ParentAccountLevel],
[dbo].[fn_getDescFromCRMguid](isnull(a.[owninguser], a.[owningteam])) as [OwningEntity],
a.*
from [Stg_CRMAccount] a
where a.[statuscode] = 'Active::1'
and a.[etf_accountlevel] like 'Branch%'
and a.[Parentaccountid] is not null
and exists (
select 0
from [Stg_CRMAccount] a2
where a2.[statuscode] = 'Active::1'
and a2.[etf_accountlevel] like 'Branch%'
and left(a.[Parentaccountid],36) = a2.[accountid]
)
order by a.[Parentaccountid],
a.[name],
a.[address1_city];
I am also attaching the screenshot of my Advanced Find query and please feel free to comment or let me know of any useful thoughts.
I hope some person has any idea about this because to me it is puzzling.
I have navigated into the resulting Accounts from this and then into their parent Account and it was not a Branch.
Despite the clear (at least to me) specification of the Where clause under the use of the related Entity - Account, using the relation Parent Account.
The plot thickens even more, if you remove the Account Type = Branch for the related Entity and say change the Status = Active to Inactive then it will very correctly find and show only those Branches where their Parent is Inactive.
That leads me to suspect some kind of a recursive error with the query builder they are using.
It finds Account Type = Branch twice and erroneously does not differentiate between the first occurrence which is for the outer selection and the second occurrence which would be for the nested (in the Where clause) selection.
But all that is mere speculation.
I don't really know why it can't handle it.
It begs the question in what other scenarios it can mess up recursive query builds.
Your thoughts?
and here is my fetch XML from the Advanced Find query
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="account">
<attribute name="etf_rating" />
<attribute name="parentaccountid" />
<attribute name="etf_lastcontact" />
<attribute name="etf_city" />
<attribute name="address1_line1" />
<attribute name="etf_accountlevel" />
<attribute name="name" />
<attribute name="ownerid" />
<attribute name="etf_segment" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="etf_accountlevel" operator="eq" value="964850002" />
</filter>
<link-entity name="account" from="parentaccountid" to="accountid" alias="ag">
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="etf_accountlevel" operator="eq" value="964850002" />
</filter>
</link-entity>
</entity>
</fetch>

Resources