json - xml到json转换中的空元素
问题描述
json 文件应该按照下面的 xsl 代码工作,否则应该填充 json 文件中的空标签,这两种情况都应该工作。当我们在 xml 节点中有值时,它应该像下面的 json 文件一样工作。
xslcode 应该适用于这两种情况。
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:PO_Record
xmlns:ns0="http://Test.com/Order">
<domainId>TEST</domainId>
<hubDomainId>TEST</hubDomainId>
<isForReference>false</isForReference>
<status>releasedToVendor</status>
<docStatus>active</docStatus>
<editingStatus>confirmed</editingStatus>
<vpoNo>10000341</vpoNo>
<vpoDate>2021-05-11</vpoDate>
<instructions>STONE GREY-XS:060000</instructions>
<businessRefNo>10000341</businessRefNo>
<totalItems>1</totalItems>
<totalQty>5</totalQty>
<customFields>
<customFieldDate1>2021-11-01</customFieldDate1>
</customFields>
<season>
<code>F20</code>
</season>
<custId>
<custCode>TEST</custCode>
<refNo>C000001</refNo>
</custId>
<vendorId>
<vendorCode>46211</vendorCode>
</vendorId>
<vpoItemList>
<itemNo>12345-2020-1002-45671</itemNo>
<itemName>Wallace</itemName>
<customerItemNo>21916</customerItemNo>
<lotNo>1</lotNo>
<itemDesc>Wallace</itemDesc>
<shipQty>5</shipQty>
<planedQty>5</planedQty>
<qtyPerExportCarton>5</qtyPerExportCarton>
<qtyPerInnerCarton>5</qtyPerInnerCarton>
<factCode>0000444488</factCode>
<refNo>12345-2020-1002-45671-1</refNo>
<uom>
<code>PCS</code>
</uom>
<brand>
<code>TEST Co-op</code>
</brand>
<itemId>
<itemNo>12345-2020-1002-45671</itemNo>
</itemId>
<portOfLanding>
<code>City</code>
</portOfLanding>
<vpoItemCsList>
<itemLotNo>12345-2020-1002-45671-Lot1</itemLotNo>
<vpoItemColorRef>STONE</vpoItemColorRef>
<vpoItemColorId>
<refNo>STONE</refNo>
<shortName>STONE</shortName>
</vpoItemColorId>
<vpoItemSizeId>
<refNo>XS</refNo>
<displayName>XS</displayName>
</vpoItemSizeId>
<itemId>
<itemNo>12345-2020-1002-45671</itemNo>
</itemId>
<lotNo>1</lotNo>
</vpoItemCsList>
<factId>
<factCode>902397</factCode>
</factId>
</vpoItemList>
<vpoShipDtlDtoGroupList>
<qty>5</qty>
<refNo>00001/12345-2020-1002-45671-1</refNo>
<vpoItemRef
xmlns:ns1="http://sap.com/xi/SAPGlobal/GDT">12345-2020-1002-45671-1
</vpoItemRef>
<vpoItemId>
<itemId>
<itemNo>12345-2020-1002-45671</itemNo>
</itemId>
</vpoItemId>
<vpoShipRef>00001</vpoShipRef>
<vpoShipId>
<shipmentNo>00001</shipmentNo>
<originalShipmentDate>2021-11-01</originalShipmentDate>
<shipmentDate>2021-11-05</shipmentDate>
<originalInDcDate>2021-12-29</originalInDcDate>
<inDcDate>2021-12-29</inDcDate>
<refNo>00001/12345-2020-1002-45671-1</refNo>
<shipMode>
<code>3</code>
</shipMode>
<finalDestination>
<code>0001</code>
</finalDestination>
<portOfLoading>
<code>City</code>
</portOfLoading>
</vpoShipId>
<shipMode>
<code>3</code>
</shipMode>
<portOfLoading>
<code>City</code>
</portOfLoading>
</vpoShipDtlDtoGroupList>
<vpoShipDtlCsGroupList>
<itemLotNo>12345-2020-1002-45671-Lot1</itemLotNo>
<shipmentNo>00001</shipmentNo>
<colorSizeQty>5</colorSizeQty>
<skuNo>1407600001</skuNo>
<refNo>00001/12345-2020-1002-45671-1/STONE/XS</refNo>
<vpoItemRef>12345-2020-1002-45671-1</vpoItemRef>
<vpoItemId>
<itemId>
<itemNo>12345-2020-1002-45671</itemNo>
</itemId>
</vpoItemId>
<vpoShipRef>00001/12345-2020-1002-45671-1</vpoShipRef>
<vpoItemColorRef>STONE</vpoItemColorRef>
<vpoItemSizeRef>XS</vpoItemSizeRef>
<vpoShipDtlColorRef>1</vpoShipDtlColorRef>
<vpoShipDtlSizeRef>1</vpoShipDtlSizeRef>
</vpoShipDtlCsGroupList>
</ns0:PO_Record>
json文件:
{“domainId”:“TEST”,“hubDomainId”:“TEST”,“isForReference”:“false”,“status”:“releasedToVendor”,“docStatus”:“active”,“editingStatus”:“confirmed”,“ vpoNo”:“10000341”,“vpoDate”:“2021-05-11”,“说明”:“STONE GREY-XS:060000”,“businessRefNo”:“10000341”,“totalItems”:“1”,“totalQty” “:“5”,“customFields”:{“customFieldDate1”:“2021-11-01”},“季节”:{“code”:“F20”},“custId”:{“custCode”:“TEST” ,“refNo”:“C000001”},“vendorId”:{“vendorCode”:“46211”},“vpoItemList”:[{“itemNo”:“12345-2020-1002-45671”,“itemName”:“Wallace”,“customerItemNo”:“21916”,“lotNo”:“1”, “itemDesc”:“Wallace”,“shipQty”:“5”,“planedQty”:“5”,“qtyPerExportCarton”:“5”,“qtyPerInnerCarton”:“5”,“factCode”:“0000444488”,“refNo” " : "12345-2020-1002-45671-1", "uom" : { "code" : "PCS" }, "brand" : { "code" : "TEST Co-op" }, "itemId" : { “itemNo”:“12345-2020-1002-45671”},“portOfLanding”:{“code”:“City”},“vpoItemCsList”:[ {“itemLotNo”:“12345-2020-1002-45671-Lot1”,“vpoItemColorRef”:“STONE”,“vpoItemColorId”:{“refNo”:“STONE”,“shortName”:“STONE”},“vpoItemSizeId “:{“refNo”:“XS”,“displayName”:“XS”},“itemId”:{“itemNo”:“12345-2020-1002-45671”},“lotNo”:“1”}], “factId”:{“factCode”:“902397”}}],“vpoShipDtlDtoGroupList”:[[{“qty”:“5”,“refNo”:“00001/12345-2020-1002-45671-1”,“ vpoItemRef”:“12345-2020-1002-45671-1\n\t\t”,“vpoItemId”:{“itemId”:{“itemNo”:“12345-2020-1002-45671”}},“vpoShipRef”:“00001”,“vpoShipId”:{“shipmentNo”:“00001”,“originalShipmentDate”:“2021-11-01”,“shipmentDate”:“2021-11-05”,“originalInDcDate”:“2021 -12-29”,“inDcDate”:“2021-12-29”,“refNo”:“00001/12345-2020-1002-45671-1”,“shipMode”:{“code”:“3”}, “finalDestination”:{“code”:“0001”},“portOfLoading”:{“code”:“City”}},“shipMode”:{“code”:“3”},“portOfLoading”:{“code” " : "City" } } ] ], "vpoShipDtlCsGroupList" : [ [ { "itemLotNo" : "12345-2020-1002-45671-Lot1", "shipmentNo" : "00001”、“colorSizeQty”:“5”、“skuNo”:“1407600001”、“refNo”:“00001/12345-2020-1002-45671-1/STONE/XS”、“vpoItemRef”:“12345-2020- 1002-45671-1”,“vpoItemId”:{“itemId”:{“itemNo”:“12345-2020-1002-45671”}},“vpoShipRef”:“00001/12345-2020-1002-45671-1” ,“vpoItemColorRef”:“STONE”,“vpoItemSizeRef”:“XS”,“vpoShipDtlColorRef”:“1”,“vpoShipDtlSizeRef”:“1”}]]}:{“itemId”:{“itemNo”:“12345-2020-1002-45671”}},“vpoShipRef”:“00001/12345-2020-1002-45671-1”,“vpoItemColorRef”:“STONE”,“ vpoItemSizeRef”:“XS”,“vpoShipDtlColorRef”:“1”,“vpoShipDtlSizeRef”:“1”}]]}:{“itemId”:{“itemNo”:“12345-2020-1002-45671”}},“vpoShipRef”:“00001/12345-2020-1002-45671-1”,“vpoItemColorRef”:“STONE”,“ vpoItemSizeRef”:“XS”,“vpoShipDtlColorRef”:“1”,“vpoShipDtlSizeRef”:“1”}]]}
xslt code:
<?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="*[(*) 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] or self::vpoItemList or self::vpoItemCsList or self::vpoShipDtlDtoGroupList or self::vpoShipDtlCsGroupList">
<array key="{local-name()}">
<xsl:choose>
<xsl:when test="self::vpoShipDtlDtoGroupList">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:when>
<xsl:when test="self::vpoShipDtlCsGroupList">
<xsl:for-each-group select="current-group()" group-by="itemLotNo">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:for-each-group>
</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>
</array>
</xsl:for-each-group>
</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>
第二种情况:当我们在 xml 节点中没有数据时
Input xml file:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:PO_Record
xmlns:ns0="http://Test.com/Order">
<domainId>TEST</domainId>
<hubDomainId>TEST</hubDomainId>
<isForReference>false</isForReference>
<status>Ok</status>
<docStatus>Success</docStatus>
<editingStatus>confirmed</editingStatus>
<vpoNo>10000341</vpoNo>
<vpoDate>2021-03-05</vpoDate>
<instructions></instructions>
<businessRefNo>10000341</businessRefNo>
<totalItems>0</totalItems>
<totalQty>0</totalQty>
<customFields>
<customFieldDate1>2042-01-21</customFieldDate1>
</customFields>
<season>
<code>F22</code>
</season>
<custId>
<custCode>TEST</custCode>
<refNo>002001</refNo>
</custId>
<vendorId>
<vendorCode>1235</vendorCode>
</vendorId>
<vpoItemList></vpoItemList>
<vpoShipDtlDtoGroupList></vpoShipDtlDtoGroupList>
<vpoShipDtlCsGroupList></vpoShipDtlCsGroupList>
</ns0:PO_Record>
ExpectedJsonFile:
{“domainId”:“TEST”,“hubDomainId”:“TEST”,“isForReference”:“false”,“status”:“Ok”,“docStatus”:“Success”,“editingStatus”:“confirmed” ,“vpoNo”:“10000341”,“vpoDate”:“2021-03-05”,“说明”:“”,“businessRefNo”:“10000341”,“totalItems”:“0”,“totalQty”:“0 ”,“customFields”:{“customFieldDate1”:“2042-01-21”},“季节”:{“code”:“F22”},“custId”:{“custCode”:“TEST”,“refNo” :“002001”},“vendorId”:{“vendorCode”:“1235”},“vpoItemList”:[],“vpoShipDtlDtoGroupList”:[],“vpoShipDtlCsGroupList”:[]}
解决方案
推荐阅读
- javascript - 向 php 文件发送请求时,AJAX 发布请求返回 405(不允许的方法)
- visual-studio - PNPM 和 Visual Studio .Net 2019
- jsonschema - 针对 JSON 中的拼写错误的 JSON Schema 验证
- angular - “Promise”类型上不存在属性“uid”
'.ts(2339) - c++ - c++中string&和string的区别
- elasticsearch - 如何在elasticsearch中获取不同字段/数组值的总和?
- python - PermissionError: [WinError 5] Access is denied 错误发生在 os.remove 中的 pyinstaller exe
- html - 将一个元素右对齐并在其他两个元素下对齐
- python - 使用带有 win32com 的 Python 按名称定位特定的 Excel 工作簿
- python - 从 pandas 列转换或至少删除非英语/不需要(非 ascii)值或将其转换为英文字符