使用任何脚本更改 wso2 esb 中 xml 的内部标记值或节点值适用于 wso2 esb
change inside tag values or node values of xml in wso2 esb using any script works on wso2 esb
这是我的来源XML
<request>
<applicationVersion>agidmp-5.0.0.0</applicationVersion>
<serviceName>changeRequestService</serviceName>
<changes>
<change>
<entityName>ChangeRequest</entityName>
<path>ChangeRequest--4</path>
<operation>a</operation>
<values>
<changeRequestName>ChangeRequest-CR-007</changeRequestName>
<changeRequestNumber>ChangeRequest-CR-007</changeRequestNumber>
<changeRequestUID>ChangeRequest-CR-007</changeRequestUID>
<productCategory>20984</productCategory>
<requestCategory.recordId>20032</requestCategory.recordId>
<sourceSystem.recordId>20048</sourceSystem.recordId>
<scopeDescription> Minimum age limit:15Years</scopeDescription>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--7</path>
<operation>a</operation>
<values>
<country.recordId>IND</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--8</path>
<operation>a</operation>
<values>
<country.recordId>AFG</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--9</path>
<operation>a</operation>
<values>
<country.recordId>AUT</country.recordId>
</values>
</change>
<change>
<path>ChangeRequest--4.submissionRequestScopeStudyProgramList--5</path>
<operation>a</operation>
<entityName>SubmissionRequestScopeStudyAndProgram</entityName>
<values>
<invStudy.recordId>40037</invStudy.recordId>
</values>
</change>
</changes>
我想替换以下路径中的标签值:/soapenv:Body/request/changes/change/values/country.recordId
我尝试用 enrich mediator 替换 属性。但它没有更改我的源 XML 中的任何标签值。请提出任何方法来实现这一目标
我的目标 xml 应该是 Like
<soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<request>
<applicationVersion>agidmp-5.0.0.0</applicationVersion>
<serviceName>changeRequestService</serviceName>
<changes>
<change>
<entityName>ChangeRequest</entityName>
<path>ChangeRequest--4</path>
<operation>a</operation>
<values>
<changeRequestName>ChangeRequest-CR-007</changeRequestName>
<changeRequestNumber>ChangeRequest-CR-007</changeRequestNumber>
<changeRequestUID>ChangeRequest-CR-007</changeRequestUID>
<productCategory>20984</productCategory>
<requestCategory.recordId>20032</requestCategory.recordId>
<sourceSystem.recordId>20048</sourceSystem.recordId>
<scopeDescription> Minimum age limit:15Years</scopeDescription>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--7</path>
<operation>a</operation>
<values>
<country.recordId>1234</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--8</path>
<operation>a</operation>
<values>
<country.recordId>1235</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--9</path>
<operation>a</operation>
<values>
<country.recordId>1236</country.recordId>
</values>
</change>
<change>
<path>ChangeRequest--4.submissionRequestScopeStudyProgramList--5</path>
<operation>a</operation>
<entityName>SubmissionRequestScopeStudyAndProgram</entityName>
<values>
<invStudy.recordId>40037</invStudy.recordId>
</values>
</change>
</changes>
</request>
</soapenv:Body>
是否可以通过添加一些 javascript 代码或在 wso2 调解器中使用 xQuery 来做到这一点。
提前致谢。
有趣的问题。有几条路线可供选择。我打算建议将 recordId 作为变量传递给 xslt,但经过仔细检查,我发现您需要为可能不同的 landcodes 替换多个 'recordId' 元素,这当然不是微不足道的。
在这种情况下,我建议您使用带有查找功能的 xslt table。由于我自己还没有使用过其中一个,所以我很好奇,所以我摆弄了一些示例并提出了以下建议:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://example.country"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<!-- copy everything that is not country.recordId -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- create a lookup variable based on the table at the bottom -->
<xsl:key name="id-lookup" match="s:id" use="s:abbr"/>
<xsl:variable name="allrecordids" select="document('')/*/s:recordIds"/>
<!-- replace the country.recordIds -->
<xsl:template match="country.recordId">
<xsl:apply-templates select="$allrecordids">
<xsl:with-param name="curr-label" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="s:recordIds">
<xsl:param name="curr-label"/>
<country.recordId><xsl:value-of select="key('id-lookup', $curr-label)/s:nr"/></country.recordId>
</xsl:template>
<!-- the lookup table -->
<s:recordIds>
<s:id><s:abbr>AFG</s:abbr><s:nr>1234</s:nr></s:id>
<s:id><s:abbr>IND</s:abbr><s:nr>1235</s:nr></s:id>
</s:recordIds>
</xsl:stylesheet>
我在这里做了一个很大的假设,即 'IND' 和 'AFG' 总是产生相同的数字,所以希望这对您有所帮助!至少它很有趣 :)
正如您指出的上述解决方案不适用于您的特定用例,我想出了一个使用 foreach 的不同解决方案。
<proxy xmlns="http://ws.apache.org/ns/synapse" name="translate_landcodes" startOnLoad="true" statistics="disable" trace="disable" transports="http,https">
<target>
<inSequence>
<foreach expression="//change">
<sequence>
<filter xpath="//country.recordId">
<then>
<property expression="//country.recordId" name="landcode" scope="default" type="STRING"/>
<sequence key="get_landnumber"/>
<enrich>
<source clone="true" property="landnumber" type="property"/>
<target xpath="//country.recordId"/>
</enrich>
</then>
</filter>
</sequence>
</foreach>
<respond/>
</inSequence>
</target>
<description/>
</proxy>
我运行进入以下错误https://wso2.org/jira/browse/ESBJAVA-5227。这在 IE 6.20 和更新版本中已修复,但我通过将数据库调用移动到单独的序列来解决它:
<sequence name="get_landnumber" xmlns="http://ws.apache.org/ns/synapse">
<dblookup>
<connection>
<pool>
<dsName>jdbc/landcodes_db</dsName>
</pool>
</connection>
<statement>
<sql><![CDATA[select numbercode from translate where lettercode=?]]></sql>
<parameter expression="get-property('landcode')"
type="VARCHAR" xmlns:ns="http://org.apache.synapse/xsd"/>
<result column="numbercode" name="landnumber"/>
</statement>
</dblookup>
</sequence>
请注意,您必须在 configuration/datasources 中定义数据库连接。
这是我的来源XML
<request>
<applicationVersion>agidmp-5.0.0.0</applicationVersion>
<serviceName>changeRequestService</serviceName>
<changes>
<change>
<entityName>ChangeRequest</entityName>
<path>ChangeRequest--4</path>
<operation>a</operation>
<values>
<changeRequestName>ChangeRequest-CR-007</changeRequestName>
<changeRequestNumber>ChangeRequest-CR-007</changeRequestNumber>
<changeRequestUID>ChangeRequest-CR-007</changeRequestUID>
<productCategory>20984</productCategory>
<requestCategory.recordId>20032</requestCategory.recordId>
<sourceSystem.recordId>20048</sourceSystem.recordId>
<scopeDescription> Minimum age limit:15Years</scopeDescription>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--7</path>
<operation>a</operation>
<values>
<country.recordId>IND</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--8</path>
<operation>a</operation>
<values>
<country.recordId>AFG</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--9</path>
<operation>a</operation>
<values>
<country.recordId>AUT</country.recordId>
</values>
</change>
<change>
<path>ChangeRequest--4.submissionRequestScopeStudyProgramList--5</path>
<operation>a</operation>
<entityName>SubmissionRequestScopeStudyAndProgram</entityName>
<values>
<invStudy.recordId>40037</invStudy.recordId>
</values>
</change>
</changes>
我想替换以下路径中的标签值:/soapenv:Body/request/changes/change/values/country.recordId
我尝试用 enrich mediator 替换 属性。但它没有更改我的源 XML 中的任何标签值。请提出任何方法来实现这一目标 我的目标 xml 应该是 Like
<soapenv:Body xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<request>
<applicationVersion>agidmp-5.0.0.0</applicationVersion>
<serviceName>changeRequestService</serviceName>
<changes>
<change>
<entityName>ChangeRequest</entityName>
<path>ChangeRequest--4</path>
<operation>a</operation>
<values>
<changeRequestName>ChangeRequest-CR-007</changeRequestName>
<changeRequestNumber>ChangeRequest-CR-007</changeRequestNumber>
<changeRequestUID>ChangeRequest-CR-007</changeRequestUID>
<productCategory>20984</productCategory>
<requestCategory.recordId>20032</requestCategory.recordId>
<sourceSystem.recordId>20048</sourceSystem.recordId>
<scopeDescription> Minimum age limit:15Years</scopeDescription>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--7</path>
<operation>a</operation>
<values>
<country.recordId>1234</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--8</path>
<operation>a</operation>
<values>
<country.recordId>1235</country.recordId>
</values>
</change>
<change>
<entityName>ChangeRequestScopeCountry</entityName>
<path>ChangeRequest--4.changeRequestCountryList--9</path>
<operation>a</operation>
<values>
<country.recordId>1236</country.recordId>
</values>
</change>
<change>
<path>ChangeRequest--4.submissionRequestScopeStudyProgramList--5</path>
<operation>a</operation>
<entityName>SubmissionRequestScopeStudyAndProgram</entityName>
<values>
<invStudy.recordId>40037</invStudy.recordId>
</values>
</change>
</changes>
</request>
</soapenv:Body>
是否可以通过添加一些 javascript 代码或在 wso2 调解器中使用 xQuery 来做到这一点。 提前致谢。
有趣的问题。有几条路线可供选择。我打算建议将 recordId 作为变量传递给 xslt,但经过仔细检查,我发现您需要为可能不同的 landcodes 替换多个 'recordId' 元素,这当然不是微不足道的。
在这种情况下,我建议您使用带有查找功能的 xslt table。由于我自己还没有使用过其中一个,所以我很好奇,所以我摆弄了一些示例并提出了以下建议:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://example.country"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<!-- copy everything that is not country.recordId -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- create a lookup variable based on the table at the bottom -->
<xsl:key name="id-lookup" match="s:id" use="s:abbr"/>
<xsl:variable name="allrecordids" select="document('')/*/s:recordIds"/>
<!-- replace the country.recordIds -->
<xsl:template match="country.recordId">
<xsl:apply-templates select="$allrecordids">
<xsl:with-param name="curr-label" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="s:recordIds">
<xsl:param name="curr-label"/>
<country.recordId><xsl:value-of select="key('id-lookup', $curr-label)/s:nr"/></country.recordId>
</xsl:template>
<!-- the lookup table -->
<s:recordIds>
<s:id><s:abbr>AFG</s:abbr><s:nr>1234</s:nr></s:id>
<s:id><s:abbr>IND</s:abbr><s:nr>1235</s:nr></s:id>
</s:recordIds>
</xsl:stylesheet>
我在这里做了一个很大的假设,即 'IND' 和 'AFG' 总是产生相同的数字,所以希望这对您有所帮助!至少它很有趣 :)
正如您指出的上述解决方案不适用于您的特定用例,我想出了一个使用 foreach 的不同解决方案。
<proxy xmlns="http://ws.apache.org/ns/synapse" name="translate_landcodes" startOnLoad="true" statistics="disable" trace="disable" transports="http,https">
<target>
<inSequence>
<foreach expression="//change">
<sequence>
<filter xpath="//country.recordId">
<then>
<property expression="//country.recordId" name="landcode" scope="default" type="STRING"/>
<sequence key="get_landnumber"/>
<enrich>
<source clone="true" property="landnumber" type="property"/>
<target xpath="//country.recordId"/>
</enrich>
</then>
</filter>
</sequence>
</foreach>
<respond/>
</inSequence>
</target>
<description/>
</proxy>
我运行进入以下错误https://wso2.org/jira/browse/ESBJAVA-5227。这在 IE 6.20 和更新版本中已修复,但我通过将数据库调用移动到单独的序列来解决它:
<sequence name="get_landnumber" xmlns="http://ws.apache.org/ns/synapse">
<dblookup>
<connection>
<pool>
<dsName>jdbc/landcodes_db</dsName>
</pool>
</connection>
<statement>
<sql><![CDATA[select numbercode from translate where lettercode=?]]></sql>
<parameter expression="get-property('landcode')"
type="VARCHAR" xmlns:ns="http://org.apache.synapse/xsd"/>
<result column="numbercode" name="landnumber"/>
</statement>
</dblookup>
</sequence>
请注意,您必须在 configuration/datasources 中定义数据库连接。