微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml 到 json 的转换 - [[]] 的附加方括号用于 lineitems 和 line item

如何解决xml 到 json 的转换 - [[]] 的附加方括号用于 lineitems 和 line item

我们有 1 个或超过 1 个订单项。

即使对于“vpoItemList”的单行项目或多行项目也需要方括号:[和子行项目“vpoItemcslist”:[

用于 (vpoShipDtlDtoGroupList & vpoShipDtlCsGroupList) 的单个或多个行项目的附加方括号。

“vpoShipDtlCsGroupList”应根据以下预期的 json 文件为每个订单项添加方括号。 "vpoShipDtlCsGroupList": [[{....}],[{...}]]

xml 文件

    <?xml version="1.0" encoding="UTF-8" ?>
<ns0:PO_Message
    xmlns:ns0="http://test.com/Test">
    <domainId>MAIL</domainId>
    <hubDomainId>MAIL</hubDomainId>
    <isForReference>false</isForReference>
    <status>releasedTovendor</status>
    <docStatus>active</docStatus>
    <editingStatus>confirmed</editingStatus>
    <vpoNo>4512826888</vpoNo>
    <vpoDate>2021-02-23</vpoDate>
    <instructions>MetaL-No Size:1360600001-Article Number</instructions>
    <businessRefNo>4512826118</businessRefNo>
    <totalItems>1</totalItems>
    <totalQty>160</totalQty>
    <season>
        <code>F21</code>
    </season>
    <custId>
        <refNo>C000001</refNo>
    </custId>
    <vendorId>
        <vendorCode>41843</vendorCode>
    </vendorId>
    <headerFactory>
        <refNo>F000026</refNo>
        <factCode>900088</factCode>
    </headerFactory>
    <vpoItemList>
        <itemNo>19979-2021-0002-41843</itemNo>
        <itemName>Base Camp 6 Footprint</itemName>
        <customerItemNo>19979</customerItemNo>
        <lotNo>1</lotNo>
        <itemDesc>Base Camp 6 Footprint</itemDesc>
        <shipQty>160</shipQty>
        <planedQty>160</planedQty>
        <qtyPerExportCarton>160</qtyPerExportCarton>
        <qtyPerInnerCarton>160</qtyPerInnerCarton>
        <factCode>900088</factCode>
        <refNo>19979-2021-0002-41843-1</refNo>
        <uom>
            <code>PCS</code>
        </uom>
        <brand>
            <code>MAIL Co-op</code>
        </brand>
        <itemId>
            <itemNo>19979-2021-0002-41843</itemNo>
        </itemId>
        <portOfLoading>
            <code>DLC dalian</code>
        </portOfLoading>
        <vpoItemcslist>
            <itemLotNo>19979-2021-0002-41843-Lot1</itemLotNo>
            <vpoItemColorRef>MetaL</vpoItemColorRef>
            <vpoItemColorId>
                <refNo>MetaL</refNo>
                <shortName>MetaL</shortName>
            </vpoItemColorId>
            <vpoItemSizeId>
                <refNo>No Size</refNo>
                <displayName>No Size</displayName>
            </vpoItemSizeId>
            <itemId>
                <itemNo>19979-2021-0002-41843</itemNo>
            </itemId>
            <lotNo>1</lotNo>
        </vpoItemcslist>
        <factId>
            <factCode>900088</factCode>
        </factId>
    </vpoItemList>
    <vpoItemList>
        <itemNo>22825-2021-0001-43095</itemNo>
        <itemName>Flash 22 Member Exclusive</itemName>
        <customerItemNo>22825</customerItemNo>
        <lotNo>1</lotNo>
        <itemDesc>Flash 22 Member Exclusive</itemDesc>
        <shipQty>160</shipQty>
        <planedQty>160</planedQty>
        <qtyPerExportCarton>160</qtyPerExportCarton>
        <qtyPerInnerCarton>160</qtyPerInnerCarton>
        <factCode>900088</factCode>
        <refNo>22825-2021-0001-43095-1</refNo>
        <uom>
            <code>PCS</code>
        </uom>
        <brand>
            <code>MAIL Co-op</code>
        </brand>
        <itemId>
            <itemNo>22825-2021-0001-43095</itemNo>
        </itemId>
        <portOfLoading>
            <code>DLC dalian</code>
        </portOfLoading>
        <vpoItemcslist>
            <itemLotNo>22825-2021-0001-43095-Lot1</itemLotNo>
            <vpoItemColorRef>UNDYED</vpoItemColorRef>
            <vpoItemColorId>
                <refNo>UNDYED</refNo>
                <shortName>UNDYED</shortName>
            </vpoItemColorId>
            <vpoItemSizeRef>One Size</vpoItemSizeRef>
            <vpoItemSizeId>
                <refNo>One Size</refNo>
                <displayName>One Size</displayName>
                <sizeUniqueKey>SIZE1</sizeUniqueKey>
            </vpoItemSizeId>
            <itemId>
                <itemNo>22825-2021-0001-43095</itemNo>
            </itemId>
            <lotNo>1</lotNo>
        </vpoItemcslist>
        <factId>
            <factCode>900088</factCode>
        </factId>
    </vpoItemList>
    <vpoShipDtlDtoGroupList>
        <qty>160</qty>
        <refNo>00001/19979-2021-0002-41843-1</refNo>
        <vpoItemRef>19979-2021-0002-41843-1</vpoItemRef>
        <vpoItemId>
            <itemId>
                <itemNo>19979-2021-0002-41843</itemNo>
            </itemId>
        </vpoItemId>
        <vpoShipRef>00001</vpoShipRef>
        <vpoShipId>
            <shipmentNo>00001</shipmentNo>
            <originalShipmentDate>2021-08-17</originalShipmentDate>
            <shipmentDate>2021-08-23</shipmentDate>
            <originalInDcDate>2021-09-15</originalInDcDate>
            <inDcDate>2021-09-15</inDcDate>
            <refNo>00001/19979-2021-0002-41843-1</refNo>
            <shipMode>
                <code>3</code>
            </shipMode>
            <finalDestination>
                <code>0001</code>
            </finalDestination>
            <portOfLoading>
                <code>DLC dalian</code>
            </portOfLoading>
        </vpoShipId>
        <shipMode>
            <code>3</code>
        </shipMode>
        <portOfLoading>
            <code>DLC dalian</code>
        </portOfLoading>
    </vpoShipDtlDtoGroupList>
    <vpoShipDtlDtoGroupList>
        <qty>630</qty>
        <refNo>00001/22825-2021-0001-43095-1</refNo>
        <vpoItemRef>22825-2021-0001-43095-1</vpoItemRef>
        <vpoItemId>
            <itemId>
                <itemNo>22825-2021-0001-43095</itemNo>
            </itemId>
        </vpoItemId>
        <vpoShipRef>00001</vpoShipRef>
        <vpoShipId>
            <shipmentNo>00001</shipmentNo>
            <originalShipmentDate>2021-08-17</originalShipmentDate>
            <shipmentDate>2021-08-23</shipmentDate>
            <originalInDcDate>2021-09-15</originalInDcDate>
            <inDcDate>2021-09-15</inDcDate>
            <refNo>00001/19979-2021-0002-41843-1</refNo>
            <shipMode>
                <code>3</code>
            </shipMode>
            <finalDestination>
                <code>0001</code>
            </finalDestination>
            <portOfLoading>
                <code>DLC dalian</code>
            </portOfLoading>
        </vpoShipId>
        <shipMode>
            <code>3</code>
        </shipMode>
        <portOfLoading>
            <code>DLC dalian</code>
        </portOfLoading>
    </vpoShipDtlDtoGroupList>
    <vpoShipDtlCsGroupList>
        <itemLotNo>19979-2021-0002-41843-Lot1</itemLotNo>
        <shipmentNo>00001</shipmentNo>
        <colorSizeQty>160</colorSizeQty>
        <refNo>00001/19979-2021-0002-41843-1/MetaL/No Size</refNo>
        <vpoItemRef>19979-2021-0002-41843-1</vpoItemRef>
        <vpoItemId>
            <itemId>
                <itemNo>19979-2021-0002-41843</itemNo>
            </itemId>
        </vpoItemId>
        <vpoShipRef>00001/19979-2021-0002-41843-1</vpoShipRef>
        <vpoItemColorRef>MetaL</vpoItemColorRef>
        <vpoItemSizeRef>No Size</vpoItemSizeRef>
        <vpoShipDtlColorRef>1</vpoShipDtlColorRef>
        <vpoShipDtlSizeRef>1</vpoShipDtlSizeRef>
    </vpoShipDtlCsGroupList>
    <vpoShipDtlCsGroupList>
        <itemLotNo>22825-2021-0001-43095-Lot1</itemLotNo>
        <shipmentNo>00001</shipmentNo>
        <colorSizeQty>630</colorSizeQty>
        <refNo>00001/22825-2021-0001-43095-1/UNDYED/One Size</refNo>
        <vpoItemRef>22825-2021-0001-43095-1</vpoItemRef>
        <itemSizeId>035ae556951b413588cb935fe8abe89e</itemSizeId>
        <itemColorId>92bdbbe775cd470dbfaba3b58597f0a8</itemColorId>
        <itemColor />
        <vpoItemId>
            <itemId>
                <itemNo>22825-2021-0001-43095</itemNo>
            </itemId>
        </vpoItemId>
        <vpoShipRef>00001/22825-2021-0001-43095-1</vpoShipRef>
        <vpoItemColorRef>UNDYED</vpoItemColorRef>
        <vpoItemSizeRef>One Size</vpoItemSizeRef>
        <vpoShipDtlColorRef>1</vpoShipDtlColorRef>
        <vpoShipDtlSizeRef>1</vpoShipDtlSizeRef>
    </vpoShipDtlCsGroupList>
</ns0:PO_Message>

XsltCode:

<?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"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:variable name="json-xml">
          <xsl:apply-templates/>
      </xsl:variable>
      <xsl:value-of select="xml-to-json($json-xml,map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="*[not(*)]">
    <string key="{local-name()}">{.}</string>
  </xsl:template>
  
  <xsl:template match="*[not(*) and . castable as xs:double]">
    <number key="{local-name()}">{.}</number>
  </xsl:template>
  
  <xsl:template match="*[*]">
    <xsl:param name="key" as="xs:boolean" select="false()"/>
    <map>
      <xsl:if test="$key">
        <xsl:attribute name="key" select="local-name()"/>
      </xsl:if>
      <xsl:for-each-group select="*" group-by="node-name()">
          <xsl:choose>
              <xsl:when test="current-group()[2]">
                  <array key="{local-name()}">
                      <xsl:apply-templates select="current-group()">
                        <xsl:with-param name="key" select="false()"/>
                      </xsl:apply-templates>
                  </array>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()">
                    <xsl:with-param name="key" select="true()"/>
                  </xsl:apply-templates>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
    </map>
  </xsl:template>


 <xsl:template match="vpoShipDtlDtoGroupList | vpoShipDtlCsGroupList">
    <array key="{local-name()}">
        <array>
          <map>
            <xsl:apply-templates/>
          </map>          
        </array>
    </array>
  </xsl:template>

</xsl:stylesheet>

预期的Json文件

 {
    "domainId": "MAIL","hubDomainId": "MAIL","isForReference": false,"status": "releasedTovendor","docStatus": "active","editingStatus": "confirmed","vpoNo": "4512826888","vpoDate": "2021-02-23","instructions": "MetaL-No Size:1360600001-Article Number","businessRefNo": "4512826118","totalItems": 1,"totalQty": 160,"season": {
        "code": "F21"
    },"custId": {
        "refNo": "C000001"
    },"vendorId": {
        "vendorCode": "41843"
    },"headerFactory": {
        "refNo": "F000026","factCode": "900088"
    },"vpoItemList": [
        {
            "itemNo": "19979-2021-0002-41843","itemName": "Base Camp 6 Footprint","customerItemNo": "19979","lotNo": 1,"itemDesc": "Base Camp 6 Footprint","shipQty": 160,"planedQty": 160,"qtyPerExportCarton": 160,"qtyPerInnerCarton": 160,"factCode": "900088","refNo": "19979-2021-0002-41843-1","uom": {
                "code": "PCS"
            },"brand": {
                "code": "MAIL Co-op"
            },"itemId": {
                "itemNo": "19979-2021-0002-41843"
            },"portOfLoading": {
                "code": "DLC dalian"
            },"vpoItemcslist": [
                {
                    "itemLotNo": "19979-2021-0002-41843-Lot1","vpoItemColorRef": "MetaL","vpoItemColorId": {
                        "refNo": "MetaL","shortName": "MetaL"
                    },"vpoItemSizeId": {
                        "refNo": "No Size","displayName": "No Size"
                    },"itemId": {
                        "itemNo": "19979-2021-0002-41843"
                    },"lotNo": 1
                }
            ],"factId": {
                "factCode": "900088"
            }
        },{
            "itemNo": "22825-2021-0001-43095","itemName": "Flash 22 Member Exclusive","customerItemNo": "22825","itemDesc": "Flash 22 Member Exclusive","refNo": "22825-2021-0001-43095-1","itemId": {
                "itemNo": "22825-2021-0001-43095"
            },"vpoItemcslist": [
                                {
                    "itemLotNo": "22825-2021-0001-43095-Lot1","vpoItemColorRef": "UNDYED","vpoItemColorId": {
                        "refNo": "UNDYED","shortName": "UNDYED"
                        },"vpoItemSizeRef": "One Size","vpoItemSizeId": {
                        "refNo": "One Size","displayName": "One Size","sizeUniqueKey": "SIZE1"
                        },"itemId": {
                        "itemNo": "22825-2021-0001-43095"
                        },"lotNo": 1
                    
                }
            ],"factId": {
                "factCode": "900088"
            }
        }       
    ],"vpoShipDtlDtoGroupList": [
        [
            {
                "qty": 160,"refNo": "00001/19979-2021-0002-41843-1","vpoItemRef": "19979-2021-0002-41843-1","vpoItemId": {
                    "itemId": {
                        "itemNo": "19979-2021-0002-41843"
                    }
                },"vpoShipRef": "00001","vpoShipId": {
                    "shipmentNo": "00001","originalShipmentDate": "2021-08-17","shipmentDate": "2021-08-23","originalInDcDate": "2021-09-15","inDcDate": "2021-09-15","shipMode": {
                        "code": "3"
                    },"finalDestination": {
                            "code": "0001"
                            },"portOfLoading": {
                        "code": "DLC dalian"
                    }
                },"shipMode": {
                    "code": "3"
                            },"portOfLoading": {
                            "code": "DLC dalian"
                }
            },{
                "qty": 630,"refNo": "00001/22825-2021-0001-43095-1","vpoItemRef": "22825-2021-0001-43095-1","vpoItemId": {
                    "itemId": {
                        "itemNo": "22825-2021-0001-43095"
                    }
                },"portOfLoading": {
                            "code": "DLC dalian"
                }
            }
        ]
    ],"vpoShipDtlCsGroupList": [
        [
            {
                "itemLotNo": "19979-2021-0002-41843-Lot1","shipmentNo": "00001","colorSizeQty": 160,"refNo": "00001/19979-2021-0002-41843-1/MetaL/No Size","vpoShipRef": "00001/19979-2021-0002-41843-1","vpoItemSizeRef": "No Size","vpoShipDtlColorRef": "1","vpoShipDtlSizeRef": "1"
            }
        ],[
                        {
                "itemLotNo": "22825-2021-0001-43095-Lot1","colorSizeQty": 630,"refNo": "00001/22825-2021-0001-43095-1/UNDYED/One Size","itemSizeId": "035ae556951b413588cb935fe8abe89e","itemColorId": "92bdbbe775cd470dbfaba3b58597f0a8","itemColor": {},"vpoItemId": {
                    "itemId": {
                        "itemNo": "22825-2021-0001-43095"
                         }
                    
                },"vpoShipRef": "00001/22825-2021-0001-43095-1","vpoShipDtlSizeRef": "1"
            }
        ]
    ]
}

解决方法

代码的改编需要检查分组代码中的那些元素:

<?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"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:variable name="json-xml">
          <xsl:apply-templates/>
      </xsl:variable>
      <xsl:value-of select="xml-to-json($json-xml,map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="*[not(*)]">
    <string key="{local-name()}">{.}</string>
  </xsl:template>
  
  <xsl:template match="*[not(*) and . castable as xs:double]">
    <number key="{local-name()}">{.}</number>
  </xsl:template>
  
  <xsl:template match="*[*]">
    <xsl:param name="key" as="xs:boolean" select="false()"/>
    <map>
      <xsl:if test="$key">
        <xsl:attribute name="key" select="local-name()"/>
      </xsl:if>
      <xsl:for-each-group select="*" group-by="node-name()">
          <xsl:choose>
              <xsl:when test="current-group()[2]">
                  <array key="{local-name()}">
                    <xsl:choose>
                      <xsl:when test="self::vpoShipDtlDtoGroupList | self::vpoShipDtlCsGroupList">
                        <array>
                          <xsl:apply-templates select="current-group()">
                            <xsl:with-param name="key" select="false()"/>
                          </xsl:apply-templates>                        
                        </array>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:apply-templates select="current-group()">
                          <xsl:with-param name="key" select="false()"/>
                        </xsl:apply-templates>
                      </xsl:otherwise>                      
                    </xsl:choose>
                  </array>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()">
                    <xsl:with-param name="key" select="true()"/>
                  </xsl:apply-templates>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
    </map>
  </xsl:template>

</xsl:stylesheet>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。