Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • kinra 2 posts 82 karma points
    Dec 16, 2015 @ 03:17
    kinra
    0

    XML Transformation using XSLT -get value-of select and to delete a element

    Hello there,

    Need Help...

    I am converting source xml to target xml using xslt . I have below two questions :

    1. How to the get a value of element <ns0:SID>4567</ns0:SID> from source XML to Target XML element <urn:sessionId /> using XSLT
    2. How to delete element <ns0:SID>4567</ns0:SID> when source XML transforms to Target XML

    Below are the source XML, XSLT Transform and Target XML

    Source XML :

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:Submit_Input
    xmlns:ns0="http://soap.sforce.com/schemas/class/test">
    <ns0:ListOfInboundResponse>
    <ns0:TransactionHeader>
    <ns0:ListOfTransactionDetailItems>
    <ns0:ErrorDesc>123</ns0:ErrorDesc>
    <ns0:ErrorNum />
    <ns0:ErrorSeverity />
    <ns0:LineNumber />
    <ns0:Status />
    </ns0:ListOfTransactionDetailItems>
    <ns0:PONumber />
    <ns0:SID>4567</ns0:SID> 
    <ns0:TransactionId />
    <ns0:TransactionMode />
    <ns0:TransactionNumber />
    <ns0:TransactionRowId />
    <ns0:TransactionStatus />
    <ns0:TransactionType />
    </ns0:TransactionHeader>
    <ns0:TransactionHeader>
    <ns0:ListOfTransactionDetailItems>
    <ns0:ErrorDesc />
    <ns0:ErrorNum />
    <ns0:ErrorSeverity />
    <ns0:LineNumber />
    <ns0:Status />
    </ns0:ListOfTransactionDetailItems>
    <ns0:PONumber />
    <ns0:TransactionId />
    <ns0:TransactionMode />
    <ns0:TransactionNumber />
    <ns0:TransactionRowId />
    <ns0:TransactionStatus />
    <ns0:TransactionType />
    </ns0:TransactionHeader>
    </ns0:ListOfInboundResponse>
    </ns0:Submit_Input>
    

    XSLT Transform

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:urn="urn:partner.soap.sforce.com">
    <soapenv:Header>
    <urn:SessionHeader>
    <urn:sessionId>
    <xsl:value-of select="SID" />
    </urn:sessionId>
    </urn:SessionHeader>
    </soapenv:Header>
    <soapenv:Body>
    <xsl:copy-of select="." />
    </soapenv:Body>
    </soapenv:Envelope>
    </xsl:template>
    </xsl:stylesheet>
    

    Target XML

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:urn="urn:partner.soap.sforce.com">
    <soapenv:Header>
    <urn:SessionHeader>
    <urn:sessionId />
    </urn:SessionHeader>
    </soapenv:Header>
    <soapenv:Body>
    <ns0:Submit_Input xmlns:ns0="http://soap.sforce.com/schemas/class/test">
    <ns0:ListOfInboundResponse>
    <ns0:TransactionHeader>
    <ns0:ListOfTransactionDetailItems>
    <ns0:ErrorDesc>123</ns0:ErrorDesc>
    <ns0:ErrorNum />
    <ns0:ErrorSeverity />
    <ns0:LineNumber />
    <ns0:Status />
    </ns0:ListOfTransactionDetailItems>
    <ns0:PONumber />
    <ns0:SID>4567</ns0:SID>
    <ns0:TransactionId />
    <ns0:TransactionMode />
    <ns0:TransactionNumber />
    <ns0:TransactionRowId />
    <ns0:TransactionStatus />
    <ns0:TransactionType />
    </ns0:TransactionHeader>
    <ns0:TransactionHeader>
    <ns0:ListOfTransactionDetailItems>
    <ns0:ErrorDesc />
    <ns0:ErrorNum />
    <ns0:ErrorSeverity />
    <ns0:LineNumber />
    <ns0:Status />
    </ns0:ListOfTransactionDetailItems>
    <ns0:PONumber />
    <ns0:TransactionId />
    <ns0:TransactionMode />
    <ns0:TransactionNumber />
    <ns0:TransactionRowId />
    <ns0:TransactionStatus />
    <ns0:TransactionType />
    </ns0:TransactionHeader>
    </ns0:ListOfInboundResponse>
    </ns0:Submit_Input>
    </soapenv:Body>
    </soapenv:Envelope>
    

    Thanks Kiran

    (Edited to display the code snippets)

  • Chriztian Steinmeier 2732 posts 8341 karma points MVP 5x admin c-trib
    Dec 16, 2015 @ 13:32
    Chriztian Steinmeier
    100

    Hi Kiran,

    (Note: I've edited the question to show the code snippets, which were hidden - hope that's alright with everyone)

    Here's a transform XSLT that does the trick - it looks a bit scary because of the namespaces, when in reality, it's pretty straightforward, e.g.:

    • Output the soap Envelope header-stuff, handpicking the session ID from further down.
    • Process the document using the identity template which will automatically use the specific template for the SID element we don't want to output.

    EDIT: Looks like code snippets are broken again, sigh...

    I've posted a Gist with the code here

    Hope that helps,

    /Chriztian

  • kinra 2 posts 82 karma points
    Dec 16, 2015 @ 15:32
    kinra
    0

    Thank you Chriztian. That works !!!!

Please Sign in or register to post replies

Write your reply to:

Draft