on 09-12-2016 6:59 AM
Hi Experts,
I have one xml as below:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<TransferFileResponse xmlns="http://tempuri.org/">
<TransferFileResult xmlns:a="http://abc.com/gds.externalServices/data" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Errors xmlns:b="https://ccs.abc.com.au/Internal/">
<b:ServiceRequestError>
<b:ErrorCode>InternalError</b:ErrorCode>
<b:ErrorMessage>Error Occurred. Please refer the episode id 'e2f2bc27' for further help.</b:ErrorMessage>
<b:ErrorTime>2016-09-12T13:43:32.7413505+10:00</b:ErrorTime>
</b:ServiceRequestError>
<b:ServiceRequestError>
<b:ErrorCode>InvalidData</b:ErrorCode>
<b:ErrorMessage>[MELYDEVAPP816] - Invalid reference code '95' specified.</b:ErrorMessage>
<b:ErrorTime>2016-09-12T13:42:54.5554226+10:00</b:ErrorTime>
</b:ServiceRequestError>
</a:Errors>
<a:IsSuccessful>false</a:IsSuccessful>
<a:ServiceReferenceId>82338378</a:ServiceReferenceId>
</TransferFileResult>
</TransferFileResponse>
</s:Body>
</s:Envelope>
I want the output to be generated as below:
<TransferFileResponse xmlns="http://tempuri.org/">
<TransferFileResult xmlns:a="http://abc.com/gds.externalServices/datacontracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Errors xmlns="http:/abc.com/gds.externalServices/datacontracts">
<ServiceRequestError xmlns="https://ccs.abc.com.au/CCSConnectInternal/">
<ErrorCode>InternalError</ErrorCode>
<ErrorMessage>Error Occurred. Please refer the episode id 'e2f2bc27' for further help.</ErrorMessage>
<ErrorTime>2016-09-12T13:43:32.7413505+10:00</ErrorTime>
</ServiceRequestError>
<ServiceRequestError xmlns="https://ccs.abc.com.au/CCSConnectInternal/">
<ErrorCode>InvalidData</ErrorCode>
<ErrorMessage>[MELYDEVAPP816] - Invalid reference code '95' specified.</ErrorMessage>
<ErrorTime>2016-09-12T13:42:54.5554226+10:00</ErrorTime>
</ServiceRequestError>
</Errors>
<IsSuccessful xmlns="http://abc.com/gds.externalServices/datacontracts">false</IsSuccessful>
<ServiceReferenceId xmlns="http://abc.com/gds.externalServices/datacontracts">82338378</ServiceReferenceId>
</TransferFileResult>
</TransferFileResponse>
Could you please let me know how can I use xslt code to generate the output xml. I have tried with some but not able to get the exact output.
Appreciate your help.
Regards,
Treya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thankyou all for such useful suggestions.
I applied the solution provided by Gaurav according to my requirement and got the expected output.
Thanks & Regards,
Treya Dasgupta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Actually I have more than one requirement:
So, if you could please assist me in the last point specially then it would be great.
Regards,
Treya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Treya!
I guess it's not the best XSL transformation , but you could try it for your requirement:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:usr="urn://user_values"
xmlns:a="http://abc.com/gds.externalServices/datacontracts"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="a i"
version="1.0">
<xsl:output indent="yes"/>
<usr:ns_list xml:space="preserve">
<item key="TransferFileResponse">http://tempuri.org/</item>
<item key="Errors">http://abc.com/gds.externalServices/datacontracts</item>
<item key="ServiceRequestError">https://ccs.abc.com.au/CCSConnectInternal/</item>
<item key="IsSuccessful">http://abc.com/gds.externalServices/datacontracts</item>
<item key="ServiceReferenceId">http://abc.com/gds.externalServices/datacontracts</item>
</usr:ns_list>
<xsl:variable name="nsPatterns" select="document('')/*/usr:ns_list/*"/>
<xsl:variable name="v_ns_a" select="document('')/*/namespace::*[name()='a']"/>
<xsl:variable name="v_ns_i" select="document('')/*/namespace::*[name()='i']"/>
<xsl:template match="/">
<xsl:apply-templates select="//*[local-name()='TransferFileResponse']">
<xsl:with-param name="elementNS" select="''"></xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<xsl:template name="createNodes" match="*">
<xsl:param name="elementNS"><xsl:value-of select="namespace-uri()"/></xsl:param>
<xsl:variable name="nodeName" select="local-name()"/>
<xsl:variable name="subElementsNS">
<xsl:choose>
<xsl:when test="$nsPatterns[@key=$nodeName]">
<xsl:value-of select="$nsPatterns[@key=$nodeName][1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$elementNS"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$nodeName}" namespace="{$subElementsNS}">
<xsl:if test="$nodeName='TransferFileResult'">
<xsl:copy-of select="$v_ns_a"/>
<xsl:copy-of select="$v_ns_i"/>
</xsl:if>
<xsl:apply-templates select="@* | node()">
<xsl:with-param name="elementNS" select="$subElementsNS"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="comment() | text() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
Source XML:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<TransferFileResponse xmlns="http://tempuri.org/">
<TransferFileResult xmlns:a="http://abc.com/gds.externalServices/data" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Errors xmlns:b="https://ccs.abc.com.au/Internal/">
<b:ServiceRequestError>
<b:ErrorCode>InternalError</b:ErrorCode>
<b:ErrorMessage>Error Occurred. Please refer the episode id 'e2f2bc27' for further help.</b:ErrorMessage>
<b:ErrorTime>2016-09-12T13:43:32.7413505+10:00</b:ErrorTime>
</b:ServiceRequestError>
<b:ServiceRequestError>
<b:ErrorCode>InvalidData</b:ErrorCode>
<b:ErrorMessage>[MELYDEVAPP816] - Invalid reference code '95' specified.</b:ErrorMessage>
<b:ErrorTime>2016-09-12T13:42:54.5554226+10:00</b:ErrorTime>
</b:ServiceRequestError>
</a:Errors>
<a:IsSuccessful>false</a:IsSuccessful>
<a:ServiceReferenceId>82338378</a:ServiceReferenceId>
</TransferFileResult>
</TransferFileResponse>
</s:Body>
</s:Envelope>
Result XML:
<?xml version="1.0" encoding="utf-8"?>
<TransferFileResponse xmlns="http://tempuri.org/">
<TransferFileResult xmlns:a="http://abc.com/gds.externalServices/datacontracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Errors xmlns="http://abc.com/gds.externalServices/datacontracts">
<ServiceRequestError xmlns="https://ccs.abc.com.au/CCSConnectInternal/">
<ErrorCode>InternalError</ErrorCode>
<ErrorMessage>Error Occurred. Please refer the episode id 'e2f2bc27' for further help.</ErrorMessage>
<ErrorTime>2016-09-12T13:43:32.7413505+10:00</ErrorTime>
</ServiceRequestError>
<ServiceRequestError xmlns="https://ccs.abc.com.au/CCSConnectInternal/">
<ErrorCode>InvalidData</ErrorCode>
<ErrorMessage>[MELYDEVAPP816] - Invalid reference code '95' specified.</ErrorMessage>
<ErrorTime>2016-09-12T13:42:54.5554226+10:00</ErrorTime>
</ServiceRequestError>
</Errors>
<IsSuccessful xmlns="http://abc.com/gds.externalServices/datacontracts">false</IsSuccessful>
<ServiceReferenceId xmlns="http://abc.com/gds.externalServices/datacontracts">82338378</ServiceReferenceId>
</TransferFileResult>
</TransferFileResponse>
Regards, Evgeniy.
Hi Treya!
Are you sure that you need output like above?
For example, your prefixes "a" and "i" are never used in described output XML structure.
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.