3

I have a JSON request like

{
  "aaaa": []
}

First I need to check aaaa exist in my request payload, if exists like as above, I need to add a jsonObject with dummy attributes and values like:

{
  "aaaa": [
    {
     "@c": "test"
      "a": "99999",
      "b": "test",
      "c": "test"
    }
],

If aaaa does not exist in my payload, I need to add it with its dummy attributes and values also. So, if the payload is {} , after xslt, it should be the same JSON as above. In my transformation, I tried to handle this problem in this way:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//jsonObject">
        <xsl:copy>
            <xsl:if test="//jsonObject/not(aaaa)">
                <aaaa>
                    <xsl:attribute name="c">test</xsl:attribute>
                    <a>99999</a>
                    <b>test</b>
                    <c>test</c>
                </aaaa>
            </xsl:if>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//jsonObject/aaaa">
        <xsl:copy>
            <xsl:attribute name="c">test</xsl:attribute>
            <a>99999</a>
            <b>test</b>
            <c>test</c>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

With this request:

{
  "aaaa": []
}

I cannot see aaaa array and its dummy attributes after transformation.

Thanks for any advice!

Community
  • 1
  • 1
karacaoglanb
  • 310
  • 4
  • 17
  • 2
    I guess the tags in your question might reveal some clues, but it's not clear otherwise how you are mapping JSON to XDM node trees. It certainly doesn't appear to be the standard mapping defined in XSLT 3.0. – Michael Kay Oct 31 '19 at 12:37
  • For reference, [here](https://docs.wso2.com/display/ESB481/JSON+Support#JSONSupport-XMLrepresentationofJSONpayloads) is the XML representation of JSON used by WSO2 ESB. – Alejandro Oct 31 '19 at 16:09
  • 2
    @Alejandro, this WSO2 ESB seems very strange, to put it in the mildest possible way ... – Dimitre Novatchev Nov 09 '19 at 21:17
  • 1
    BK, you are violating a fundamental SO recommendation for composing a question: the source **XML** document is not provided -- how then anyone would know what this transformation is supposed to be applied against and what is supposed to be the wanted result? – Dimitre Novatchev Nov 09 '19 at 21:18

1 Answers1

1

From WSO2 documentation

JSON:

{"array":[]}

XML (JsonStreamBuilder):

<jsonObject></jsonObject>

XML (JsonBuilder):

<jsonObject>
   <?xml-multiple array?>
</jsonObject>

When I run your transformation with both outputs, I get

<jsonObject>
   <aaaa c="test">
      <a>99999</a>
      <b>test</b>
      <c>test</c>
   </aaaa>
</jsonObject>

And

<jsonObject>
   <aaaa c="test">
      <a>99999</a>
      <b>test</b>
      <c>test</c>
   </aaaa>
   <?xml-multiple array?>
</jsonObject>

Do note: I'm not saying that I endorse your stylesheet. I would be better using:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="jsonObject[not(aaaa)]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <aaaa c="test">
                <a>99999</a>
                <b>test</b>
                <c>test</c>
            </aaaa>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Alejandro
  • 1,882
  • 6
  • 13