我在 WSO2 中绝对是新的,我对我正在进行的 ESB 项目有以下疑问。
我必须迭代以下 XML 文档的每个<Sample>...</Sample>
元素(来自 DSS 服务):
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<Samples xmlns="http://ws.wso2.org/dataservice">
<Sample>
<sample_id>XXX00001</sample_id>
<processed>n</processed><sample_doi>sampleid001</sample_doi><date>2000-01-01</date><hold_wiews>hold_wiews-0001</hold_wiews><hold_pid>hold_pid-001</hold_pid><hold_name>hold_name-001</hold_name><hold_address>hold_address-001</hold_address><hold_country>001</hold_country><hold_lat>hold_lat-1</hold_lat><hold_lon>hold_lon-1</hold_lon><method>acqu</method><genus>genus-001</genus><species>species-001</species><sp_auth>sp_auth-001</sp_auth><subtaxa>subtaxa-001</subtaxa><st_auth>st_auth-001</st_auth><bio_status>001</bio_status><mls_status>01</mls_status><prov_sid>prov_sid-001</prov_sid><provenance>pr1</provenance><coll_sid>coll_sid-001</coll_sid><coll_miss_id>coll_miss_id-001</coll_miss_id><coll_site>coll_site-001</coll_site><coll_lat>coll_lat-1</coll_lat><coll_lon>coll_lon-1</coll_lon><coll_uncert>coll_uncert-001</coll_uncert><coll_datum>coll_datum-001</coll_datum><coll_georef>coll_georef-001</coll_georef><coll_elevation>1</coll_elevation><coll_date>2001-01-01</coll_date><coll_source>s1</coll_source><ancestry>ancestry-001</ancestry>
</Sample>
<Sample>
<sample_id>XXX00002</sample_id>
<processed>n</processed><sample_doi>sampleid002</sample_doi><date>2000-01-02</date><hold_wiews>hold_wiews-0002</hold_wiews><hold_pid>hold_pid-002</hold_pid><hold_name>hold_name-002</hold_name><hold_address>hold_address-002</hold_address><hold_country>002</hold_country><hold_lat>hold_lat-2</hold_lat><hold_lon>hold_lon-2</hold_lon><method>acqu</method><genus>genus-002</genus><species>species-002</species><sp_auth>sp_auth-002</sp_auth><subtaxa>subtaxa-002</subtaxa><st_auth>st_auth-002</st_auth><bio_status>002</bio_status><mls_status>02</mls_status><prov_sid>prov_sid-002</prov_sid><provenance>pr2</provenance><coll_sid>coll_sid-002</coll_sid><coll_miss_id>coll_miss_id-002</coll_miss_id><coll_site>coll_site-002</coll_site><coll_lat>coll_lat-2</coll_lat><coll_lon>coll_lon-2</coll_lon><coll_uncert>coll_uncert-002</coll_uncert><coll_datum>coll_datum-002</coll_datum><coll_georef>coll_georef-002</coll_georef><coll_elevation>2</coll_elevation><coll_date>2001-01-02</coll_date><coll_source>s2</coll_source><ancestry>ancestry-002</ancestry>
</Sample>
<Sample>
<sample_id>XXX00003</sample_id>
<processed>n</processed><sample_doi>sampleid003</sample_doi><date>2000-01-03</date><hold_wiews>hold_wiews-0003</hold_wiews><hold_pid>hold_pid-003</hold_pid><hold_name>hold_name-003</hold_name><hold_address>hold_address-003</hold_address><hold_country>003</hold_country><hold_lat>hold_lat-3</hold_lat><hold_lon>hold_lon-3</hold_lon><method>acqu</method><genus>genus-003</genus><species>species-003</species><sp_auth>sp_auth-003</sp_auth><subtaxa>subtaxa-003</subtaxa><st_auth>st_auth-003</st_auth><bio_status>003</bio_status><mls_status>03</mls_status><prov_sid>prov_sid-003</prov_sid><provenance>pr3</provenance><coll_sid>coll_sid-003</coll_sid><coll_miss_id>coll_miss_id-003</coll_miss_id><coll_site>coll_site-003</coll_site><coll_lat>coll_lat-3</coll_lat><coll_lon>coll_lon-3</coll_lon><coll_uncert>coll_uncert-003</coll_uncert><coll_datum>coll_datum-003</coll_datum><coll_georef>coll_georef-003</coll_georef><coll_elevation>3</coll_elevation><coll_date>2001-01-03</coll_date><coll_source>s3</coll_source><ancestry>ancestry-003</ancestry>
</Sample>
</Samples>
</soapenv:Body>
</soapenv:Envelope>
要做到这一点,我正在使用迭代中介(它工作得非常好),如下所示:
<iterate expression="$body//ds:Sample" id="ITR_AGG" xmlns:ds="http://ws.wso2.org/dataservice">
<target>
<sequence>
<enrich>
<source clone="true" type="envelope"/>
<target property="sampleData" type="property"/>
</enrich>
<!-- Log Sample Data -->
<log level="custom">
<property expression="$ctx:sampleData" name="sample data"/>
</log>
...........................................................
...........................................................
DO SOME OTHER STUFF
...........................................................
...........................................................
</sequence>
</target>
</iterate>
它工作正常,但我有些疑惑,我试图解释这些疑惑。
迭代介体实现了 Splitter企业集成模式**,它用于将将消息发送到从父消息派生的许多不同消息中**
好的,我将基本上包含在<soapenv:Body>---</soapenv:Body>
容器中的原始邮件拆分为3 <Sample>...</Sample>
分隔邮件。
它有效,但为什么迭代表达式定义在迭代器介体中:
<iterate expression="$body//ds:Sample" id="ITR_AGG" xmlns:ds="http://ws.wso2.org/dataservice">
是 $ body // ds:示例?
好的,所以我认为我必须使用 ds:前缀,因为它来自数据服务(告诉我,如果我做错了断言)。但究竟代表 $ body “变量”的是什么? (这是变量吗?)
它是否代表必须在<soapenv:Body>
元素中搜索必须被迭代的<Sample>
元素?或者是什么?如果我的假设是正确的,<soapenv:Body>
如何与 $ body “变量”相关联?
第二个疑问与迭代调解员开头的充实调解员的使用有关:
<enrich>
<source clone="true" type="envelope"/>
<target property="sampleData" type="property"/>
</enrich>
究竟是什么?在我看来,它正在将正在迭代的当前元素复制到名为 sampleData 的新属性中,事实上在此中介之后,记录器记录了 sampleData 属性并且它印刷的东西是这样的:
sample data =
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<Sample xmlns="http://ws.wso2.org/dataservice">
<sample_id>XXX00001</sample_id>
<processed>n</processed><sample_doi>sampleid001</sample_doi><date>2000-01-01</date><hold_wiews>hold_wiews-0001</hold_wiews><hold_pid>hold_pid-001</hold_pid><hold_name>hold_name-001</hold_name><hold_address>hold_address-001</hold_address><hold_country>001</hold_country><hold_lat>hold_lat-1</hold_lat><hold_lon>hold_lon-1</hold_lon><method>acqu</method><genus>genus-001</genus><species>species-001</species><sp_auth>sp_auth-001</sp_auth><subtaxa>subtaxa-001</subtaxa><st_auth>st_auth-001</st_auth><bio_status>001</bio_status><mls_status>01</mls_status><prov_sid>prov_sid-001</prov_sid><provenance>pr1</provenance><coll_sid>coll_sid-001</coll_sid><coll_miss_id>coll_miss_id-001</coll_miss_id><coll_site>coll_site-001</coll_site><coll_lat>coll_lat-1</coll_lat><coll_lon>coll_lon-1</coll_lon><coll_uncert>coll_uncert-001</coll_uncert><coll_datum>coll_datum-001</coll_datum><coll_georef>coll_georef-001</coll_georef><coll_elevation>1</coll_elevation><coll_date>2001-01-01</coll_date><coll_source>s1</coll_source><ancestry>ancestry-001</ancestry>
</Sample>
</soapenv:Body>
</soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
表示我正在迭代的当前元素。这是我的推理正确吗?
答案 0 :(得分:1)
您可以使用任何字符串作为前缀,只需确保在xmlns之后使用相同的字符串:
xmlns:ds="http://ws.wso2.org/dataservice"
前:
<iterate expression="$body//mypref:Sample" id="ITR_AGG" xmlns:mypref="http://ws.wso2.org/dataservice">
$ body是表示正文消息的synapse的预定义xpath表达式:
https://docs.wso2.com/display/ESB500/Synapse+XPath+Variables#SynapseXPathVariables- $体
富裕调解员:
<enrich>
<source clone="true" type="envelope"/>
<target property="sampleData" type="property"/>
</enrich>
只需复制传入消息的内容,整个有效负载包括名为sampleData的属性中的soapevelop。