Arrays 包含多行项目的Xml到Json
我试图将xml转换为json,但在xml转换为json的过程中,没有形成多行项目。在xml消息中,总是有一个或多个行项,它们的名称与“vpoItemList”、“VPOSHIPDTLDTTOGROUPTLIST”和“VPOSHIPDTLCSGROUPTLIST”相同 请求检查代码以获得json文件中的所有行项,如预期的json文件。将错误消息获取为error:xml到json:duplicate键值{vpoItemList} 输入xml文件Arrays 包含多行项目的Xml到Json,arrays,xslt,multidimensional-array,xml-parsing,xslt-2.0,Arrays,Xslt,Multidimensional Array,Xml Parsing,Xslt 2.0,我试图将xml转换为json,但在xml转换为json的过程中,没有形成多行项目。在xml消息中,总是有一个或多个行项,它们的名称与“vpoItemList”、“VPOSHIPDTLDTTOGROUPTLIST”和“VPOSHIPDTLCSGROUPTLIST”相同 请求检查代码以获得json文件中的所有行项,如预期的json文件。将错误消息获取为error:xml到json:duplicate键值{vpoItemList} 输入xml文件 <?xml version="
<?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>28288</vpoNo>
<vpoDate>2021-02-23</vpoDate>
<instructions>METAL-No Size:1360600001-Article Number</instructions>
<businessRefNo>28118</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>
XSLT代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="text"/>
<xsl:template match="/*" priority="5">
<xsl:variable name="json-xml">
<map>
<xsl:apply-templates/>
</map>
</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="*[* and not(*[6])]">
<map key="{local-name()}">
<xsl:apply-templates/>
</map>
</xsl:template>
<xsl:template match="*[* and *[6]]">
<array key="{local-name()}">
<map>
<xsl:apply-templates/>
</map>
</array>
</xsl:template>
<xsl:template match="vpoShipDtlDtoGroupList/vpoShipId">
<map key="{local-name()}">
<xsl:apply-templates/>
</map>
</xsl:template>
<xsl:template match="vpoShipDtlDtoGroupList | vpoShipDtlCsGroupList" priority="20">
<array key="{local-name()}">
<array>
<map>
<xsl:apply-templates/>
</map>
</array>
</array>
</xsl:template>
</xsl:stylesheet>
谢谢,
Ravi虽然使用XSLT 3,但不清楚为什么要将其标记为XSLT 2.0
一般来说,如果您有几个同名的子元素,并且想在JSON表示中作为一个数组的成员将它们“块”在一起,则可以考虑使用<代码> XSL:对于每个组选择“=”*组=“NoDeNAMEY()”< /代码>,然后,如果组中有不止一个项(例如“代码> < /代码>”)输出所需的数组,否则进行常规处理以构建映射
我没有处理所有选项的完整、现成的代码,下面是一个使用分组构造数组的示例<?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="*[*]">
<map>
<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()"/>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
</xsl:stylesheet>
您可能需要根据自己的需要改进/扩展/调整该模板。我试图在xsl代码末尾添加模板,但错误相同。当行项目为1或多个时,请共享正确的模板以获取值1@raviteja,我有一个片段,但是请务必理解,由于输入和想要的输出的结构方式多种多样,因此很难编写简短/紧凑的“一刀切”代码,因此请做好准备,使其适应您的需要。-我试图使用相同的代码,但子元素“EmployeeCode”无法识别。错误:在第15行执行XSLT时出错:的子元素必须具有键属性Alex423.14fooF2142bartest
<?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="*[*]">
<map>
<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()"/>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
</xsl:stylesheet>
<root>
<item>
<number>3.14</number>
<name>foo</name>
</item>
<item>
<number>42</number>
<name>bar</name>
</item>
<data>test</data>
</root>
{ "item" :
[
{ "number" : 3.14,
"name" : "foo" },
{ "number" : 42,
"name" : "bar" } ],
"data" : "test" }