简体   繁体   中英

XSLT convert XML to JSON

I am new with xslt and I am facing an issue when I try to convert a xml to Json. The main problem is that I can not update the variables inside the loop. So, I am to handle with multiples . Any thoughts? Thank you!

input xml:

<attributes>
 <name>uid</name>
 <values>user123</values>
 <name>mail</name>
 <values>xxxxxxxxxxxxxx</values>
 <name>ismemberof</name>
 <values>cn=Partner xxxxxxxxxxxxxxxxxx dc=com</values>
 <values>cn=Partner zzzzzzzzzzzzzzzzzzz dc=com</values>
 <values>ccn=Partner 444444444444444 dc=com</values>
 <values>cn=MANSFIELD MEDICAL,teste teste</values>
 <values>cn=Partner Portal - hthththththththt</values>
<name>dn</name>
<values>uid=user123,ou=people,dc=tttt,dc=com</values>
<name>telephoneNumber</name>
<name>objectClass</name>
<values>person</values>
<values>inetorgperson</values>
<values>dspswuser</values>
<values>top</values>
</attributes>

My xslT code :

<xsl:template match="/">    
<xsl:for-each select = "attributes/*">    
<xsl:if test = "(name(.) = 'name')"> 
<xsl:value-of select="concat(text(),':[&quot;')" />    
  </xsl:if>
 <xsl:if test = "(name(.) = 'values')">     
    <xsl:value-of select="concat(text(),'&quot;],')" />    
</xsl:if>    
</xsl:for-each>     
</xsl:template> 

Expected output:

{
    "username": "bjensen",
       "realm": "/",
       "uid": [
           "bjensen"
       ],
       "mail": [
           "bjensen@example.com"
       ],
       "sn": [
           "bjensen"
       ],    
       "dn": [
           "uid=bjensen,ou=people,dc=openam,dc=forgerock,dc=org"
       ],
       "objectclass": [
           "person",
           "sunIdentityServerLibertyPPService",
           "sunFederationManagerDataStore",
            "top"
       ],
       "universalid": [
           "id=bjensen,ou=user,dc=openam,dc=forgerock,dc=org"
       ]
}

It is not really clear which input format you have and how you want to map it to JSON but part of the data you have presented looks like you could use for-each-group select="*" group-starting-with="name" on the child elements of the attributes element:

<?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="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-skip"/>

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="attributes">
      <xsl:map>
          <xsl:for-each-group select="*" group-starting-with="name">
              <xsl:map-entry key="." select="array { data(tail(current-group())) }"/>
          </xsl:for-each-group>
      </xsl:map>
  </xsl:template>

</xsl:stylesheet>

This assumes an XSLT 3 processor like Saxon 9.8 or 9.9 or Altova XML 2017 or 2018 or 2019 and will for your input give a JSON result like

 {
  "dn": [
    "uid=user123,ou=people,dc=tttt,dc=com"
   ],
  "telephoneNumber": [

   ],
  "uid": [
    "user123"
   ],
  "mail": [
    "xxxxxxxxxxxxxx"
   ],
  "objectClass": [
    "person",
    "inetorgperson",
    "dspswuser",
    "top"
   ],
  "ismemberof": [
    "cn=Partner xxxxxxxxxxxxxxxxxx dc=com",
    "cn=Partner zzzzzzzzzzzzzzzzzzz dc=com",
    "ccn=Partner 444444444444444 dc=com",
    "cn=MANSFIELD MEDICAL,teste teste",
    "cn=Partner Portal - hthththththththt"
   ]
 }

https://xsltfiddle.liberty-development.net/3NzcBtX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM