Gleb Belyaev
Gleb Belyaev

Reputation: 1049

How to make a dynamic xPath and execute it?

I collect string to xpath

<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>

and I want to run this xpath and write the value of a Property

<property name="KEY" expression="get-property('xPathElemet')"/>

but receive only collected a string

how to xpath of the Property?

example code sequence :

<iterate continueParent="true" expression="//t:Employee">
    <target>
        <sequence>
            <call-template target="save_element">
                <with-param name="key_element" value="Name"/>
            </call-template>
        </sequence>
    </target>
</iterate>

example code template :

<template xmlns="http://ws.apache.org/ns/synapse" name="save_element">
    <parameter name="key_element"/>  <!--example: "Name"-->
    <sequence>
        <property name="KEY" expression="fn:concat(//t:Employee/t:SNILS, ':' ,$func:key_element)" scope="default"
                  type="STRING"/>             <!--example: "111-111-111-1:Name"-->
        <property name="xPathElemet" expression="fn:concat('//t:Employee/t:', $func:element)"/>      <!--example: "//t:Employee/t:Name"-->
        <property name="VALUE" expression="get-property('xPathElemet')" scope="default" type="STRING"/>      <!--example: Den (Now it does not work)-->
        <dbreport>
            <connection>
                <pool>
                    <seetings/>
                </pool>
            </connection>
            <statement>
                <sql>
                    <![CDATA[insert into cache( key , value ) values (?, ?);]]></sql>      <!--insert new line where key = "111-111-111-1:Name" and value = "Den"-->
                <parameter expression="get-property('KEY')" type="VARCHAR"/>
                <parameter expression="get-property('VALUE')" type="VARCHAR"/>
            </statement>
        </dbreport>
    </sequence>
</template>

example xml:

<Employees xmlns="https://services">
    <Employee>
        <SNILS>111-111-111-1</SNILS>
        <Name>Den</Name>
    </Employee>
    <Employee>
        <SNILS>111-111-111-2</SNILS>
        <Name>Elena</Name>
    </Employee>
</Employees>

Upvotes: 1

Views: 693

Answers (2)

tyfyh
tyfyh

Reputation: 333

Use evaluate

In your case:

<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>
<property name="KEY" expression="evaluate(get-property('xPathElemet'))"/>

You can find more infomation in this blog.

Upvotes: 3

Nadeesha
Nadeesha

Reputation: 885

Please try the following and see if it works

<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>
<property xmlns:t="https://services" name="xPathfull" expression="fn:concat(get-property('xPathElemet'),'/text()')"/>
<property name="KEY" expression="get-property('xPathfull')"/>

What was missing from your code was the /text() which refer to the parameters of the given xpath. Please try this

Upvotes: 0

Related Questions