Simple XSLT with choose function WSO2

Viewed 50

i have a simple operation with choose function. i have planning validation for numeric input json. and response for xml with xslt. after test i got was null with json format. can anyone helping me about this, which one i was wrong.

my code API:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/a" name="A" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <xslt key="LOCAL_XSLT_Validate"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_Validate" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:template match="/">
        <!-- TODO: Auto-generated template -->
        <xsl:variable name="Value" select="/OperationValueRegex/Value/text()"/>
        <OperationValueRegex>
            <xsl:choose>
                <xsl:when test="/OperationValueRegex/Value != 0">
                    <Result1><xsl:value-of select="fn:tokenize(/OperationValueRegex/Value, ' ')[matches(., '\d+')]"/></Result1>
                </xsl:when>
            </xsl:choose>
        </OperationValueRegex>
    </xsl:template>
</xsl:stylesheet>
</localEntry>

sample request and response Request:

{
    "OperationValueRegex" : {
        "Value" : "123"
    }
}

Expected Result:

<OperationValueRegex>
        <Result>123</Result>
</OperationValueRegex>

response when string

<OperationValueRegex>
        <Result></Result>
</OperationValueRegex>

THanks

1 Answers

As you didn't list which version of the WSO2 product you're using i'm going by the EI 6.6.0 but this has been the same for quite a few versions.

With your input:

{
    "OperationValueRegex" : {
        "Value" : "123"
    }
}

Inside the WSO2 product it will look like:

<jsonObject>
   <OperationValueRegex>
        <Value>123</Value>
    </OperationValueRegex>
</jsonObject>

So your xpath searching for the value is probably not finding any value currently.

/jsonObject/OperationValueRegex/Value != 0

Will likely find you a result, if not log the payload just before the xslt with <log level="full"/> to check the xml representation of your json.

Reference: https://docs.wso2.com/display/EI600/JSON+Support

Related