简体   繁体   中英

What should be Switch Mediator's source path?

Scenario I've two XML-based data files, I need to use a switch mediator in my API to direct the xml file to its respective mediation flow which is implemented in the same API. The XMLs are shown below:

XML - 1

<Table>
     <Trademarks>
          <FileNbr>574988</FileNbr>
          <DateOfRegistration>03-DEC-21</DateOfRegistration>
          <Class> 43 </Class>
          <ApplicantName>Ramada International, Inc.</ApplicantName>
          <Address>22 Sylvan Way Parsippany, New Jersey  07054</Address>
          <Title>RAMADA RESIDENCES</Title>
          <Status>Trademark registered</Status>
     </Trademarks>
     <Trademarks>
          <FileNbr>524918</FileNbr>
          <DateOfRegistration>03-DEC-21</DateOfRegistration>
          <Class> 37 </Class>
          <ApplicantName>Muhammad Usman, Trading as, IMAMDIN ASSOCIATES</ApplicantName>
          <Address>Imamdin Associates, 89/6-R,</Address>
          <Title>IMAM DIN</Title>
          <Status>Trademark registered</Status>
     </Trademarks>
</Table>

XML - 2

<Table>
     <Copyright>
          <applicationno>1641/2001</applicationno>
          <applicationdate>11/15/2001 12:00:00 AM</applicationdate>
          <TitleEnglish>DURA PLUS</TitleEnglish>
          <applicantname>FARMIGEA PAKISTAN (PVT) LTD.</applicantname>
          <class>ARTISTIC WORK</class>
          <City>LAHORE</City>
          <Country>PAKISTAN</Country>
          <status>Application Registered</status>
     </Copyright>
     <Copyright>
          <applicationno>1644/2001</applicationno>
          <applicationdate>11/15/2001 12:00:00 AM</applicationdate>
          <TitleEnglish>OCUGEL FARMIGEA</TitleEnglish>
          <applicantname>FARMIGEA PAKISTAN (PVT) LTD.</applicantname>
          <class>ARTISTIC WORK</class>
          <City>LAHORE</City>
          <Country>PAKISTAN</Country>
          <status>Application Registered</status>
     </Copyright>
</Table>

Question

  1. I need to specify the Source XPath for the Switch mediator . In the above scenario how can I differentiate between the two XML data files based on the second nodes ie Trademarks & Copyright. ?
  2. Which property should I used for the above requirement?

You can use the xpath local-name() function, like below. But keep in mind, that only checks the first element after <Table> . I also like to use upper-case() function, to avoid upper/lower case issues. And the cases are with regex start ^ and end $ matches. Working example below.

<switch source="upper-case(local-name(//Table/*))" xmlns:ns="http://org.apache.synapse/xsd">
    <case regex="^TRADEMARKS$">
        <payloadFactory media-type="json">
            <format>{"Trademarks":"DoSomething"}</format>
        </payloadFactory>
    </case>
    <case regex="^COPYRIGHT$">
        <payloadFactory media-type="json">
            <format>{"Copyright":"To something else"}</format>
        </payloadFactory>
    </case>
    <default>
        <payloadFactory media-type="json">
            <format>{"Error":"Invalid table name"}</format>
        </payloadFactory>
    </default>
</switch>
<respond/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM