Extract numeric and non-numeric part from a string with WSO2 - xpath

Thanks before. i want need help. I have simple process flow with wso2. The plan was validate and print string for alphabet and numeric. I can print both of them. but i think the formula was so much affort with that. i want find the simple way. i done try with regular expression. but when i try with that. i always get error result
My code :
<?xml version="1.0" encoding="UTF-8"?>
<api context="/split1" name="SplitAlphaNumber" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<property expression="//OperationValueRegex/Value" name="Value" scope="default" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<OperationValueRegex xmlns="">
<Result1>$1</Result1>
<Result2>$2</Result2>
</OperationValueRegex>
</format>
<args>
<arg evaluator="xml" expression="translate(., translate($ctx:Value,'0123456789',''), '')"/>
<arg evaluator="xml" expression="translate(., translate($ctx:Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',''), '')"/>
</args>
</payloadFactory>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Expected Result was like this
<OperationValueRegex>
<Result1>1234</Result1>
<Result2>Mario Naga</Result2>
</OperationValueRegex>
Sample Input from postman :
<OperationValueRegex>
<Value>Mario Naga 1234</Value>
</OperationValueRegex>
and actual output :
<OperationValueRegex>
<Result1>
1234
</Result1>
<Result2>
MarioNaga
</Result2>
</OperationValueRegex>
Please need suggestion with this. thanks

Here is a simpler way to achieve what you need. Make sure Xpath 2.0 is enabled in WSO2 Server.
Use the following two XPath expressions.
fn:tokenize($ctx:Value, ' ')[matches(., '\d+')] // Tokenize the String with space and extract the part with numeric values.
fn:replace($ctx:Value, ' \d+', '') //Replace the numeric part from the string
PLFactory Mediator
<payloadFactory media-type="xml">
<format>
<OperationValueRegex xmlns="">
<Result1>$1</Result1>
<Result2>$2</Result2>
</OperationValueRegex>
</format>
<args>
<arg evaluator="xml" expression="fn:tokenize($ctx:Value, ' ')[matches(., '\d+')]" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
<arg evaluator="xml" expression="fn:replace($ctx:Value, ' \d+', '')" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
</args>
</payloadFactory>

It seems you do a second translate to get rid of leading/trailing spaces. This messes up your values. Instead you can use normalize-space() function as follows, this gets you the desired output:
<api xmlns="http://ws.apache.org/ns/synapse" name="SplitAlphaNumber" context="/split1">
<resource methods="POST">
<inSequence>
<property name="Value" expression="//OperationValueRegex/Value" scope="default" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<OperationValueRegex xmlns="">
<Result1>$1</Result1>
<Result2>$2</Result2>
</OperationValueRegex>
</format>
<args>
<arg evaluator="xml" expression="normalize-space(translate($ctx:Value,'0123456789',''))"/>
<arg evaluator="xml" expression="normalize-space(translate($ctx:Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',''))"/>
</args>
</payloadFactory>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

Related

XSLT: filter and retrieve different elements

<cars>
<car>
<name v="speedy"/>
<type v="sport"/>
<engine>
<hp>300</hp>
</engine>
<car>
<car>
<name v="biggo"/>
<type v="truck"/>
<engine>
<hp>190</hp>
</engine>
<car>
</cars>
I have a problem in building a xpath-term that gives my biggos horsepower.
I am not sure how to filter and get the value of something that is not in the filtered element.

XML sorted list of only certain nodes

I have the following XML file. I need to print a list of only selected nodes (Total) in ascending order. I tried to use sort function, but there were some mistakes I couldn't identify and it returned everything, including values of other nodes in the initial file.
XML input:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
<From>
<Name>Lucy</Name>
<Country>UK</Country>
</From>
<To>
<Name>John</Name>
<Country>US</Country>
</To>
<Items>
<Position>
<Name>Table</Name>
<Total>1</Total>
</Position>
<Position>
<Name>Chair</Nr>
<Total>4</Total>
</Position>
<Position>
<Name>Cup</Name>
<Total>5</Total>
</Position>
<Position>
<Name>Box</Name>
<Total>4</Total>
</Position>
</Items>
</Invoice>
How could I get the required output using?
Any help is greatly appreciated! Thank you!
One obvious approach to generate the desired output from the given input would be using an xsl:for-each also making use of xsl:sort:
<xsl:template match="/Invoice">
<SortedTotalList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:for-each select="Positions/Position">
<xsl:sort select="Total"/>
<xsl:copy-of select="Total" />
</xsl:for-each>
</SortedTotalList>
</xsl:template>
Output is:
<?xml version="1.0"?>
<SortedTotalList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Total>1</Total>
<Total>4</Total>
<Total>4</Total>
<Total>5</Total>
</SortedTotalList>

XPATH get default value when node is empty or not present

I have 3 types of data
<results>
<place>
<key>place</key>
<value>1</value>
</place>
</results>
OR
<results>
<place>
<key>place</key> // notice the missing value
</place>
</results>
OR
<results>
</results>
So my sample data will be like
<event>
<results>
<place>
<key>place</key>
<value>1</value>
</place>
<some additional data here>
</results>
</event>
<event>
<results>
<place>
<key>place</key>
</place>
<some additional data here>
</results>
</event>
<event>
<results>
<some additional data here>
</results>
</event>
I need an XPath expression that can give me a default value when <value> of <place> is present, null or missing. <place> can be missing as well in some cases as mentioned in my third sample data.
Output that I expect here is 1, <default-value>, <default-value>.
XPATH 2.0 solution will work as well. I have tried scourging stackoverflow and google but couldnt find anything.
Use:
//results/concat(place/value, for $r in . return 'default-value'[not($r/place/value)])
XSLT - based verification:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"//results/concat(place/value, for $r in . return 'default-value'[not($r/place/value)])"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided (and completed) XML document:
<t>
<event>
<results>
<place>
<key>place</key>
<value>1</value>
</place>
<x/>
</results>
</event>
<event>
<results>
<place>
<key>place</key>
</place>
<y/>
</results>
</event>
<event>
<results>
<z/>
</results>
</event>
</t>
the XPath expression is evaluated and its results are copied to the output:
1 default-value default-value
I did it finally after a lot of trial and error.
{xpath::/events/event/(results//(place|rank)/value/string(), '')[1]}
the trick was to go one level up i.e. <results> in my case and then use the (if value present, default-value) XPATH notation.
Earlier, I was trying this unsuccessfully.
{xpath::/events/event/results//((place|rank)/value/string(), '')[1]}

Connect internal numbers on freeswitch

I would like to connect two internal numbers with one, I mean if I call 499 then two phones should ring for example 123, 127.
My .xml files in directory/default looks like this:
<include>
<user id="127" mailbox="127">
<params>
<param name="password" value="xxxx"/>
<param name="vm-password" value="127"/>
</params>
<variables>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="127"/>
<variable name="user_context" value="default"/>
<variable name="effective_caller_id_name" value="Extension 127"/>
<variable name="effective_caller_id_number" value="127"/>
<variable name="outbound_caller_id_name" value="$${outbound_caller_name}"/>
<variable name="outbound_caller_id_number" value="$${outbound_caller_id}"/>
<variable name="callgroup" value="techsupport"/>
</variables>
similar for 123 and 499 numbers.
How can I change it to make two phones rings(123,127) when someone calls 499?
in your dialplan you should call the bridge application like this.
<extension name="Local_Extension">
<condition field="destination_number" expression="^(1001)$">
<action application="bridge" data="sofia/internal/1001%${server-domain-name},sofia/internal/1002%${server-domain-name},sofia/internal/1003%${server domain-name}"/>
</condition>
</extension>
So if you call to 1001 then it will ring to 1001,1002,1003

xquery filter on attribute and element

I have the following simple XML document:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>855</text>
</data>
</car>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>745</text>
</data>
</car>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>V70R</text>
</data>
</car>
</cars>
And the following XPath:
/cars/car/data[(#attrib='Model') and (text='855')]
This returns the following result:
<data attrib="Model"><text>855</text></data>
I want the XPath to return the whole <car> block for the match.
So return data would be like this:
<cars>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>855</text>
</data>
</car>
</cars>
How would I modify the XPath expression above to achieve this?
XPath returns whatever node you go up to - in your case you're going to data, so that's what you're getting back. If you want car instead, place your predicate after car.
/cars/car[data/#attrib='Model' and data/text='855']
Or, slightly shorter
/cars/car[data[#attrib='Model' and text='855']]
XQuery to produce the desired output:
<cars>
{/cars/car[data[#attrib='Model' and text='855']]}
</cars>
Here is a complete and likely one of the shortest possible XSLT solutions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/*">
<cars>
<xsl:copy-of select="car[data[#attrib='Model' and text='855']]"/>
</cars>
</xsl:template>
</xsl:stylesheet>
However, the following transformation, using the wellknown identity rule is both easier to write and provides maximum flexibility, extensibility and maintainability:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="car[not(data[#attrib='Model' and text='855'])]"/>
</xsl:stylesheet>
When either of these two transformations is applied on the provided XML document:
<cars>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>855</text>
</data>
</car>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>745</text>
</data>
</car>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>V70R</text>
</data>
</car>
</cars>
the wanted, correct result is produced:
<cars>
<car>
<data attrib="Make">
<text>Volvo</text>
</data>
<data attrib="Model">
<text>855</text>
</data>
</car>
</cars>
Explanation:
The first transformation generates the top element cars, then simply selects the wanted car element and copies it as the body of cars.
The second transformation is based on one of the most fundamental and powerful XSLT design patterns -- using and overriding the identity rule.
The identity template copies every matched node (for which it is selected to process) "as-is".
There is one template overriding the identity rule. This template matches any car for which it is not true that data[#attrib='Model' and text='855']. The body of the template is empty and this results in nothing from the matched car element being copied to the output -- in other words we can say that amy matching car element is "deleted".

Resources