开发XML转JSON-API的API服务,是否有适配的XSLT Schema?
Great question! Let's walk through how to tackle this, since converting simple XML to JSON-API via XSLT 3.0 is totally doable—even if there's no official "XSLT Schema" tailored specifically for this exact use case. Here's what you need to know:
xml-to-json() for JSON-API Compliance JSON-API has strict structural rules (like top-level data, attributes, and relationships fields, standardized resource object formats, etc.). The key is to first map your input XML to an intermediate XML structure that aligns with JSON-API, then use XSLT 3.0's xml-to-json() function to convert that to valid JSON.
Step 1: Map Your XML to JSON-API's Structure
Let's use a concrete example. Suppose your input XML looks like this:
<user> <id>123</id> <name>John Doe</name> <email>john@example.com</email> <postId>456</postId> </user>
You'll write an XSLT stylesheet that transforms this into the structured XML expected by xml-to-json() (using the json namespace for type hints), while adhering to JSON-API specs:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.w3.org/2005/xpath-functions/json"> <xsl:output method="text"/> <xsl:template match="/"> <!-- Build intermediate XML matching JSON-API structure --> <xsl:variable name="json-api-xml"> <json:object> <data> <json:object> <type> <json:string>users</json:string> </type> <id> <json:string><xsl:value-of select="user/id"/></json:string> </id> <attributes> <json:object> <name> <json:string><xsl:value-of select="user/name"/></json:string> </name> <email> <json:string><xsl:value-of select="user/email"/></json:string> </email> </json:object> </attributes> <relationships> <json:object> <post> <json:object> <data> <json:object> <type> <json:string>posts</json:string> </type> <id> <json:string><xsl:value-of select="user/postId"/></json:string> </id> </json:object> </data> </json:object> </post> </json:object> </relationships> </json:object> </data> </json:object> </xsl:variable> <!-- Convert to formatted JSON --> <xsl:value-of select="xml-to-json($json-api-xml, map { 'indent': true() })"/> </xsl:template> </xsl:stylesheet>
Step 2: Reuse Generic JSON-API Conversion Logic
If you're handling multiple resource types (users, posts, comments, etc.), abstract reusable templates to avoid repetition:
- A template for generating resource objects (handles
type,id, andattributes) - A template for building
relationshipsfrom nested or referenced XML elements
This makes your stylesheet easier to maintain as your API grows.
Are There Pre-Built XSLT Solutions?
While there's no official standardized XSLT schema or stylesheet for JSON-API, the community has shared open-source XSLT 3.0 snippets and templates that you can adapt:
- Generic templates that dynamically map XML elements to JSON-API
attributesorrelationshipsbased on naming conventions - Logic for handling collections (converting XML lists to JSON-API's
dataarrays)
Key Considerations
- XSLT 3.0 Support: Make sure your processor (like Saxon HE/PE/EE, or Altova XMLSpy) fully supports XSLT 3.0 and the
xml-to-json()function. - JSON-API Details: Stick to JSON-API rules—like using plural
typevalues, ensuringidis a string, and separating resource attributes from relationships. - Nested XML: Don't flatten nested XML into
attributes; map them torelationshipsinstead to follow JSON-API's resource linkage pattern.
内容的提问来源于stack exchange,提问作者mkardakov

