Gopinath
Gopinath

Reputation: 57

XSLT - Get data from HTTP Json endpoint and transform it

I am trying to retrieve JSON data from the HTTP endpoint and use that values in XSLT.

Need help to retrieve data from HTTP URL of JSON data and transform it.

//sample json
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
} 

// xslt
<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>   
    <xsl:template match="*">
        <person>
                    <xsl:variable name="title" select="json-to-xml(document('https://jsonplaceholder.typicode.com/todos/1'))/title"/>
                    <xsl:element name="Field">
                        <xsl:attribute name="name">Title</xsl:attribute>
                        <xsl:attribute name="value">
                            <xsl:value-of select="$title"/>
                        </xsl:attribute>
                    </xsl:element>                    
                </person>           
    </xsl:template>
</xsl:stylesheet> ```

Getting error like Error: org.xml.sax.SAXParseException; systemId: https://jsonplaceholder.typicode.com/todos/1; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

Upvotes: 0

Views: 165

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167641

To load JSON, you can use json-doc e.g. json-doc('https://jsonplaceholder.typicode.com/todos/1'). That gives you an XDM map for your sample, you can process it with XPath 3.1 e.g.

<xsl:variable name="json" select="json-doc('https://jsonplaceholder.typicode.com/todos/1')"/>
<person>
  <Field name="Title" value="{$json?title}"/>
</person>

Upvotes: 1

Related Questions