【问题标题】:change inside tag values or node values of xml in wso2 esb using any script works on wso2 esb使用适用于 wso2 esb 的任何脚本更改 wso2 esb 中 xml 的内部标记值或节点值
【发布时间】:2018-11-27 21:54:02
【问题描述】:

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

我尝试使用丰富的中介替换属性。但它不会更改我的源 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 来做到这一点。 提前致谢。

【问题讨论】:

    标签: xml xslt integration wso2esb


    【解决方案1】:

    有趣的问题。有几条路线可供选择。我本来建议将 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”总是产生相同的数字,所以希望这会有所帮助!至少它很有趣:)

    【讨论】:

    • 非常感谢的回复,但“IND”和“AFG”会根据系统而变化。它就像一个数据库表,其中包含 table.Country_Name 作为 IND 这是一个 unique Key 并且我需要 table.ID 从应该被替换的表中。
    • 是否可以在 XSLT 中编写 Select Query
    • 我认为不可能从 xslt 中调用 dblookup。假设这是一个不经常更改的有限列表,是否可以将 DB 表中的所有元素添加到 xslt?
    • 不。这不是一张桌子。我需要让它通用
    • 好的,我有一些想法,今天晚些时候会尝试发布更新。
    【解决方案2】:

    正如您所指出的,上述解决方案不适用于您的特定用例,我使用 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 中定义数据库连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多