将XML封装成JSON列表

问题描述 投票:0回答:1

我需要将一个XML源转换为指定的JSON格式。 要做到这一点,我需要删除头节点,保留数组主体,并用[ ]封装。 我已经转换了数组主体,但在删除头节点和插入封装的[ ]时遇到了问题。

这是我收到的XML格式。

<?xml version="1.0" encoding="UTF-8"?>
<ns1:DDMRP_Parts xmlns:ns1="urn:za.sabmiller.com:supplychain:3rdp:transdata">
   <Part>
      <PartNumber>000096</PartNumber>
      <Location>A000</Location>
      <Description>TEST OF RAMIS</Description>
      <UnitOfMeasure>EA</UnitOfMeasure>
      <PartType>1</PartType>
      <FixedLeadTime>1</FixedLeadTime>
      <MaterialType>Filling &amp; Mixing Eq</MaterialType>
   </Part>
   <Part>
      <PartNumber>000096</PartNumber>
      <Location>A000</Location>
      <Description>TEST OF RAMIS</Description>
      <UnitOfMeasure>EA</UnitOfMeasure>
      <PartType>1</PartType>
      <FixedLeadTime>1</FixedLeadTime>
      <MaterialType>Filling &amp; Mixing Eq</MaterialType>
   </Part>
</ns1:DDMRP_Parts>

我试着使用SAP PI上提供的适配器来进行转换,但这并不能满足一个完整的数组体。 我试着用XSLT重新格式化,但我不能正确地删除外部节点,并用[ ]封装。

这是我的XSLT。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="text"/>
       <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>
       <!-- Object or Element Property-->
       <xsl:template match="*">
        "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
       </xsl:template>
       <!-- Array Element -->
       <xsl:template match="*" mode="ArrayElement">
              <xsl:call-template name="Properties"/>
       </xsl:template>
       <!-- Object Properties -->
       <xsl:template name="Properties">
              <xsl:variable name="childName" select="name(*[1])"/>
              <xsl:choose>
                     <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
                     <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
                     <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                           <xsl:apply-templates select="*"/>
    }</xsl:otherwise>
              </xsl:choose>
              <xsl:if test="following-sibling::*">,</xsl:if>
       </xsl:template>
       <!-- Attribute Property -->
       <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>

这是我目前正在制作的东西

{

        "ns1:DDMRP_Parts" : { "Part" :[{

        "PartNumber" : "000096",
        "Location" : "A000",
        "Description" : "TEST OF RAMIS",
        "UnitOfMeasure" : "EA",
        "PartType" : "1",
        "FixedLeadTime" : "1",
        "MaterialType" : "Filling & Mixing Eq"
    },{

        "PartNumber" : "000096",
        "Location" : "A000",
        "Description" : "TEST OF RAMIS",
        "UnitOfMeasure" : "EA",
        "PartType" : "1",
        "FixedLeadTime" : "1",
        "MaterialType" : "Filling & Mixing Eq"
    }] }}

这是我需要输出的东西

[{

        "PartNumber" : "000096",
        "Location" : "A000",
        "Description" : "TEST OF RAMIS",
        "UnitOfMeasure" : "EA",
        "PartType" : "1",
        "FixedLeadTime" : "1",
        "MaterialType" : "Filling & Mixing Eq"
    },{

        "PartNumber" : "000096",
        "Location" : "A000",
        "Description" : "TEST OF RAMIS",
        "UnitOfMeasure" : "EA",
        "PartType" : "1",
        "FixedLeadTime" : "1",
        "MaterialType" : "Filling & Mixing Eq"
    }]
json xslt sap sap-xi sap-pi
1个回答
0
投票

我建议你试试这种方式。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/*">
    <!-- array -->
    <xsl:text>[&#10;</xsl:text>
    <xsl:apply-templates select="*"/>
    <xsl:text>&#10;]</xsl:text>
</xsl:template>

<xsl:template match="*">
    <!-- object -->
    <xsl:text>&#9;{&#10;</xsl:text>
    <xsl:apply-templates select="*" mode="data"/>
    <xsl:text>&#10;&#9;}</xsl:text>
    <xsl:if test="position()!=last()">
        <xsl:text>,&#10;</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="*" mode="data">
    <!--  name/value pair -->
    <xsl:text>&#9;&#9;"</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text>" : "</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
    <xsl:if test="position()!=last()">
        <xsl:text>,&#10;</xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

P.S.空格字符是可选的,可以在不影响JSON结果有效性的情况下删除。

© www.soinside.com 2019 - 2024. All rights reserved.