我目前正在使用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"
}
]
}
}
}
正如您所看到的,有一个为数据的每一部分创建的数组。如何使用XSLT实现这一点?
到目前为止,无论我尝试过什么代码,转换后的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"
}
}
}
}
真的需要一些帮助。感谢
为了确定哪些XML元素可以出现基数为0..*的情况,您只需要通过测试count(*[name()=$childName]) > 1
。这样,您就忽略了<EducationList>
被允许包含多个<Education>
的事实(即使它在这个特定的有效负载实例中只包含一个)。
一个干净的解决方案需要查看XML模式(可能在WSDL文档中)。但在这种情况下,以下启发式方法可能就足够了:如果元素名称以List
结尾,则它包含基数为0..*的子元素。因此,用替换测试count(*[name()=$childName]) > 1
substring(name(), string-length(name()) - 3, 4) = 'List'
使用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")
)包装成数组([.]
)。将所有应该是数组的元素添加到代码中。
使用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")
,则会检查所有给定的路径。对象被包装成一个数组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"
}
}
}
}
}
标准的xml-to-json
XSLT 3.0函数可以满足您的需求。复杂性在于建立何时使用JSON映射和数组的转换规则。
我从提供的输入XML和输出JSON推断出转换规则。这些规则可能需要调整,以涵盖未经样本测试的情况。这是xsl:template
的主要指令:
<xsl:template match="*[*]" mode="outer">
<xsl:param name="key" as="xs:string?"/>
<xsl:variable name="distinctChildNames" as="xs:string*" select="*!name() => distinct-values()"/>
<xsl:choose>
<xsl:when test="count($distinctChildNames) gt 1 and exists($key)">
<array>
<xsl:sequence select="fn:keyAttribute(name())"/>
<map>
<xsl:apply-templates select="*" mode="outer">
<xsl:with-param name="key" select="name()"/>
</xsl:apply-templates>
</map>
</array>
</xsl:when>
<xsl:when test="count(*) gt 1 and count($distinctChildNames) eq 1">
<map>
<xsl:sequence select="fn:keyAttribute(name())"/>
<array key="{name(*[1])}">
<xsl:for-each select="*">
<map>
<xsl:apply-templates select="*" mode="outer"/>
</map>
</xsl:for-each>
</array>
</map>
</xsl:when>
<xsl:otherwise>
<map>
<xsl:sequence select="fn:keyAttribute(name())"/>
<xsl:apply-templates select="*" mode="outer">
<xsl:with-param name="key" select="name()"/>
</xsl:apply-templates>
</map>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
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",
"State": "",
"City": "",
"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",
"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": "Ref1",
"RefereeOccupation": "SDE",
"RefereePhoneNumber": "123456",
"RefereeEmailAddress": "Ref1@com1.com"
}
]
},
"DOB": "03-08-2021",
"DLDetails": {
"DOB": "03-08-2021",
"ApplicantName": "Test",
"FatherName": "Test",
"dl_remarks": "",
"UniqueIDCode": "1231231",
"DocList": {
"listofdocs": [
{
"DocumentName": "abc.jpg",
"DocumentPath": "base64"
}
]
}
},
"PanDetails": {
"DOB": "03-08-2021",
"ApplicantName": "Sunil Kumar Yadav",
"FatherName": "Sunil",
"pan_remarks": "",
"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"
}
]
}
},
"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"
}
]
}
}
}
结果略有不同:
- JSON输出中的'/'字符转义为
/
- 从空XML元素转换而来的JSON属性:这里的XSLT始终保留该属性并将值设置为空字符串
下面是使用的完整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"
xmlns:fn="com.example.functions"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">
<xsl:output method="text" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:mode name="outer" on-no-match="shallow-copy"/>
<xsl:template match="/*">
<xsl:variable name="result" as="node()*">
<map>
<xsl:apply-templates select="*" mode="outer"/>
</map>
</xsl:variable>
<xsl:sequence select="xml-to-json($result)"/>
</xsl:template>
<xsl:template match="*[*]" mode="outer">
<xsl:param name="key" as="xs:string?"/>
<xsl:variable name="distinctChildNames" as="xs:string*" select="*!name() => distinct-values()"/>
<xsl:choose>
<xsl:when test="count($distinctChildNames) gt 1 and exists($key)">
<array>
<xsl:sequence select="fn:keyAttribute(name())"/>
<map>
<xsl:apply-templates select="*" mode="outer">
<xsl:with-param name="key" select="name()"/>
</xsl:apply-templates>
</map>
</array>
</xsl:when>
<xsl:when test="count(*) gt 1 and count($distinctChildNames) eq 1">
<map>
<xsl:sequence select="fn:keyAttribute(name())"/>
<array key="{name(*[1])}">
<xsl:for-each select="*">
<map>
<xsl:apply-templates select="*" mode="outer"/>
</map>
</xsl:for-each>
</array>
</map>
</xsl:when>
<xsl:otherwise>
<map>
<xsl:sequence select="fn:keyAttribute(name())"/>
<xsl:apply-templates select="*" mode="outer">
<xsl:with-param name="key" select="name()"/>
</xsl:apply-templates>
</map>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*[empty(*) and exists(text())]" mode="outer">
<xsl:apply-templates select="node()" mode="outer">
<xsl:with-param name="key" select="name()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*[empty(*) and empty(text())]" mode="outer">
<string key="{name()}"/>
</xsl:template>
<xsl:template match="text()[. = ('true', 'false')]" mode="outer">
<xsl:param name="key" as="xs:string?"/>
<boolean key="{$key}">{.}</boolean>
</xsl:template>
<xsl:template match="text()" mode="outer">
<xsl:param name="key" as="xs:string?"/>
<xsl:if test="exists($key)">
<string key="{$key}">{.}</string>
</xsl:if>
</xsl:template>
<xsl:function name="fn:keyAttribute" as="attribute()?">
<xsl:param name="key" as="xs:string?"/>
<xsl:if test="$key">
<xsl:attribute name="key" select="$key"/>
</xsl:if>
</xsl:function>
</xsl:stylesheet>