0

I am trying to fetch data from array list, I'm using xslt 3.0 (saxon-HE v11.4 library) to convert json to xml in Java. If data in array list is empty string then space should appended in the required output.

Below are required details: sample json input:

{
"employee":{
"id":["1","2",""]
}
}

required output:

<employee>
<id>
<id indexarray="0">1</id>
<id indexarray="1">2</id>
<id indexarray="2"> </id>
</id>
<name>
<name indexarray="0">a</name>
<name indexarray="1"> </name>
<name indexarray="2"> </name>
</name>
</employee>

tried below code


<id>
<xsl:for-each select="$employee?id?*">
<xsl:choose>
<xsl:when test="$employee?id?*!=''">
<id indexarray="{position()-1}">{.}</id>
<xsl:otherwise>
<id indexarray="{position()-1}">  </id>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</id>

getting output as below:

<id>
<id indexarray="0">1</id>
<id indexarray="1">2</id>
<id indexarray="2"/>
</id>

Any help is appreciated.

3
  • Well, you got one example how to process JSON, but now please take the effort to try to adapt that for further needs on your own instead of just posting new requirements. So for all your needs, show us what you have tried and if it failed, how it failed. Commented Nov 18, 2022 at 17:55
  • The previous question is stackoverflow.com/questions/74476515/…, take the answer there as starting point. Commented Nov 18, 2022 at 19:42
  • Thanks @MartinHonnen for your reply! have updated data with code which i tried, am a beginner, trying to learn xslt, kindly, correct me if am wrong Commented Nov 20, 2022 at 16:03

1 Answer 1

0

For Saxon Java or C or CS the following should give you an idea:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
<xsl:output indent="yes"/>

<xsl:template match=".[. instance of map(*)]">
  <Details>
    <xsl:iterate select="?employee?id?*">
      <name indexarray="{position() - 1}">{if (. = '') then ' ' else .}</name>
    </xsl:iterate>
  </Details>
</xsl:template>
  
</xsl:stylesheet>

SaxonJS seems to have a bug https://saxonica.plan.io/issues/5739 (at least as far as I have tested in 2.5) that somehow causes the ' ' to not being output as part of the element content.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.