能否按指定格式生成带XSD Schema的JSON?XML转JSON对接需求
Hey there! Let's tackle your XML-to-JSON conversion problem step by step. First, I noticed a small syntax error in the sample JSON you provided—there was a missing comma after the subAccount object. I've fixed that first to ensure we're targeting a valid JSON structure:
{ "checkups": [ { "checkupId": " 20 ", "jobTitle": " Busisness Analyst, Project Management ", "requisitionNumber": " F834234 ", "subAccount": { "email": "ramesh.rathod@uic.com", "firstName": " Ramesh ", "lastName": " Rathod ", "phone": " +1 (189) 234-1122 x11-1275 " }, "candidate": { "email": "srujan.rao@gmail.com", "firstName": " srujan ", "lastName": " rao " } } ] }
Assumptions About Your XML Structure
Since you didn't share your system's XML output, I'll assume a straightforward XML structure that maps directly to the JSON above (adjust paths later if your XML differs):
<checkups> <checkup> <checkupId> 20 </checkupId> <jobTitle> Busisness Analyst, Project Management </jobTitle> <requisitionNumber> F834234 </requisitionNumber> <subAccount> <email>ramesh.rathod@uic.com</email> <firstName> Ramesh </firstName> <lastName> Rathod </lastName> <phone> +1 (189) 234-1122 x11-1275 </phone> </subAccount> <candidate> <email>srujan.rao@gmail.com</email> <firstName> srujan </firstName> <lastName> rao </lastName> </candidate> </checkup> </checkups>
Solution 1: XSLT 3.0 for Direct XML-to-JSON Conversion
XSLT 3.0 has native support for JSON output, making it the ideal tool here. Here's a stylesheet that will transform your XML to the exact JSON format you need:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="json" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:map> <xsl:map-entry key="'checkups'" select="array{/checkups/checkup!xsl:map{ 'checkupId' : string(checkupId), 'jobTitle' : string(jobTitle), 'requisitionNumber' : string(requisitionNumber), 'subAccount' : xsl:map{ 'email' : string(subAccount/email), 'firstName' : string(subAccount/firstName), 'lastName' : string(subAccount/lastName), 'phone' : string(subAccount/phone) }, 'candidate' : xsl:map{ 'email' : string(candidate/email), 'firstName' : string(candidate/firstName), 'lastName' : string(candidate/lastName) } }}"/> </xsl:map> </xsl:template> </xsl:stylesheet>
How This Works:
xsl:output method="json"tells the processor to generate valid JSON instead of XML.xsl:mapconstructs JSON objects, whilearray{}builds the JSON array forcheckups.- The
string()function ensures node values are converted to JSON strings, preserving any leading/trailing spaces (usenormalize-space()instead if you want to trim whitespace).
Solution 2: XSD Schema for XML Validation (Optional but Recommended)
If you want to ensure your input XML adheres to a consistent structure before conversion, you can use this XSD to validate it first:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="checkups"> <xs:complexType> <xs:sequence> <xs:element name="checkup" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="checkupId" type="xs:string"/> <xs:element name="jobTitle" type="xs:string"/> <xs:element name="requisitionNumber" type="xs:string"/> <xs:element name="subAccount"> <xs:complexType> <xs:sequence> <xs:element name="email" type="xs:string"/> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string"/> <xs:element name="phone" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="candidate"> <xs:complexType> <xs:sequence> <xs:element name="email" type="xs:string"/> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Quick Adjustments for Your Actual XML
If your system's XML has different node names or structure, just modify the XPath expressions in the XSLT. For example, if your root node is <checkupList> instead of <checkups>, change /checkups/checkup to /checkupList/checkup.
内容的提问来源于stack exchange,提问作者user8340629




