使用在wso2 esb上运行的任何脚本更改wso2 esb中的xml的内部标记值或节点值

时间:2018-11-16 10:04:34

标签: xml xslt integration wso2esb

这是我的源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

我尝试做丰富的调解员替换属性。但这并没有更改我的Source XML中的任何标签值。请提出实现此目标的任何方法 我的目标xml应该是

<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来做到这一点。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

有趣的问题。有几种可用的路线。我本来建议将recordId作为变量传递给xslt,但经过仔细检查,我发现您需要替换多个'recordId'元素以替换可能不同的地域代码,这当然并不简单。

在这种情况下,建议您将xslt与查找表一起使用。由于我自己没有使用过其中的一个,所以我很好奇,所以我摆弄一些示例,并提出了以下建议:

<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'总是得到相同的数字,因此希望对您有所帮助!至少这很有趣:)

答案 1 :(得分:0)

正如您指出的,上述解决方案不适用于您的特定用例,我提出了一个使用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>

请注意,您必须在配置/数据源中定义数据库连接。