用于将XML转换为JSON数组的XSLT

f4t66c6m  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(170)

我目前正在使用SAP CPI来实现这种转换。我已经尝试了可用的常规XML到JSON转换器,但无法实现这一要求。然后我开始尝试,看看XSLT是否能提供帮助。
我正在尝试转换以下XML负载:

<root>
    <ClientID>1</ClientID>
    <PackageID>650</PackageID>
    <SBUID>2187</SBUID>
    <CandidateID>456</CandidateID>
    <AssociateId>789</AssociateId>
    <FirstName>Meghana</FirstName>
    <MiddleName></MiddleName>
    <LastName>Rao</LastName>
    <FatherName>Satish</FatherName>
    <ContactNo>7530001169</ContactNo>
    <EmailID>dummy@sap.com</EmailID>
    <AddressHistory>
        <Address>
            <SequenceNo>0</SequenceNo>
            <AddressLine>Kharghar,navi mumbai</AddressLine>
            <City>Maharashtra-Mumbai</City>
            <State>Maharashtra</State>
            <PinCode>410210</PinCode>
            <Country>India</Country>
            <Landmark></Landmark>
            <StayFrom>01-08-2013</StayFrom>
            <StayTo>06-08-2021</StayTo>
            <IsCurrentAddress>false</IsCurrentAddress>
            <IsPermanentAddress>false</IsPermanentAddress>
            <HouseNo></HouseNo>
            <AddressType>Current</AddressType>
            <DocList>
                <listofdocs>
                    <DocumentName>abc.jpg</DocumentName>
                    <DocumentPath>base64</DocumentPath>
                </listofdocs>
            </DocList>
        </Address>
    </AddressHistory>
    <EducationList>
        <Education>
            <SequenceNo>0</SequenceNo>
            <Qualification></Qualification>
            <Degree>Under Graduate Degree</Degree>
            <CollegeName>Amrutvahini College of engineering, Sangamner (Pune University)</CollegeName>
            <Location></Location>
            <RollNumber>123123</RollNumber>
            <UniversityName>Mumbai University</UniversityName>
            <UniversityAddress></UniversityAddress>
            <PeriodFrom></PeriodFrom>
            <PeriodTo></PeriodTo>
            <YearOfPassing>2014</YearOfPassing>
            <Zipcode></Zipcode>
            <Percentage></Percentage>
            <AdditionalRemarks>10th/12th/Undergrad etc</AdditionalRemarks>
            <International>false</International>
            <Country></Country>
            <DocList>
                <listofdocs>
                    <DocumentName>abc.jpg</DocumentName>
                    <DocumentPath>base64</DocumentPath>
                </listofdocs>
            </DocList>
        </Education>
    </EducationList>
    <EmploymentList>
        <Employment>
            <SequenceNo>0</SequenceNo>
            <EmployerName>Stravis Solutions</EmployerName>
            <EmployerAddress>Bangalore</EmployerAddress>
            <EmployerContactNo></EmployerContactNo>
            <Designation>SDE</Designation>
            <EmployeeID>asdas</EmployeeID>
            <FixedSalary>0</FixedSalary>
            <IsCurrentEmployment>false</IsCurrentEmployment>
            <RelievingDate>15-10-2021</RelievingDate>
            <State></State>
            <City></City>
            <Zipcode></Zipcode>
            <International>false</International>
            <Country></Country>
            <PFNumber></PFNumber>
            <UANNumber></UANNumber>
            <DocList>
                <listofdocs>
                    <DocumentName>abc.jpg</DocumentName>
                    <DocumentPath>base64</DocumentPath>
                </listofdocs>
            </DocList>
            <DateOfJoining>18-03-2015</DateOfJoining>
        </Employment>
        <Employment>
            <SequenceNo>0</SequenceNo>
            <EmployerName>Stravis Solutions</EmployerName>
            <EmployerAddress>Bangalore</EmployerAddress>
            <EmployerContactNo></EmployerContactNo>
            <Designation>SDE</Designation>
            <EmployeeID>asdas</EmployeeID>
            <FixedSalary>0</FixedSalary>
            <IsCurrentEmployment>false</IsCurrentEmployment>
            <RelievingDate>15-10-2021</RelievingDate>
            <International>false</International>
            <Country></Country>
            <PFNumber></PFNumber>
            <UANNumber></UANNumber>
            <DocList>
                <listofdocs>
                    <DocumentName>abc.jpg</DocumentName>
                    <DocumentPath>base64</DocumentPath>
                </listofdocs>
            </DocList>
            <DateOfJoining>18-03-2015</DateOfJoining>
        </Employment>
    </EmploymentList>
    <AddressReferencesList>
        <ListofReferences>
            <OrganizationName>Com 1</OrganizationName>
            <AdditionalRemarks></AdditionalRemarks>
            <NameOfReferee>Ref1</NameOfReferee>
            <RefereeOccupation>SDE</RefereeOccupation>
            <RefereePhoneNumber>123456</RefereePhoneNumber>
            <RefereeEmailAddress>Ref1@com1.com</RefereeEmailAddress>
        </ListofReferences>
        <ListofReferences>
            <OrganizationName>Com 1</OrganizationName>
            <AdditionalRemarks></AdditionalRemarks>
            <NameOfReferee>Ref1</NameOfReferee>
            <RefereeOccupation>SDE</RefereeOccupation>
            <RefereePhoneNumber>123456</RefereePhoneNumber>
            <RefereeEmailAddress>Ref1@com1.com</RefereeEmailAddress>
        </ListofReferences>
    </AddressReferencesList>
    <DOB>03-08-2021</DOB>
    <DLDetails>
        <DOB>03-08-2021</DOB>
        <ApplicantName>Test</ApplicantName>
        <FatherName>Test</FatherName>
        <dl_remarks></dl_remarks>
        <UniqueIDCode>1231231</UniqueIDCode>
        <DocList>
            <listofdocs>
                <DocumentName>abc.jpg</DocumentName>
                <DocumentPath>base64</DocumentPath>
            </listofdocs>
        </DocList>
    </DLDetails>
    <PanDetails>
        <DOB>03-08-2021</DOB>
        <ApplicantName>Sunil Kumar  Yadav</ApplicantName>
        <FatherName>Sunil</FatherName>
        <pan_remarks></pan_remarks>
        <UniqueIDCode>23123131</UniqueIDCode>
        <DocList>
            <listofdocs>
                <DocumentName>abc.jpg</DocumentName>
                <DocumentPath>base64</DocumentPath>
            </listofdocs>
        </DocList>
    </PanDetails>
    <PVWDetails>
        <AddressList>
            <Address>
                <SequenceNo>0</SequenceNo>
                <AddressLine>Kharghar,navi mumbai</AddressLine>
                <City>Maharashtra-Mumbai</City>
                <State>Maharashtra</State>
                <PinCode>410210</PinCode>
                <Country>India</Country>
                <Landmark></Landmark>
                <StayFrom>01-08-2013</StayFrom>
                <StayTo>06-08-2021</StayTo>
                <IsCurrentAddress>false</IsCurrentAddress>
                <IsPermanentAddress>false</IsPermanentAddress>
                <HouseNo>Sai shradha CHS.Sector-11,</HouseNo>
                <AddressType>Current</AddressType>
                <DocList>
                    <listofdocs>
                        <DocumentName>abc.jpg</DocumentName>
                        <DocumentPath>base64</DocumentPath>
                    </listofdocs>
                </DocList>
            </Address>
        </AddressList>
        <DOB>03-08-2021</DOB>
        <FatherName>Sunil</FatherName>
        <ApplicantName>Sunil Kumar  Yadav</ApplicantName>
    </PVWDetails>
    <CreditDetail>
        <ApplicantName>Test</ApplicantName>
        <DOB>03-08-2021</DOB>
        <FatherName>Test</FatherName>
        <Gender>Male</Gender>
        <UniqueIDCode>Pan Number</UniqueIDCode>
        <EmailID>asda@gmail.com</EmailID>
        <DocList>
            <listofdocs>
                <DocumentName>abc.jpg</DocumentName>
                <DocumentPath>base64</DocumentPath>
            </listofdocs>
        </DocList>
    </CreditDetail>
    <DrugTestPanelCheck>
        <DrugTestPanel>DrugTestPanel5</DrugTestPanel>
        <ApplicantName>Test Candidate</ApplicantName>
    </DrugTestPanelCheck>
    <GDCDetails>
        <ApplicantName>Sunil Kumar  Yadav</ApplicantName>
        <DOB>03-08-2021</DOB>
        <FatherName>Sunil</FatherName>
    </GDCDetails>
    <PassportCheckDetails>
        <NameInPassport>Sunil Kumar  Yadav</NameInPassport>
        <PassportNo>1231231</PassportNo>
        <MachineReadableZone></MachineReadableZone>
        <CandidateFirstName>Sunil</CandidateFirstName>
        <CandidateLastName>Yadav</CandidateLastName>
        <DOB>03-08-2021</DOB>
        <FatherName>Sunil</FatherName>
        <DocList>
            <listofdocs>
                <DocumentName>abc.jpg</DocumentName>
                <DocumentPath>base64</DocumentPath>
            </listofdocs>
        </DocList>
    </PassportCheckDetails>
</root>

到下面的JSON有效负载,如您所见,即使是单个有效负载也有多个数组元素:

{
    "ClientID": "1",
    "PackageID": "650",
    "SBUID": "2187",
    "CandidateID": "456",
    "AssociateId": "789",
    "FirstName": "Meghana",
    "MiddleName": "",
    "LastName": "Rao",
    "FatherName": "Satish",
    "ContactNo": "7530001169",
    "EmailID": "dummy@sap.com",
    "AddressHistory": {
        "Address": [
            {
                "SequenceNo": "0",
                "AddressLine": "Kharghar,navi mumbai",
                "City": "Maharashtra-Mumbai",
                "State": "Maharashtra",
                "PinCode": "410210",
                "Country": "India",
                "Landmark": "",
                "StayFrom": "01-08-2013",
                "StayTo": "06-08-2021",
                "IsCurrentAddress": false,
                "IsPermanentAddress": false,
                "HouseNo": "",
                "AddressType": "Current",
                "DocList": {
                    "listofdocs": [
                        {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
                    ]
                }
            }
        ]
    },
    "EducationList": {
        "Education": [
            {
                "SequenceNo": "0",
                "Qualification": "",
                "Degree": "Under Graduate Degree",
                "CollegeName": "Amrutvahini College of engineering, Sangamner (Pune University)",
                "Location": "",
                "RollNumber": "123123",
                "UniversityName": "Mumbai University",
                "UniversityAddress": "",
                "PeriodFrom": "",
                "PeriodTo": "",
                "YearOfPassing": "2014",
                "Zipcode": "",
                "Percentage": "",
                "AdditionalRemarks": "10th/12th/Undergrad etc",
                "International": false,
                "Country": "",
                "DocList": {
                    "listofdocs": [
                        {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
                    ]
                }
            }
        ]
    },
    "EmploymentList": {
        "Employment": [
            {
                "SequenceNo": "0",
                "EmployerName": "Stravis Solutions",
                "EmployerAddress": "Bangalore",
                "EmployerContactNo": "",
                "Designation": "SDE",
                "EmployeeID": "asdas",
                "FixedSalary": "0",
                "IsCurrentEmployment": false,
                "RelievingDate": "15-10-2021",
                "Zipcode": "",
                "International": false,
                "Country": "",
                "PFNumber": "",
                "UANNumber": "",
                "DocList": {
                    "listofdocs": [
                        {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
                    ]
                },
                "DateOfJoining": "18-03-2015"
            },
            {
                "SequenceNo": "0",
                "EmployerName": "Stravis Solutions",
                "EmployerAddress": "Bangalore",
                "EmployerContactNo": "",
                "Designation": "SDE",
                "EmployeeID": "asdas",
                "FixedSalary": "0",
                "IsCurrentEmployment": false,
                "RelievingDate": "15-10-2021",
                "Zipcode": "",
                "International": false,
                "Country": "",
                "PFNumber": "",
                "UANNumber": "",
                "DocList": {
                    "listofdocs": [
                        {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
                    ]
                },
                "DateOfJoining": "18-03-2015"
            }
        ]
    },
    "AddressReferencesList": {
        "ListofReferences": [
            {
                "OrganizationName": "Com 1",
                "AdditionalRemarks": "",
                "NameOfReferee": "Ref1",
                "RefereeOccupation": "SDE",
                "RefereePhoneNumber": "123456",
                "RefereeEmailAddress": "Ref1@com1.com"
            },
            {
                "OrganizationName": "Com 1",
                "AdditionalRemarks": "",
                "NameOfReferee": "Ref2",
                "RefereeOccupation": "SDE",
                "RefereePhoneNumber": "123456",
                "RefereeEmailAddress": "Ref1@com1.com"
            }
        ]
    },
    "DOB": "03-08-2021",
    "DLDetails": {
        "DOB": "03-08-2021",
        "ApplicantName": "Test",
        "FatherName": "Test",
        "UniqueIDCode": "1231231",
        "DocList": {
            "listofdocs": [
                  {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
            ]
        }
    },
    "PanDetails": {
        "DOB": "03-08-2021",
        "ApplicantName": "Sunil Kumar  Yadav",
        "FatherName": "Sunil",
        "UniqueIDCode": "23123131",
        "DocList": {
            "listofdocs": [
                {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                   }
            ]
        }
    },
    "PVWDetails": {
        "AddressList": {
            "Address": [
                {
                    "SequenceNo": "0",
                    "AddressLine": "Kharghar,navi mumbai",
                    "City": "Maharashtra-Mumbai",
                    "State": "Maharashtra",
                    "PinCode": "410210",
                    "Country": "India",
                    "Landmark": "",
                    "StayFrom": "01-08-2013",
                    "StayTo": "06-08-2021",
                    "IsCurrentAddress": false,
                    "IsPermanentAddress": false,
                    "HouseNo": "Sai shradha CHS.Sector-11,",
                    "AddressType": "Current",
                    "DocList": {
                        "listofdocs": [
                            {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }
                        ]
                    }
                }
            ]
        },
        "DOB": "03-08-2021",
        "FatherName": "Sunil",
        "ApplicantName": "Sunil Kumar  Yadav"
    },
    "CreditDetail": {
        "ApplicantName": "Test",
        "DOB": "03-08-2021",
        "FatherName": "Test",
        "Gender": "Male",
        "UniqueIDCode": "Pan Number",
        "EmailID": "asda@gmail.com",
        "DocList": {
            "listofdocs": [
                {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }

            ]
        }
    },
    "PassportCheckDetails": {
        "NameInPassport": "Sunil Kumar  Yadav",
        "PassportNo": "1231231",
        "MachineReadableZone": "",
        "CandidateFirstName": "Sunil",
        "CandidateLastName": "Yadav",
        "DOB": "03-08-2021",
        "FatherName": "Sunil",
        "DocList": {
            "listofdocs": [
                {
                                    "DocumentName": "abc.jpg",
                            "DocumentPath": "base64"                        
                         }

            ]
        }
    }
}

正如您所看到的,为数据的每个部分都创建了一个数组。
到目前为止,无论我尝试了什么代码,转换后的JSON都没有任何数组,除非根目录下有多个记录。
我尝试了以下代码的变体:

<?xml version="1.0" encoding="UTF-8"?>
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://use your namespace">
    <xsl:output method="text"/>
    <xsl:template match="/ns0:Account_Resp">{
        <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 `enter code here`match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
    </xsl:stylesheet>

并收到以下输出-在其中您可以看到,对于单个有效负载,没有创建数组:

{
    "ClientID" : "1",
    "PackageID" : "650",
    "SBUID" : "2187",
    "CandidateID" : "456",
    "AssociateId" : "789",
    "FirstName" : "Meghana",
    "MiddleName" : "",
    "LastName" : "Rao",
    "FatherName" : "Satish",
    "ContactNo" : "7530001169",
    "EmailID" : "dummy@sap.com",

    "AddressHistory" : { "Address" :[{
    "SequenceNo" : "0",
    "AddressLine" : "Kharghar,navi mumbai",
    "City" : "Maharashtra-Mumbai",
    "State" : "Maharashtra",
    "PinCode" : "410210",
    "Country" : "India",
    "Landmark" : "",
    "StayFrom" : "01-08-2013",
    "StayTo" : "06-08-2021",
    "IsCurrentAddress" : "false",
    "IsPermanentAddress" : "false",
    "HouseNo" : "",
    "AddressType" : "Current",
    "DocList" : { "listofdocs" :[{
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}] }
},{
    "SequenceNo" : "1",
    "AddressLine" : "Kharghar,navi mumbai",
    "City" : "Maharashtra-Mumbai",
    "State" : "Maharashtra",
    "PinCode" : "410210",
    "Country" : "India",
    "Landmark" : "",
    "StayFrom" : "01-08-2013",
    "StayTo" : "06-08-2021",
    "IsCurrentAddress" : "false",
    "IsPermanentAddress" : "false",
    "HouseNo" : "",
    "AddressType" : "Current",
    "DocList" : { "listofdocs" :[{
    "DocumentName" : "def.jpg",
    "DocumentPath" : "base64"
}] }
}] },

    "EducationList" : {
    "Education" : {
    "SequenceNo" : "0",
    "Qualification" : "",
    "Degree" : "Under Graduate Degree",
    "CollegeName" : "Amrutvahini College of engineering, Sangamner (Pune University)",
    "Location" : "",
    "RollNumber" : "123123",
    "UniversityName" : "Mumbai University",
    "UniversityAddress" : "",
    "PeriodFrom" : "",
    "PeriodTo" : "",
    "YearOfPassing" : "2014",
    "Percentage" : "",
    "AdditionalRemarks" : "10th/12th/Undergrad etc",
    "International" : "false",
    "Country" : "",
    "DocList" : { "listofdocs" :[{
    "DocumentName" : "def.jpg",
    "DocumentPath" : "base64"
}] }
}
},
    "EmploymentList" : { "Employment" :[{
    "SequenceNo" : "0",
    "EmployerName" : "Stravis Solutions",
    "EmployerAddress" : "Bangalore",
    "EmployerContactNo" : "",
    "Designation" : "SDE",
    "EmployeeID" : "asdas",
    "FixedSalary" : "0",
    "IsCurrentEmployment" : "false",
    "RelievingDate" : "15-10-2021",
    "Zipcode" : "",
    "International" : "false",
    "Country" : "",
    "PFNumber" : "",
    "UANNumber" : "",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}
},
    "DateOfJoining" : "18-03-2015"
},{
    "SequenceNo" : "1",
    "EmployerName" : "Stravis Solutions",
    "EmployerAddress" : "Bangalore",
    "EmployerContactNo" : "",
    "Designation" : "SDE",
    "EmployeeID" : "asdas",
    "FixedSalary" : "0",
    "IsCurrentEmployment" : "false",
    "RelievingDate" : "15-10-2021",
    "Zipcode" : "",
    "International" : "false",
    "Country" : "",
    "PFNumber" : "",
    "UANNumber" : "",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "def.jpg",
    "DocumentPath" : "base64"
}
},
    "DateOfJoining" : "18-03-2015"
}] },

    "AddressReferencesList" : { "ListofReferences" :[{
    "OrganizationName" : "Com 1",
    "AdditionalRemarks" : "",
    "NameOfReferee" : "Ref1",
    "RefereeOccupation" : "SDE",
    "RefereePhoneNumber" : "123456",
    "RefereeEmailAddress" : "Ref1@com1.com"
},{
    "OrganizationName" : "Com 1",
    "AdditionalRemarks" : "",
    "NameOfReferee" : "Ref2",
    "RefereeOccupation" : "SDE",
    "RefereePhoneNumber" : "123456",
    "RefereeEmailAddress" : "Ref1@com1.com"
}] },
    "DOB" : "03-08-2021",
    "DLDetails" : {
    "DOB" : "03-08-2021",
    "ApplicantName" : "Test",
    "FatherName" : "Test",
    "UniqueIDCode" : "1231231",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}
}
},
    "PanDetails" : {
    "DOB" : "03-08-2021",
    "ApplicantName" : "Sunil Kumar  Yadav",
    "FatherName" : "Sunil",
    "UniqueIDCode" : "23123131",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}
}
},
    "PVWDetails" : {
    "AddressList" : {
    "Address" : {
    "SequenceNo" : "0",
    "AddressLine" : "Kharghar,navi mumbai",
    "City" : "Maharashtra-Mumbai",
    "State" : "Maharashtra",
    "PinCode" : "410210",
    "Country" : "India",
    "Landmark" : "",
    "StayFrom" : "01-08-2013",
    "StayTo" : "06-08-2021",
    "IsCurrentAddress" : "false",
    "IsPermanentAddress" : "false",
    "HouseNo" : "Sai shradha CHS.Sector-11,",
    "AddressType" : "Current"
}
},
    "DOB" : "03-08-2021",
    "FatherName" : "Sunil",
    "ApplicantName" : "Sunil Kumar  Yadav"
},
    "CreditDetail" : {
    "ApplicantName" : "Test",
    "DOB" : "03-08-2021",
    "FatherName" : "Test",
    "Gender" : "Male",
    "UniqueIDCode" : "Pan Number",
    "EmailID" : "asda@gmail.com",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}
}
},        "DrugTestPanelCheck" : {
    "DrugTestPanel" : "DrugTestPanel5",
    "ApplicantName" : "Test Candidate"
},
    "GDCDetails" : {
    "ApplicantName" : "Sunil Kumar  Yadav",
    "DOB" : "03-08-2021",
    "FatherName" : "Sunil"
},
    "PassportCheckDetails" : {
    "NameInPassport" : "Sunil Kumar  Yadav",
    "PassportNo" : "1231231",
    "MachineReadableZone" : "",
    "CandidateFirstName" : "Sunil",
    "CandidateLastName" : "Yadav",
    "DOB" : "03-08-2021",
    "FatherName" : "Sunil",
    "DocList" : {
    "listofdocs" : {
    "DocumentName" : "abc.jpg",
    "DocumentPath" : "base64"
}
}
}
}

真的需要一些帮助。谢谢

btxsgosb

btxsgosb1#

为了确定哪些XML元素的基数可以为0..,只需通过测试count(*[name()=$childName]) > 1是否为0来查看有效负载中的实际基数,这样就忽略了<EducationList> * 被允许 * 包含多个<Education>(即使在这个特定的有效负载示例中它只包含一个<Education>)。
一个干净的解决方案需要查看XML模式(可能在WSDL文档中)。如果元素名称以List结尾,则它包含基数为0..
的子元素。因此,将测试count(*[name()=$childName]) > 1替换为

substring(name(), string-length(name()) - 3, 4) = 'List'
rvpgvaaj

rvpgvaaj2#

使用YAML处理器mikefarah/yq,可以将XML转换为JSON,并对转换进行一些自定义调整:

yq --input-format xml --output-format json '
with(.root;
     .AddressHistory.Address                 |= select(type == "!!map") |= [.] |
     .EducationList.Education                |= select(type == "!!map") |= [.] |
     .EmploymentList.Employment              |= select(type == "!!map") |= [.] |
     .AddressReferencesList.ListofReferences |= select(type == "!!map") |= [.] |
     .PVWDetails.AddressList.Address         |= select(type == "!!map") |= [.]
    )
   ' input.xml

对于给定的路径,更新操作符|=将对象(select(type == "!!map")) Package 到数组([.])中。将所有应该是数组的元素添加到代码中。
使用kislyuk/xq,您可以这样解决它:

ARRAY_PATHS='
[
  "root.AddressHistory.Address",
  "root.EducationList.Education",
  "root.EmploymentList.Employment",
  "root.AddressReferencesList.ListofReferences",
  "root.PVWDetails.AddressList.Address"
]
'

xq --argjson paths "$ARRAY_PATHS" '
  . as $input
  | reduce ($paths
            | map(split(".")                                              # split given paths by "."
                  | . as $p
                  | select($input | getpath($p) | type == "object")))[]   # process only objects at the given paths (ignore arrays)
           as $path
           (.; setpath($path; [getpath($path)]))                          # wrap objects at the given paths in an array
 ' input.xml

如果值是对象,则会检查所有指定的路径:select($input | getpath($p) | type == "object")。对象被 Package 到一个数组setpath($path; [getpath($path)])中。如果输入中缺少给定的路径,这个解决方案更通用,也更安全。

输出(两个版本)

{
  "root": {
    "ClientID": "1",
    "PackageID": "650",
    "SBUID": "2187",
    "CandidateID": "456",
    "AssociateId": "789",
    "FirstName": "Meghana",
    "MiddleName": null,
    "LastName": "Rao",
    "FatherName": "Satish",
    "ContactNo": "7530001169",
    "EmailID": "dummy@sap.com",
    "AddressHistory": {
      "Address": [
        {
          "SequenceNo": "0",
          "AddressLine": "Kharghar,navi mumbai",
          "City": "Maharashtra-Mumbai",
          "State": "Maharashtra",
          "PinCode": "410210",
          "Country": "India",
          "Landmark": null,
          "StayFrom": "01-08-2013",
          "StayTo": "06-08-2021",
          "IsCurrentAddress": "false",
          "IsPermanentAddress": "false",
          "HouseNo": null,
          "AddressType": "Current",
          "DocList": {
            "listofdocs": {
              "DocumentName": "abc.jpg",
              "DocumentPath": "base64"
            }
          }
        }
      ]
    },
    "EducationList": {
      "Education": [
        {
          "SequenceNo": "0",
          "Qualification": null,
          "Degree": "Under Graduate Degree",
          "CollegeName": "Amrutvahini College of engineering, Sangamner (Pune University)",
          "Location": null,
          "RollNumber": "123123",
          "UniversityName": "Mumbai University",
          "UniversityAddress": null,
          "PeriodFrom": null,
          "PeriodTo": null,
          "YearOfPassing": "2014",
          "Zipcode": null,
          "Percentage": null,
          "AdditionalRemarks": "10th/12th/Undergrad etc",
          "International": "false",
          "Country": null,
          "DocList": {
            "listofdocs": {
              "DocumentName": "abc.jpg",
              "DocumentPath": "base64"
            }
          }
        }
      ]
    },
    "EmploymentList": {
      "Employment": [
        {
          "SequenceNo": "0",
          "EmployerName": "Stravis Solutions",
          "EmployerAddress": "Bangalore",
          "EmployerContactNo": null,
          "Designation": "SDE",
          "EmployeeID": "asdas",
          "FixedSalary": "0",
          "IsCurrentEmployment": "false",
          "RelievingDate": "15-10-2021",
          "State": null,
          "City": null,
          "Zipcode": null,
          "International": "false",
          "Country": null,
          "PFNumber": null,
          "UANNumber": null,
          "DocList": {
            "listofdocs": {
              "DocumentName": "abc.jpg",
              "DocumentPath": "base64"
            }
          },
          "DateOfJoining": "18-03-2015"
        },
        {
          "SequenceNo": "0",
          "EmployerName": "Stravis Solutions",
          "EmployerAddress": "Bangalore",
          "EmployerContactNo": null,
          "Designation": "SDE",
          "EmployeeID": "asdas",
          "FixedSalary": "0",
          "IsCurrentEmployment": "false",
          "RelievingDate": "15-10-2021",
          "International": "false",
          "Country": null,
          "PFNumber": null,
          "UANNumber": null,
          "DocList": {
            "listofdocs": {
              "DocumentName": "abc.jpg",
              "DocumentPath": "base64"
            }
          },
          "DateOfJoining": "18-03-2015"
        }
      ]
    },
    "AddressReferencesList": {
      "ListofReferences": [
        {
          "OrganizationName": "Com 1",
          "AdditionalRemarks": null,
          "NameOfReferee": "Ref1",
          "RefereeOccupation": "SDE",
          "RefereePhoneNumber": "123456",
          "RefereeEmailAddress": "Ref1@com1.com"
        },
        {
          "OrganizationName": "Com 1",
          "AdditionalRemarks": null,
          "NameOfReferee": "Ref1",
          "RefereeOccupation": "SDE",
          "RefereePhoneNumber": "123456",
          "RefereeEmailAddress": "Ref1@com1.com"
        }
      ]
    },
    "DOB": "03-08-2021",
    "DLDetails": {
      "DOB": "03-08-2021",
      "ApplicantName": "Test",
      "FatherName": "Test",
      "dl_remarks": null,
      "UniqueIDCode": "1231231",
      "DocList": {
        "listofdocs": {
          "DocumentName": "abc.jpg",
          "DocumentPath": "base64"
        }
      }
    },
    "PanDetails": {
      "DOB": "03-08-2021",
      "ApplicantName": "Sunil Kumar  Yadav",
      "FatherName": "Sunil",
      "pan_remarks": null,
      "UniqueIDCode": "23123131",
      "DocList": {
        "listofdocs": {
          "DocumentName": "abc.jpg",
          "DocumentPath": "base64"
        }
      }
    },
    "PVWDetails": {
      "AddressList": {
        "Address": [
          {
            "SequenceNo": "0",
            "AddressLine": "Kharghar,navi mumbai",
            "City": "Maharashtra-Mumbai",
            "State": "Maharashtra",
            "PinCode": "410210",
            "Country": "India",
            "Landmark": null,
            "StayFrom": "01-08-2013",
            "StayTo": "06-08-2021",
            "IsCurrentAddress": "false",
            "IsPermanentAddress": "false",
            "HouseNo": "Sai shradha CHS.Sector-11,",
            "AddressType": "Current",
            "DocList": {
              "listofdocs": {
                "DocumentName": "abc.jpg",
                "DocumentPath": "base64"
              }
            }
          }
        ]
      },
      "DOB": "03-08-2021",
      "FatherName": "Sunil",
      "ApplicantName": "Sunil Kumar  Yadav"
    },
    "CreditDetail": {
      "ApplicantName": "Test",
      "DOB": "03-08-2021",
      "FatherName": "Test",
      "Gender": "Male",
      "UniqueIDCode": "Pan Number",
      "EmailID": "asda@gmail.com",
      "DocList": {
        "listofdocs": {
          "DocumentName": "abc.jpg",
          "DocumentPath": "base64"
        }
      }
    },
    "DrugTestPanelCheck": {
      "DrugTestPanel": "DrugTestPanel5",
      "ApplicantName": "Test Candidate"
    },
    "GDCDetails": {
      "ApplicantName": "Sunil Kumar  Yadav",
      "DOB": "03-08-2021",
      "FatherName": "Sunil"
    },
    "PassportCheckDetails": {
      "NameInPassport": "Sunil Kumar  Yadav",
      "PassportNo": "1231231",
      "MachineReadableZone": null,
      "CandidateFirstName": "Sunil",
      "CandidateLastName": "Yadav",
      "DOB": "03-08-2021",
      "FatherName": "Sunil",
      "DocList": {
        "listofdocs": {
          "DocumentName": "abc.jpg",
          "DocumentPath": "base64"
        }
      }
    }
  }
}

相关问题