简体   繁体   中英

WSO2 Payload Factory preserving tag from xml response

In my proxy i call a remote rest service which responses me with a message like this one:

<omim version="1.0">
<entryList>
  <entry>
    <prefix>#</prefix>
    <mimNumber>230900</mimNumber>
    <status>live</status>
    <titles>
        <preferredTitle>GAUCHER DISEASE, TYPE II</preferredTitle>
        <alternativeTitles>GD II;;</alternativeTitles>
     </titles>
     <clinicalSynopsis>
         <inheritance>Autosomal recessive</inheritance>
         <growthWeight>Poor weight gain </growthWeight>
          <growthOther>Failure to thrive </growthOther>
          <headAndNeckEyes>Convergent squint</headAndNeckEyes>
          <headAndNeckMouth>Trismus </headAndNeckMouth>
          <respiratory>Apnea</respiratory>
          <respiratoryLarynx>Laryngeal spasms</respiratoryLarynx>
          <respiratoryLung>Recurrent aspiration pneumonia</respiratoryLung>
          <abdomenExternalFeatures>Protruberantabdomen</abdomenExternalFeatures>
          <abdomenLiver>Hepatomegaly</abdomenLiver>
          <abdomenSpleen>Splenomegaly</abdomenSpleen>
          <abdomenGastrointestinal>Dysphagia </abdomenGastrointestinal>
          <neurologicCentralNervousSystem>Progressive neurologic deterioration</neurologicCentralNervousSystem>
          <hematology>Thrombocytopenia</hematology>
          <laboratoryAbnormalities>Decreased acid beta galactosidase protein and activity</laboratoryAbnormalities>
          <miscellaneous>Onset between 3 and 6 months of age</miscellaneous>
          <molecularBasis>Caused by mutation in the acid beta-glucosidase gene</molecularBasis>
     </clinicalSynopsis>
    </entry>
  </entryList>
</omim>

I need to filter some data from this message and build a custom message having this structure:

<clinicalManifestation>
    <nonNeurological>
         $1
    </nonNeurological>
</clinicalManifestation>

Where $1, perhaps using payload factory? should be filled with some leaves extracted frome the previous response message, for example like this:

<clinicalManifestation>
    <nonNeurological>
         <growthWeight>Poor weight gain </growthWeight>
          <growthOther>Failure to thrive </growthOther>
          <headAndNeckEyes>Convergent squint</headAndNeckEyes>
          <headAndNeckMouth>Trismus </headAndNeckMouth>
          <respiratory>Apnea</respiratory>
          <respiratoryLarynx>Laryngeal spasms</respiratoryLarynx>
          <respiratoryLung>Recurrent aspiration pneumonia</respiratoryLung>
          <abdomenExternalFeatures>Protruberantabdomen</abdomenExternalFeatures>
          <abdomenLiver>Hepatomegaly</abdomenLiver>
          <abdomenSpleen>Splenomegaly</abdomenSpleen>
          <abdomenGastrointestinal>Dysphagia </abdomenGastrointestinal>
    </nonNeurological>
</clinicalManifestation>

Have i to use payload factory using a particular xpath expression in the arqs declaration (what expression? ). Or should i have to use another mediator? Which one? Can you give me and example please?

You need to specify them as arguments. Let me extract a code from the original documentation as an example. Here the expression means the xpath expression.

<payloadFactory>
      <format>
           <m:checkpriceresponse xmlns:m="http://services.samples/xsd">
               <m:code>$1</m:code>
               <m:price>$2</m:price>
           </m:checkpriceresponse>
      </format>
      <args>
           <arg expression="//m0:symbol" xmlns:m0="http://services.samples/xsd">
           <arg expression="//m0:last" xmlns:m0="http://services.samples/xsd">
      </arg></arg></args>
</payloadFactory>

Firstly ,in your outSequence (after you've gotten response) you would need to extract the xml tags/field's from the that response , and store them in properties so we can use them later...

For example (using xpath):

<property name="growthWeight" expression="//omin/entryList/entry/clinincalSynopsis/growthWeight/text()" scope="default" type="STRING" />
<property name="growthOth" expression="//omin/entryList/entry/clinincalSynopsis/growthOther/text()" scope="default" type="STRING" /> 

etc etc , if that xpath doesn't work see (see http://www.w3schools.com/xpath/xpath_syntax.asp )

You can print these properties to the log to see if they are setting correctly :

<log level="custom">
     <property xmlns:ns="http://org.apache.synapse/xsd" name="Extracted:  Growth weight= " expression="$ctx:growthWeight"/>
     <property xmlns:ns="http://org.apache.synapse/xsd" name="Extracted:  Growth other= " expression="$ctx:growthOth"/>
</log>

Then in your payload factory, you can set these properties as the parameters of your final response to the client :

<payloadFactory media-type="xml">
    <format>
        <clinicalManifestation>
            <nonNeurological>
                <growthWeight>$1</growthWeight>
                <growthOther>$2</growthOther>
                    :
                    :
                    :
            </nonNeurological>
        </clinicalManifestation>
    </format>
    <args>
        <arg evaluator="xml"  expression="$ctx:growthWeight" />
        <arg evaluator="xml"  expression="$ctx:growthOth" />
                    :
                    :
                    :
    </args>
</payloadFactory>

thanks to all of you. I found a better method using the xslt mediator. I wrote my xsl file and simply by adding this in my proxy:

<xslt key="myXSL" />

i processed the xml in a more efficient way... the mediator provides me more flexibility.

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