将 XML 封装成 JSON 列表 [英] Encapsulate XML into JSON list

查看:63
本文介绍了将 XML 封装成 JSON 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 XML 源转换为指定的 JSON 格式.为此,我需要删除头节点,保留数组主体并封装在 [] 中.我已经转换了正文,但在删除标题节点和插入封装​​ [ ]

I need to convert an XML source to a specified JSON format. To do this i need to remove header nodes, retain the array body and encapsulate in [ ]. I have converted the body but i am having trouble removing the header nodes and inserting the encapsulating [ ]

这是我收到的 XML 格式:

This is the XML format I receive:

<?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 重新格式化,但我没有正确删除外部节点并使用 [ ]

I have tried using the adapter provided on SAP PI to do the conversion but this does not cater for a full array body. I have tried to reformat using XSLT but I am not coming right with removing the outer nodes and encapsulating with [ ]

这是我的 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>

这是我目前正在制作的:

This is what i am currently producing:

{

        "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"
    }]

推荐答案

我建议你这样试试:

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>

<小时>

附言空白字符是可选的,可以在不影响 JSON 结果有效性的情况下删除.


P.S. The whitespace characters are optional and can be removed without affecting the validity of the JSON result.

这篇关于将 XML 封装成 JSON 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆