-1

I have the following XML coming from a Database query and need to send the result to an API call. But I want to transform data in a different format, how do I do that. Any help would be appreciated.

Result from Database:

    `<Data>  
      <User Name="XYZ">
        <DETAILS>
           <COURSE>IS</COURSE>
           <YEAR>2016</YEAR>
           <SUBJECT>COMPUTER SCIENCE<SUBJECT>         
         </DETAILS>
       </User>
       <User Name="XYZ">
        <DETAILS>
           <COURSE>IS</COURSE>
           <YEAR>2015</YEAR>
           <SUBJECT>INFO SCIENCE<SUBJECT>         
       </DETAILS>
      </User>
     </Data> `

How I need to Transform: <Data>
<User Name="XYZ"> <DETAILS> <COURSE>IS</COURSE> <YEAR>2016</YEAR> <SUBJECT>COMPUTER SCIENCE<SUBJECT> </DETAILS> <DETAILS> <COURSE>IS</COURSE> <YEAR>2015</YEAR> <SUBJECT>INFO SCIENCE<SUBJECT>
</DETAILS> </User> </Data>

4

2 回答 2

0

I assume you have unique User with attribute in message. If there is User with Name="XYZ" that guarantees that document doesn't contain any another User with attribute Name!="XYZ"

You can use next xslt transformation

<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="Data">
    <xsl:element name="{local-name(User[last()])}">
        <xsl:copy-of select="User/@*" />
        <xsl:copy-of select="User/*" />
    </xsl:element>
</xsl:template>

于 2017-06-13T15:40:55.327 回答
0

ForEach mediator is for transforming segments of data and replacing them in the original payload. Iterate mediator is not used for transformations as such. It is used for calling backends and aggregating responses. Please refer https://docs.wso2.com/display/ESB500/ForEach+Mediator for more details.

From the sample you have added here, seems like you need to merge results by the username. Hence you cannot use ForEach, Iterate mediators. However, you can use Script mediator, but it is better to use XSLT mediator since it is a direct and easy XML manipulation.

于 2017-06-14T02:52:16.093 回答