【问题标题】:How to write XSLT for Converting XML to JSON?如何编写用于将 XML 转换为 JSON 的 XSLT?
【发布时间】:2020-10-15 16:39:21
【问题描述】:

我有以下 XML 文件作为输入,并且想要使用 XSLT 映射的 JSON 输出。如何设计我的 XSLT 文件?

XML 输入:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SyncItemMaster versionID="2.14.0" releaseID="9.2"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.14.0/InforOAGIS/BODs/SyncItemMaster.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://schema.infor.com/InforOAGIS/2">
    <ApplicationArea>
        <Sender>
            <LogicalID>lid://infor.ln.ln_1100</LogicalID>
            <ComponentID>erp</ComponentID>
            <ConfirmationCode>OnError</ConfirmationCode>
        </Sender>
        <CreationDateTime>2020-06-16T11:00:16Z</CreationDateTime>
        <BODID>infor-nid:infor:1100::11064973:?ItemMaster&verb=Sync</BODID>
    </ApplicationArea>
    <DataArea>
        <Sync>
            <TenantID>infor</TenantID>
            <AccountingEntityID>1100</AccountingEntityID>
            <LocationID/>
            <ActionCriteria>
                <ActionExpression actionCode="Change"/>
            </ActionCriteria>
        </Sync>
        <ItemMaster>
            <ItemMasterHeader>
                <ItemID>
                    <ID variationID="4241287" lid="lid://infor.ln.ln_1100" accountingEntity="1100">11064973</ID>
                </ItemID>
                <DisplayID>11064973</DisplayID>
                <GTIN/>
                <ServiceIndicator>false</ServiceIndicator>
                <Description>H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="de_DE">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="fr_FR">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="it_IT">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="nl_NL">H3BO3 4% mit Sher Indikator</Description>
                <Description languageID="en_US">H3BO3 4% mit Sher Indikator</Description>
                <Note/>
            </ItemMasterHeader>
        </ItemMaster>
    </DataArea>
</SyncItemMaster>

预期的 JSON 输出:

{ 
   "description":[

      { 
         "languageID":"ge_GE",
         "text":"H3BO3 4% mit Sher Indikator"
      
},
      { 
         "languageID":"de_DE",
         "text":"H3BO3 4% mit Sher Indikator"
},
      {
         "languageID":"fr_FR",
         "text":"H3BO3 4% mit Sher Indikator" 
},
      { 
         "languageID":"it_IT",
         "text":"H3BO3 4% mit Sher Indikator"    
},
      {
         "languageID":"nl_NL",
         "text":"H3BO3 4% mit Sher Indikator"
},
      {
         "languageID":"en_US",
         "text":"H3BO3 4% mit Sher Indikator"
}
   
]
}

【问题讨论】:

  • 另请注意,您输入的部分内容,即&lt;BODID&gt;infor-nid:infor:1100::11064973:?ItemMaster&amp;verb=Sync&lt;/BODID&gt; 不是格式正确的XML,与符号需要在XML 中转义为&lt;BODID&gt;infor-nid:infor:1100::11064973:?ItemMaster&amp;amp;verb=Sync&lt;/BODID&gt;。

标签: xslt xslt-1.0 xslt-2.0 xslt-3.0


【解决方案1】:

通常,在 XSLT 3 中您有两个选择,将 JSON 创建为相应的 XPath 3.1 映射和数组 (https://xsltfiddle.liberty-development.net/pNmCzsG):

<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence
      select="map { 
                'description' : array { 
                  //Description ! 
                  map { 
                    'languageID' : (data(@languageID), 'ge_GE') => head(), 
                    'text' : data() 
                  }
                }
              }"/>
  </xsl:template>
  
</xsl:stylesheet>

或将 XML 输入转换为 xml-to-json 函数期望的 XML 输入格式(参见

的中间结果
<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <map>
        <xsl:where-populated>
            <array key="description">
                <xsl:apply-templates select="//Description"/>
            </array>
        </xsl:where-populated>
    </map>
  </xsl:template>
  
  <xsl:template match="Description[not(@languageID)]">
      <map>
          <string key="languageID">ge_GE</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
  <xsl:template match="Description">
      <map>
          <string key="languageID">{@languageID}</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
</xsl:stylesheet>

) 然后使用该函数xml-to-json (https://xsltfiddle.liberty-development.net/pNmCzsG/1):

<?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"
    xpath-default-namespace="http://schema.infor.com/InforOAGIS/2"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:variable name="json-xml">
        <map>
            <xsl:where-populated>
                <array key="description">
                    <xsl:apply-templates select="//Description"/>
                </array>
            </xsl:where-populated>
        </map>        
    </xsl:variable>
    <xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
  </xsl:template>
  
  <xsl:template match="Description[not(@languageID)]">
      <map>
          <string key="languageID">ge_GE</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
  <xsl:template match="Description">
      <map>
          <string key="languageID">{@languageID}</string>
          <string key="text">{.}</string>
      </map>
  </xsl:template>
  
</xsl:stylesheet>

上面显示的第一次尝试没有定义 JSON 属性输出的顺序,除非您使用 Saxon 特定的扩展属性。使用第二个建议,您可以定义顺序。

【讨论】:

  • 感谢您的帮助。我会试试这个。
  • 我得到一个空 JSON 作为输出。 { }
  • 您需要准确地告诉我们您使用了哪些代码以及您用于运行示例的 XSLT 处理器。
  • 我使用了您建议的第二个选项。首先将其转换为格式化的 XML,然后使用 XSLT 转换为 JSON。我正在 Azure 逻辑应用程序上执行此操作。
  • @Rushank,Azure 逻辑应用支持 XSLT 3.0 吗?
猜你喜欢
  • 2010-10-31
  • 2012-10-12
  • 2021-07-22
  • 2019-07-02
  • 2020-01-26
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多