如何制作动态xPath并执行它?

时间:2013-07-12 07:52:34

标签: properties wso2 wso2esb synapse

我收集字符串到xpath

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

我希望运行此xpath并写入Property

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

但只收到一个字符串

如何使用属性的xpath?

示例代码序列:

<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>

示例代码模板:

<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>

示例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>

2 个答案:

答案 0 :(得分:3)

使用评估

在你的情况下:

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

您可以在此blog.

中找到更多信息

答案 1 :(得分:0)

请尝试以下操作,看看是否有效

<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')"/>

代码中缺少的是/text(),它引用了给定xpath的参数。请试试这个