You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用WSO2 ESB XSLT中介在DSS输出指定位置添加<?xml-multiple?>元素

Fix XML-to-JSON Array/Object Inconsistency in WSO2 ESB/DSS

We’ve all hit this frustrating snag when working with WSO2 ESB and DSS: run a query that sometimes returns multiple records, sometimes just one. When converting the DSS XML response to JSON, multiple entries become an array (exactly what you need), but a single entry gets turned into a plain object. This breaks downstream JSON parsers that expect an array every time—total headache.

The solution is simple: we need to add the <?xml-multiple?> processing instruction right before the XML elements that should always be treated as arrays, no matter how many items they contain. We can do this cleanly using the XSLT Mediator in WSO2 ESB.

Custom XSLT Stylesheet

Here’s a tailored XSLT that targets the specific elements from your example (VaccinationDetails and DiseaseCoveredByAVaccinationInfoList):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ds="http://ws.wso2.org/dataservice">
    <!-- Identity template: copy all nodes by default -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Add xml-multiple before child VaccinationDetails elements -->
    <xsl:template match="ds:VaccinationDetails[parent::ds:VaccinationDetails]">
        <xsl:processing-instruction name="xml-multiple"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Add xml-multiple before DiseaseCoveredByAVaccinationInfoList elements -->
    <xsl:template match="ds:DiseaseCoveredByAVaccinationInfoList">
        <xsl:processing-instruction name="xml-multiple"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

How This Works

  • The identity template ensures we don’t modify any parts of the XML except the elements we explicitly target—so all other data stays intact.
  • The second template matches child VaccinationDetails elements (the actual records under the root wrapper) and inserts the <?xml-multiple?> instruction before each one.
  • The third template does the same for DiseaseCoveredByAVaccinationInfoList elements, ensuring nested lists also behave consistently as arrays.

ESB Flow Configuration

To implement this in your ESB proxy/service:

  1. Save the XSLT as add-xml-multiple.xslt and upload it to your ESB’s registry (e.g., under /_system/config).
  2. In your flow, immediately after the DSS call mediator, add an XSLT Mediator.
  3. Configure the mediator to reference your uploaded stylesheet (e.g., registry:/_system/config/add-xml-multiple.xslt).
  4. Make sure the XML response passes through this mediator before any JSON conversion step (like PayloadFactory or JSONConverter).

Verify the Result

After applying this fix, even if DSS returns only one VaccinationDetails record, the XML will include <?xml-multiple?> before it. When converted to JSON, this forces the element to be an array instead of an object—no more parsing errors!

内容的提问来源于stack exchange,提问作者AndreaNobili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:53:04