Tushar Patra
Tushar Patra

Reputation: 1

Dynamic conversion of JSON to XML using saxon

We have incoming JSON data from Source which has data and column in same payload. Using json-to-xml in XSLT I was able to generate XML like following:

  <?xml version="1.0" encoding="UTF-8"?>
  <map xmlns="http://www.w3.org/2005/xpath-functions">
   <boolean key="allData">true</boolean>
   <map key="factMap">
  <map key="T!T">
     <array key="rows">
        <map>
           <array key="dataCells">
              <map>
                 <string key="label">1A</string>
                 <string key="value">1A</string>
              </map>
              <map>
                 <string key="label">1B</string>
                 <null key="value"/>
              </map>
              <map>
                 <string key="label">1C</string>
                 <string key="value">1C</string>
              </map>
           </array>
        </map>
        <map>
           <array key="dataCells">
              <map>
                 <string key="label">2A</string>
                 <string key="value">2A</string>
              </map>
              <map>
                 <string key="label">2B</string>
                 <string key="value">2B</string>
              </map>
              <map>
                 <string key="label">2C</string>
                 <null key="value"/>
              </map>
           </array>
        </map>
      </map>
 <map key="detailColumnInfo">
  <map key="name.F1">
     <string key="dataType">string</string>
     <string key="label">F1</string>
  </map>
  <map key="state.F2">
     <string key="dataType">string</string>
     <string key="label">F2</string>
  </map>
  <map key="dist.F3">
     <string key="dataType">string</string>
     <string key="label">F3</string>
  </map>
  </map>
     </map>

We have to generate following XML output.

<Root>   
  <Rows> 
   <Row> 
    <F1> 1A </F1> 
    </F2> 
    <F3> 1C </F3> 
   </Row> 
  <Row> 
    <F1>2A </F1> 
    <F2> 2B </F2> 
    </F3> 
  </Row> 
           </Rows> 

We are able to generate data after taking reference from
https://stackoverflow.com/questions/47173669/dynamic-xml-generation-using-xslt#= But it not working as expected for null tags. Can anyone suggest possible solutions to implement such dynamic mapping.

Upvotes: 0

Views: 344

Answers (1)

John Bollinger
John Bollinger

Reputation: 180918

By reference to the other question you linked, I take your question to be about the fact that the values you want to extract are sometimes represented as <string> elements, but also sometimes as <null> elements. It appears that either way, the elements of interest are distinguished by a key attribute with value 'value', so you can base your selection criteria on that.

That is, instead of using fn:string[@key = 'value'] in your XPath expressions, use *[@key = 'value'] or even node()[@key = 'value']. There are other alternatives, but applying either of those variations to the XSL in the answer you referenced will yield a transform that handles the input you present in the manner you seem to want (notwithstanding ill-formedness in the output presented in this question, which I am taking to be unintentional).

Upvotes: 1

Related Questions