zoukankan      html  css  js  c++  java
  • XSLT Transformation XML to JSON D365FO Data Management

    Recently i was exploring an option for Transformation in Data Management framework, we do have this option of transforming the export file in different format than the default one. One more thing that triggers, D365fo doesn’t export JSON by default. However, it does export XML file format. After some research I found this XSLT code that transformed XML to JSON. https://gist.github.com/bojanbjelic/1632534

     <?xml version="1.0" encoding="UTF-8" ?>  
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
          <xsl:output method="text" encoding="utf-8"/>  
          <xsl:template match="/node()">  
               <xsl:text>{</xsl:text>  
               <xsl:apply-templates select="." mode="detect" />  
               <xsl:text>}</xsl:text>  
          </xsl:template>  
          <xsl:template match="*" mode="detect">  
               <xsl:choose>  
                    <xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">  
                              <xsl:apply-templates select="." mode="obj-content" />  
                         <xsl:text>]</xsl:text>  
                         <xsl:if test="count(following-sibling::*[name() != name(current())]) &gt; 0">, </xsl:if>  
                    </xsl:when>  
                    <xsl:when test="name(preceding-sibling::*[1]) = name(current())">  
                              <xsl:apply-templates select="." mode="obj-content" />  
                              <xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>  
                    </xsl:when>  
                    <xsl:when test="following-sibling::*[1][name() = name(current())]">  
                         <xsl:text>"</xsl:text><xsl:value-of select="name()"/><xsl:text>" : [</xsl:text>  
                              <xsl:apply-templates select="." mode="obj-content" /><xsl:text>, </xsl:text>   
                    </xsl:when>  
                    <xsl:when test="count(./child::*) > 0 or count(@*) > 0">  
                         <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : <xsl:apply-templates select="." mode="obj-content" />  
                         <xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if>  
                    </xsl:when>  
                    <xsl:when test="count(./child::*) = 0">  
                         <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/><xsl:text>"</xsl:text>  
                         <xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if>  
                    </xsl:when>  
               </xsl:choose>  
          </xsl:template>  
          <xsl:template match="*" mode="obj-content">  
               <xsl:text>{</xsl:text>  
                    <xsl:apply-templates select="@*" mode="attr" />  
                    <xsl:if test="count(@*) &gt; 0 and (count(child::*) &gt; 0 or text())">, </xsl:if>  
                    <xsl:apply-templates select="./*" mode="detect" />  
                    <xsl:if test="count(child::*) = 0 and text() and not(@*)">  
                         <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/><xsl:text>"</xsl:text>  
                    </xsl:if>  
                    <xsl:if test="count(child::*) = 0 and text() and @*">  
                         <xsl:text>"text" : "</xsl:text><xsl:value-of select="text()"/><xsl:text>"</xsl:text>  
                    </xsl:if>  
               <xsl:text>}</xsl:text>  
               <xsl:if test="position() &lt; last()">, </xsl:if>  
          </xsl:template>  
          <xsl:template match="@*" mode="attr">  
               <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="."/><xsl:text>"</xsl:text>  
               <xsl:if test="position() &lt; last()">,</xsl:if>  
          </xsl:template>  
          <xsl:template match="node/@TEXT | text()" name="removeBreaks">  
               <xsl:param name="pText" select="normalize-space(.)"/>  
               <xsl:choose>  
                    <xsl:when test="not(contains($pText, '
    '))"><xsl:copy-of select="$pText"/></xsl:when>  
                    <xsl:otherwise>  
                         <xsl:value-of select="concat(substring-before($pText, '
    '), ' ')"/>  
                         <xsl:call-template name="removeBreaks">  
                              <xsl:with-param name="pText" select="substring-after($pText, '
    ')"/>  
                         </xsl:call-template>  
                    </xsl:otherwise>  
               </xsl:choose>  
          </xsl:template>  
     </xsl:stylesheet>  
    

    so now with the help of transformation we can convert default xml export file to json format during data management export process. Under the Data management workspace, open the Source data format form. Create new record called JSON and set the default extension to json.

    • File format = XML
    • XML Style = Attribute
    • Root element = Document (I left this as default)

    Now click on the View map icon.Create a new Export and select your entity. In the Source data format, select JSON record that was created in the previous step.In the mapping form, click on the Transformations tab. Then upload the xslt file you downloaded from the github. Once we click export on data management project, it applies the transformation and you will get a JSON file like this. This works very nicely and no development or extra transformation at the target.This is interesting because we can build different types of integrations like sending purchase order confirmation as Json export to file share.

  • 相关阅读:
    【Python】使用codecs模块进行文件操作及消除文件中的BOM
    Vue公共结果页面实现
    vscode调整字体大小
    vue-cli3使用vue-svg-loader加载svg
    Microsoft Edge Chrome 下载
    [译]基于Vue.js的10个最佳UI框架,用于构建移动应用程序
    axios采坑之路
    MacBook Touch Bar 使用技巧
    vue-cli 3.x 移除console总结
    嵌入式Linux如何设置获取uboot参数
  • 原文地址:https://www.cnblogs.com/lingdanglfw/p/15202608.html
Copyright © 2011-2022 走看看