选择并在xslt中合并2个数组

时间:2020-10-27 08:50:18

标签: xslt xslt-2.0 xslt-3.0

我下面有一个Input json

[
    {
        "fileName": "abc",
        "value": [
            {
                "SSC": {
                    "Payload": [
                        {
                            "Ledger": [
                                {
                                    "Line": [
                                        {
                                            "AccountingCode": [
                                                "402050046767"
                                            ],
                                            "AccountingPeriod": [
                                                "00720201243"
                                            ]
                                        },
                                        {
                                            "AccountingCode": [
                                                "203010334567"
                                            ],
                                            "AccountingPeriod": [
                                                "00720201234"
                                            ]
                                            
                                        }
                                       
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    },
    {
        "fileName": "abc",
        "value": [
            {
                "SSC": {
                    "Payload": [
                        {
                            "Ledger": [
                                {
                                    "Line": [
                                        {
                                            "AccountingCode": [
                                                "40205004"
                                            ],
                                            "AccountingPeriod": [
                                                "0072020"
                                            ]
                                        },
                                        {
                                            "AccountingCode": [
                                                "20301033"
                                            ],
                                            "AccountingPeriod": [
                                                "0072020"
                                            ]
                                            
                                        }
                                       
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }
]

我的预期输出是

[
    {
        "fileName": "abc",
        "value": [
            {
                "SSC": {
                    "Payload": [
                        {
                            "Ledger": [
                                {
                                    "Line": [
                                        {
                                            "AccountingCode": [
                                                "402050046767"
                                            ],
                                            "AccountingPeriod": [
                                                "00720201243"
                                            ]
                                        },
                                        {
                                            "AccountingCode": [
                                                "203010334567"
                                            ],
                                            "AccountingPeriod": [
                                                "00720201234"
                                            ]
                                            
                                        },
                                        {
                                            "AccountingCode": [
                                                "40205004"
                                            ],
                                            "AccountingPeriod": [
                                                "0072020"
                                            ]
                                        },
                                        {
                                            "AccountingCode": [
                                                "20301033"
                                            ],
                                            "AccountingPeriod": [
                                                "0072020"
                                            ]
                                            
                                        }
                                       
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }
]

以下是我尝试过但未获得正确输出的xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="3.0"
    xmlns="http://www.w3.org/2005/xpath-functions"
    xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
    expand-text="yes">
    <xsl:param name="input"/>
    
    <xsl:output method="text"/>
    
    <xsl:template name="xsl:initial-template">
        <xsl:variable name="input-as-xml" select="json-to-xml($input)"/>
        <xsl:variable name="transformed-xml" as="element(array)">
           <array>
            
                           <map>  
                    <string key="fileNames">
                        <xsl:for-each select="$input-as-xml//string[@key='fileName']">
                            <xsl:variable name="file" select="../string[@key='fileName']"/>
                        <xsl:value-of select="$file"/>
                        </xsl:for-each>
                    </string>
                               <array key="value">
                                   <map>
                                       <array key="SSC">
                                           <map>
                                               <array key="Payload">
                                                   <map>
                                                       <array key="Ledger">
                                                           <xsl:for-each select="$input-as-xml//array[@key='Ledger']">
                                                               
                                                           
                                                           <map>
                                                           <xsl:choose>
                                                               <xsl:when test="$input-as-xml//string[@key='fileName'] = 'abc'">
                                                                   <xsl:value-of select="$input-as-xml//array[@key='Lines']"/>
                                                                 
                                                                       
                                                               </xsl:when>
                                                           </xsl:choose>
                                                           </map>
                                                           </xsl:for-each>
                                                           
                                                       </array>
                                                   </map>
                                               </array>
                                           </map>
                                       </array>
                                   </map>
                               </array>
                           </map>
            
           </array>
        </xsl:variable>
        <xsl:value-of select="xml-to-json($transformed-xml)"/>
    </xsl:template>
</xsl:stylesheet>

如果文件名为abc,我想从json中选择“行”,然后结合在“分类帐”中选择的值

任何人都可以帮助我了解如何在xslt中实现这一目标。

1 个答案:

答案 0 :(得分:0)

您可以执行此操作而无需转换为XML并再次返回:只需将JSON作为映射和数组的结构进行操作即可。类似于

<xsl:template name="xsl:initial-template">
  <xsl:variable name="in" select="json-doc('input.json')"/>
  <xsl:variable name="out" as="map(*)*">
    <xsl:for-each-group select="$in?*" group-by="?fileName">
      <xsl:map>
        <xsl:map-entry key="'fileName'" select="current-grouping-key()"/>
        <xsl:map-entry key="'value'" select="
           array {
             map {'SSC': 
               map {'Payload': 
                 array {
                   map {'Ledger':
                     array {
                       map{'Line': array:join(current-group()?
                                    value?*?SSC?Payload?*?Ledger?*?Line)}
                     }
                   }
               }
            }"/>
       </xsl:map>
     </xsl:for-each-group>
   </xsl:variable>
   <xsl:sequence select="array{$out}"/>
</xsl:template>
  

我还没有对此进行测试,并且在解决问题的复杂路径中可能存在一些错误,但是我希望它能够传达出这个想法。

(请注意,如果XSLT 3.0带有xsl:array指令,这会更容易。如果您准备使用Saxon扩展,则可以使用saxon:array来弥补这一空白。)

相关问题