Reputation: 150
I have to transform following result using XSLT transformation.
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:Response xmlns:ns2="http://urldata.com/">
<return>{"accountstatus":false,"response":{"MessageData":"Test","Code":"99","errors":{"name":"Hi","code":"100"}}}</return>
</ns2:Response>
</S:Body></S:Envelope>
And i Tried using below
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-declaration="yes"/>
<xsl:template match="/">
<ns1:TranResponse xmlns:ns1="http://test.com">
<ns1:TranResult>
<xsl:element name="ns1:AccountStatus">
<xsl:value-of select="//jsonObject/return"/>
</xsl:element>
</ns1:TranResult>
</ns1:TranResponse>
</xsl:template>
</xsl:stylesheet>
And Result Like This
<ns1:ResultSet>
<ns1:AccountStatus>{"accountstatus":false,"response":{"MessageData":"Test","Code":"99",
"errors":{"name":"Hi","code":"100"}}}</ns1:AccountStatus>
<ns1:ResultSet>
But i need is result like below,
<ns1:Results>
<ns1:ResultSet>
<ns1:AccountStatus>false</ns1:AccountStatus>
<ns1:ResultSet/>
<ns1:ResultSet>
<ns1:MessageData>test</ns1:MessageData>
<ns1:ResultSet/>
<ns1:ResultSet>
<ns1:Code>99</ns1:Code>
<ns1:ResultSet/>
<ns1:ResultSet>
<ns1:name>Hi</ns1:name>
<ns1:ResultSet/>
<ns1:ResultSet>
<ns1:code>100</ns1:Code>
<ns1:ResultSet/>
<ns1:Results/>
Any one can figure this out please figure this out
Upvotes: 2
Views: 719
Reputation: 190
If you're only able to use XSL 3.0 it's no problem. Just parse the JSON as XML and you're good to go:
<?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:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="true"/>
<xsl:variable name="data" select="//return => json-to-xml()"></xsl:variable>
<xsl:template match="/" xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
<ns1:root xmlns:ns1="http://urldata.com/ns1">
<ns1:MessageData>
<xsl:value-of select="$data//boolean[@key = 'accountstatus']"/>
</ns1:MessageData>
<ns1:Code>
<xsl:value-of select="$data//map[@key = 'response']/string[@key = 'Code']"/>
</ns1:Code>
<!--- use this approach for everything you want to select ... -->
</ns1:root>
</xsl:template>
</xsl:stylesheet>
See Fiddle.
Upvotes: 3