微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

所有 LineItems 作为 xml 到 json 转换中单个和多个 lineitems 的数组

如何解决所有 LineItems 作为 xml 到 json 转换中单个和多个 lineitems 的数组

我们正在尝试为 xml 到 json 转换中的“地址”的每个行项添加正方形/数组 [。 lineitems 可以是 1 个或多个,但它应该嵌入在方括号“地址”中:[[{...}],[{...}]]。

xml 文件至少可以包含 1 个地址或超过 50 个行项

xml 文件

<root>
    <FirstName>Alex</FirstName>
    <LastName>Fin</LastName>
    <Details>
        <Id_Number>111</Id_Number>
        <Location>NC</Location>
        <Contact>
             <PhoneNumber>+1 323</PhoneNumber>
         </Contact>
    </Details>
    <Details>
        <Id_Number>222</Id_Number>
        <Location>TX</Location>
        <Contact>
             <PhoneNumber>+1 323</PhoneNumber>
         </Contact>
    </Details>
    <Address>
        <Locality>Urban</Locality>
        <Type>Mobile</Type>
     </Address>
     <Address>
        <Locality>Rural</Locality>
        <Type>Landline</Type>
     </Address>
</root>

预期的 jsonfile:

{
    "FirstName": "Alex","LastName": "Fin","Details": [ [
      {
        "Id_Number": 111,"Location": "NC","Contact": {
          "PhoneNumber": "+1 323"
        }
      },{
        "Id_Number": 222,"Location": "TX","Contact": {
          "PhoneNumber": "+1 323"
        }
      }
    ]],"Address": [
      [
       {
        "Locality": "Urban"
        "Type": "Mobile"
      }
      ],[
       {
        "Locality": "Rural"
        "Type": "Landline"
      }
      ]
    ]
  }

xslt代码

<?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="*[(*) and . castable as xs:double]">
    <number key="{local-name()}">{.}</number>
  </xsl:template>
  
  <xsl:template match="*[*]">
    <xsl:param name="key" as="xs:boolean" select="false()"/>
    <map>
      <xsl:if test="$key">
        <xsl:attribute name="key" select="local-name()"/>
      </xsl:if>
      <xsl:for-each-group select="*" group-by="node-name()">
          <xsl:choose>
              <xsl:when test="current-group()[2] or self::Details or self::Address">
                  <array key="{local-name()}">
                    <xsl:choose>
                      <xsl:when test="self::Details">
                        <array>
                          <xsl:apply-templates select="current-group()">
                            <xsl:with-param name="key" select="false()"/>
                          </xsl:apply-templates>                        
                        </array>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:apply-templates select="current-group()">
                          <xsl:with-param name="key" select="false()"/>
                        </xsl:apply-templates>
                      </xsl:otherwise>                      
                    </xsl:choose>
                  </array>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()">
                    <xsl:with-param name="key" select="true()"/>
                  </xsl:apply-templates>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
    </map>
  </xsl:template>

</xsl:stylesheet>

解决方法

代码的进一步扭曲

  <xsl:template match="*[*]">
    <xsl:param name="key" as="xs:boolean" select="false()"/>
    <map>
      <xsl:if test="$key">
        <xsl:attribute name="key" select="local-name()"/>
      </xsl:if>
      <xsl:for-each-group select="*" group-by="node-name()">
          <xsl:choose>
              <xsl:when test="current-group()[2] or self::Details or self::Address">
                  <array key="{local-name()}">
                    <xsl:choose>
                      <xsl:when test="self::Details">
                        <array>
                          <xsl:apply-templates select="current-group()">
                            <xsl:with-param name="key" select="false()"/>
                          </xsl:apply-templates>                        
                        </array>
                      </xsl:when>
                      <xsl:when test="self::Address">
                        <xsl:iterate select="current-group()">
                          <array>
                            <map>
                              <xsl:apply-templates>
                                <xsl:with-param name="key" select="true()"/>
                              </xsl:apply-templates>
                            </map>
                          </array>
                        </xsl:iterate>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:apply-templates select="current-group()">
                          <xsl:with-param name="key" select="false()"/>
                        </xsl:apply-templates>
                      </xsl:otherwise>                      
                    </xsl:choose>
                  </array>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()">
                    <xsl:with-param name="key" select="true()"/>
                  </xsl:apply-templates>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
    </map>
  </xsl:template>

我认为可能会使测试用例起作用,但我想您会返回一个略有不同的输入样本并要求进行其他修改。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。