如何使用 XSLT 转换来转换 Soap 对象数据 [英] How to transform Soap object data using XSLT transformation
本文介绍了如何使用 XSLT 转换来转换 Soap 对象数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用 XSLT 转换来转换以下结果.
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>
我尝试使用以下
<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
推荐答案
如果您只能使用 XSL 3.0,那没问题.只需将 JSON 解析为 XML 就可以了:
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>
参见Fiddle.
这篇关于如何使用 XSLT 转换来转换 Soap 对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文