簡體   English   中英

將 XML 封裝成 JSON 列表

[英]Encapsulate XML into JSON list

我需要將 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"
    }]

我建議你這樣試試:

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>

PS 空白字符是可選的,可以刪除而不影響 JSON 結果的有效性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM