Java 如何使用WSO2 ESB XSLT中介添加<;?xml多重?>;元素到DSS输出的特定位置?

Java 如何使用WSO2 ESB XSLT中介添加<;?xml多重?>;元素到DSS输出的特定位置?,java,xslt,wso2,wso2esb,esb,Java,Xslt,Wso2,Wso2esb,Esb,我对WSO2 ESB\DSS有以下问题:这个问题在YENLO上的Beautifull帖子中得到了很好的解释: 在我的具体案例中,这与以下事实有关:当我在ESB流中使用DSS执行查询,然后将获得的XML文档转换为JSON文档时,此JSON的格式将不同,具体取决于返回的实体数。例如,当返回多个条目时,我通过DSS执行查询并获得以下结果: <VaccinationDetails xmlns="http://ws.wso2.org/dataservice"> <Vaccinati

我对WSO2 ESB\DSS有以下问题:这个问题在YENLO上的Beautifull帖子中得到了很好的解释:

在我的具体案例中,这与以下事实有关:当我在ESB流中使用DSS执行查询,然后将获得的XML文档转换为JSON文档时,此JSON的格式将不同,具体取决于返回的实体数。例如,当返回多个条目时,我通过DSS执行查询并获得以下结果:

<VaccinationDetails xmlns="http://ws.wso2.org/dataservice">
   <VaccinationDetails>
      <vaccination_id>1</vaccination_id>
      <vaccination_name_en>Antrax</vaccination_name_en>
      <vaccination_name>Antrax</vaccination_name>
      <description xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
      <vaccination_goal xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
      <vaccination_coverage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
      <image_link>https://firebasestorage.googleapis.com/v0/b/myproject.appspot.com/o/img%2Ficons%2Flivestock%2Fvacccination.png?alt=media&amp;token=19b15f9a-4706-4037-a928-c8151c823077</image_link>
      <DiseaseCoveredByAVaccinationInfo>
         <DiseaseCoveredByAVaccinationInfoList>
            <disease_id>6</disease_id>
            <disease_name_en>Antrax</disease_name_en>
            <disease_name>Antrax</disease_name>
            <description>Anthrax is an infection caused by the bacterium Bacillus anthracis. It can occur in four forms: skin, lungs, intestinal, and injection. Symptoms begin between one day and two months after the infection is contracted. The skin form presents with a small blister with surrounding swelling that often turns into a painless ulcer with a black center. The inhalation form presents with fever, chest pain, and shortness of breath. The intestinal form presents with diarrhea which may contain blood, abdominal pains, and nausea and vomiting. The injection form presents with fever and an abscess at the site of drug injection.</description>
            <image_link>https://firebasestorage.googleapis.com/v0/b/myproject/o/assets%2FLivestock%2FDisease%2Fanthrax.png?alt=media&amp;token=3a7245ca-f003-4823-b39f-cfaef539e98c</image_link>
         </DiseaseCoveredByAVaccinationInfoList>
      </DiseaseCoveredByAVaccinationInfo>
      <DiseaseCoveredByAVaccinationInfo>
         <DiseaseCoveredByAVaccinationInfoList>
            <livestock_species_id>1</livestock_species_id>
            <parent_livestock_species_id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <livestock_species_name_en>Cow</livestock_species_name_en>
            <livestock_species_name>Cow</livestock_species_name>
            <description>Cattle, cows are the most common type of large domesticated ungulates. They are a prominent modern member of the subfamily Bovinae, are the most widespread species of the genus Bos, and are most commonly classified collectively as Bos taurus. Cattle are commonly raised as livestock for meat (beef and veal), as dairy animals for milk and other dairy products, and as draft animals (oxen or bullocks that pull carts, plows and other implements). Other products include leather and dung for manure or fuel. In some regions, such as parts of India, cattle have significant religious meaning.</description>
            <image_link>https://firebasestorage.googleapis.com/v0/b/myproject/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&amp;token=c21866df-448a-4a72-9da2-d55d87f8b31c</image_link>
            <ls_vaccination_id>1</ls_vaccination_id>
         </DiseaseCoveredByAVaccinationInfoList>
      </DiseaseCoveredByAVaccinationInfo>
      <DiseaseCoveredByAVaccinationInfo>
         <DiseaseCoveredByAVaccinationInfoList>
            <livestock_species_id>1</livestock_species_id>
            <parent_livestock_species_id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <livestock_species_name_en>Cow</livestock_species_name_en>
            <livestock_species_name>Cow</livestock_species_name>
            <description>Cattle, cows are the most common type of large domesticated ungulates. They are a prominent modern member of the subfamily Bovinae, are the most widespread species of the genus Bos, and are most commonly classified collectively as Bos taurus. Cattle are commonly raised as livestock for meat (beef and veal), as dairy animals for milk and other dairy products, and as draft animals (oxen or bullocks that pull carts, plows and other implements). Other products include leather and dung for manure or fuel. In some regions, such as parts of India, cattle have significant religious meaning.</description>
            <image_link>https://firebasestorage.googleapis.com/v0/b/myproject/o/img%2Ficons%2Flivestock%2Fcow.png?alt=media&amp;token=c21866df-448a-4a72-9da2-d55d87f8b31c</image_link>
            <ls_vaccination_id>1</ls_vaccination_id>
         </DiseaseCoveredByAVaccinationInfoList>
      </DiseaseCoveredByAVaccinationInfo>
      <LivestockVaccinationTimeFrameInfo/>
   </VaccinationDetails>
</VaccinationDetails>
如果在XML文档中我有多个

JSON对话将创建一个JSON数组,类似于:

"DiseaseCoveredByAVaccinationInfoList":  
[

{
    "disease_id": 6,
    "disease_name_en": "YYY",
    "disease_name": "YYY",
    "description": "DESCRIPTION",
    "image_link": "ULR"
},
{
    "disease_id": 7,
    "disease_name_en": "XXX",
    "disease_name": "XXX",
    "description": "DESCRIPTION",
    "image_link": "ULR"
},
]
阅读之前的YENLO帖子,我似乎必须添加以下XML属性:

<?xml-multiple?>

我想我需要这样的东西:

"DiseaseCoveredByAVaccinationInfoList": {
    "disease_id": 6,
    "disease_name_en": "Antrax",
    "disease_name": "Antrax",
    "description": "Anthrax is an infection caused by the bacterium Bacillus anthracis. It can occur in four forms: skin, lungs, intestinal, and injection. Symptoms begin between one day and two months after the infection is contracted. The skin form presents with a small blister with surrounding swelling that often turns into a painless ulcer with a black center. The inhalation form presents with fever, chest pain, and shortness of breath. The intestinal form presents with diarrhea which may contain blood, abdominal pains, and nausea and vomiting. The injection form presents with fever and an abscess at the site of drug injection.",
    "image_link": "https://firebasestorage.googleapis.com/v0/b/my-project.appspot.com/o/assets%2FLivestock%2FDisease%2Fanthrax.png?alt=media&token=3a7245ca-f003-4823-b39f-cfaef539e98c"
}
<?xml-multiple?><DiseaseCoveredByAVaccinationInfoList></DiseaseCoveredByAVaccinationInfoList>

因此,当它转换为JSON时,该元素将始终放在JSON数组中,而不是作为单个元素

我还发现了另一个帖子:

参考上一个YENLO版本,它说使用XSLT获取SOAP响应(DSS输出)并添加xml多个处理指令。但我真的不知道要实现这个行为,并在ESB流中使用XSLT来添加这个属性,我必须做些什么

在我的ESB流中,我定义了以下API:

<resource methods="GET" protocol="http" uri-template="/country/{country_id}/vaccination/{vaccination_id}?lang={lang_id}">
    <inSequence>
        <log level="full"/>
        <property expression="get-property('uri.var.country_id')" name="country_id" scope="default" type="STRING"/>
        <property expression="get-property('uri.var.vaccination_id')" name="vaccination_id" scope="default" type="STRING"/>
        <property expression="get-property('uri.var.lang_id')" name="lang_id" scope="default" type="STRING"/>
        <log level="custom">
            <property expression="$ctx:country_id" name="country_id"/>
            <property expression="$ctx:vaccination_id" name="vaccination_id"/>
            <property expression="$ctx:lang_id" name="lang_id"/>
        </log>
        <property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
        <payloadFactory media-type="xml">
            <format>
                <ds:GetVaccinationDetails xmlns:ds="http://ws.wso2.org/dataservice">
                    <ds:vaccination_id>$1</ds:vaccination_id>
                    <ds:language_id>$2</ds:language_id>
                    <ds:country_id>$3</ds:country_id>
                </ds:GetVaccinationDetails>
            </format>
            <args>
                <arg evaluator="xml" expression="$ctx:vaccination_id"/>
                <arg evaluator="xml" expression="$ctx:country_id"/>
                <arg evaluator="xml" expression="$ctx:lang_id"/> 
            </args>
        </payloadFactory>
        <header name="Action" scope="default" value="urn:GetVaccinationDetails"/>
        <call>
            <endpoint key="livestock_Endpoint"/>
        </call>
        <log level="full"/>
        <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
        <property expression="json-eval($.)" name="JSONPayload" scope="default" type="STRING"/>


        <property name="RESPONSE" scope="default" type="STRING" value="true"/>
        <header action="remove" name="To" scope="default"/>
        <send/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
</resource>

$1
$2
$3
我认为我必须在DSS调用之后添加一个XSLT中介,但阅读文档和示例后,我无法理解具体的工作原理,以及如何在转换为JSON文档之前将这个xml multiple元素添加到原始xml中


我错过了什么?如何实现此行为?

这是一个常见的问题,如果它是单个元素,那么在从XML转换为JSON时将不会添加数组

使用XSLT中介进行转换总是比允许ESB转换负载更好

下面我提供了示例XSLT,请尝试并提供您的反馈

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" xmlns:as="http://schema.ttmw.com/Axon" xmlns:env="http://schema.concierge.com/Envelope" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text" indent="yes" media-type="application/json" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">{ 
        "VaccinationDetails":{
        "DiseaseCoveredByAVaccinationInfo":[

        <xsl:for-each select="//DiseaseCoveredByAVaccinationInfoList">
        {
        "DiseaseCoveredByAVaccinationInfoList":{
        "disease_id":"<xsl:value-of select="//disease_id"/>"        
        }
        }
       <xsl:if test="position()!=last()">,</xsl:if> 

        </xsl:for-each>        
        ]      
        }
        }
    </xsl:template>
     </xsl:stylesheet>

{ 
“疫苗接种详情”:{
“被AvaccinationInfo覆盖的疾病”:[
{
“由Avaccination信息列表覆盖的疾病”:{
“疾病id”:”
}
}
, 
]      
}
}

我已通过以下链接解决了此问题。在这里,我使用了XSLT和“处理指令”来实现这一点。还需要启用synapse.json.to.xml.processing.instruction.enabled=true属性

我不知道为什么会被否决。我不使用WSO2,但我使用StAXON,这是正确的解决方案。StAXON可能配置了数组自动检测,所以如果有多个元素,它可以检测数组,但当有单个元素时就不能检测。PI总是强制创建数组,实际上不需要“if”。您还可以选择使用“element name”指定数组中的元素。
<resource methods="GET" protocol="http" uri-template="/country/{country_id}/vaccination/{vaccination_id}?lang={lang_id}">
    <inSequence>
        <log level="full"/>
        <property expression="get-property('uri.var.country_id')" name="country_id" scope="default" type="STRING"/>
        <property expression="get-property('uri.var.vaccination_id')" name="vaccination_id" scope="default" type="STRING"/>
        <property expression="get-property('uri.var.lang_id')" name="lang_id" scope="default" type="STRING"/>
        <log level="custom">
            <property expression="$ctx:country_id" name="country_id"/>
            <property expression="$ctx:vaccination_id" name="vaccination_id"/>
            <property expression="$ctx:lang_id" name="lang_id"/>
        </log>
        <property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
        <payloadFactory media-type="xml">
            <format>
                <ds:GetVaccinationDetails xmlns:ds="http://ws.wso2.org/dataservice">
                    <ds:vaccination_id>$1</ds:vaccination_id>
                    <ds:language_id>$2</ds:language_id>
                    <ds:country_id>$3</ds:country_id>
                </ds:GetVaccinationDetails>
            </format>
            <args>
                <arg evaluator="xml" expression="$ctx:vaccination_id"/>
                <arg evaluator="xml" expression="$ctx:country_id"/>
                <arg evaluator="xml" expression="$ctx:lang_id"/> 
            </args>
        </payloadFactory>
        <header name="Action" scope="default" value="urn:GetVaccinationDetails"/>
        <call>
            <endpoint key="livestock_Endpoint"/>
        </call>
        <log level="full"/>
        <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
        <property expression="json-eval($.)" name="JSONPayload" scope="default" type="STRING"/>


        <property name="RESPONSE" scope="default" type="STRING" value="true"/>
        <header action="remove" name="To" scope="default"/>
        <send/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
</resource>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" xmlns:as="http://schema.ttmw.com/Axon" xmlns:env="http://schema.concierge.com/Envelope" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text" indent="yes" media-type="application/json" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">{ 
        "VaccinationDetails":{
        "DiseaseCoveredByAVaccinationInfo":[

        <xsl:for-each select="//DiseaseCoveredByAVaccinationInfoList">
        {
        "DiseaseCoveredByAVaccinationInfoList":{
        "disease_id":"<xsl:value-of select="//disease_id"/>"        
        }
        }
       <xsl:if test="position()!=last()">,</xsl:if> 

        </xsl:for-each>        
        ]      
        }
        }
    </xsl:template>
     </xsl:stylesheet>