简体   繁体   中英

XML transformation with XSLT data before tags

I'm trying to apply a transformation to a payload but the result has some loose values, what am I doing wrong?

At first I had problemas with the original namespace and doing some research found out I had to declare it, but now this happens and I don't understand it. I go matching pbc:operaciones so I can process data from that array, why does it show the other values?

XML input:

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <PbcWsGetOperacionesResponse xmlns="http://ddpbc.cbc.es">
            <estado>
                <estado>OK</estado>
            </estado>
            <numeroOperaciones>67</numeroOperaciones>
            <operaciones>
                <operacion>
                    <codigoOperacion>E007O000121</codigoOperacion>
                    <codigoPromocion>E007P0007</codigoPromocion>
                    <nombrePromocion>101 - Altos de Los Fresnos</nombrePromocion>
                    <codigoCliente/>
                    <identificacionCliente>10011242M</identificacionCliente>
                    <cliente>JUAN LOPEZ</cliente>
                    <inmueble/>
                    <estado>2</estado>
                    <conclusion>5</conclusion>
                    <fechaConclusion>2020-08-27T11:39:51+02:00</fechaConclusion>
                    <numeroIntervinientes>1</numeroIntervinientes>
                </operacion>
                <operacion>
                    <codigoOperacion>E007O000122</codigoOperacion>
                    <codigoPromocion>E007P0007</codigoPromocion>
                    <nombrePromocion>101 - Altos de Los Fresnos</nombrePromocion>
                    <codigoCliente/>
                    <identificacionCliente>10011242M</identificacionCliente>
                    <cliente>JUAN LOPEZ</cliente>
                    <inmueble/>
                    <estado>2</estado>
                    <conclusion>5</conclusion>
                    <fechaConclusion>2020-08-27T11:40:02+02:00</fechaConclusion>
                    <numeroIntervinientes>1</numeroIntervinientes>
                </operacion>
            </operaciones>
        </PbcWsGetOperacionesResponse>
    </soap:Body>
</soap:Envelope>

XSLT

<xsl:stylesheet version="1.0" xmlns:pbc="http://ddpbc.cbc.es" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://ws.wso2.org/dataservice" xmlns:xslFormatting="urn:xslFormatting">
    <xsl:template match="pbc:operaciones">
        <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
            <soap:Body>
                <urn:ZacCreaExpedientesFromWs>
                    <ItExpeout>
                        <xsl:for-each select="pbc:operacion">
                            <item>
                                <Expedien>
                                    <xsl:value-of select="pbc:codigoOperacion"/>
                                </Expedien>
                                <Status>
                                    <xsl:value-of select="pbc:conclusion"/>
                                </Status>
                                <Fexpedient>
                                    <xsl:value-of select="pbc:fechaConclusion"/>
                                </Fexpedient>
                            </item>
                        </xsl:for-each>
                    </ItExpeout>
                </urn:ZacCreaExpedientesFromWs>
            </soap:Body>
        </soap:Envelope>
    </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="UTF-8"?>
    
        
            
                OK
            
            67
            <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style" xmlns:pbc="http://ddpbc.cbc.es" xmlns:b="http://ws.wso2.org/dataservice" xmlns:xslFormatting="urn:xslFormatting"><soap:Body><urn:ZacCreaExpedientesFromWs><ItExpeout><item><Expedien>E007O000121</Expedien><Status>5</Status><Fexpedient>2020-08-27T11:39:51+02:00</Fexpedient></item><item><Expedien>E007O000122</Expedien><Status>5</Status><Fexpedient>2020-08-27T11:40:02+02:00</Fexpedient></item></ItExpeout></urn:ZacCreaExpedientesFromWs></soap:Body></soap:Envelope>
        
    

Thanks!

why does it show the other values?

Because your stylesheet has only a single template matching operaciones . Other nodes - namely estado and numeroOperaciones - are handled by the built-in template rules that copy their text values to the output.

Try:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:pbc="http://ddpbc.cbc.es"
exclude-result-prefixes="pbc">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
        <soap:Body>
            <urn:ZacCreaExpedientesFromWs>
                <ItExpeout>
                    <xsl:for-each select="//pbc:operacion">
                        <item>
                            <Expedien>
                                <xsl:value-of select="pbc:codigoOperacion"/>
                            </Expedien>
                            <Status>
                                <xsl:value-of select="pbc:conclusion"/>
                            </Status>
                            <Fexpedient>
                                <xsl:value-of select="pbc:fechaConclusion"/>
                            </Fexpedient>
                        </item>
                    </xsl:for-each>
                </ItExpeout>
            </urn:ZacCreaExpedientesFromWs>
        </soap:Body>
    </soap:Envelope>
</xsl:template>
    
</xsl:stylesheet>

Or (preferably, IMHO):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap0="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:pbc="http://ddpbc.cbc.es"
exclude-result-prefixes="soap0 pbc">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/soap0:Envelope">
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
        <soap:Body>
            <urn:ZacCreaExpedientesFromWs>
                <ItExpeout>
                    <xsl:for-each select="soap0:Body/pbc:PbcWsGetOperacionesResponse/pbc:operaciones/pbc:operacion">
                        <item>
                            <Expedien>
                                <xsl:value-of select="pbc:codigoOperacion"/>
                            </Expedien>
                            <Status>
                                <xsl:value-of select="pbc:conclusion"/>
                            </Status>
                            <Fexpedient>
                                <xsl:value-of select="pbc:fechaConclusion"/>
                            </Fexpedient>
                        </item>
                    </xsl:for-each>
                </ItExpeout>
            </urn:ZacCreaExpedientesFromWs>
        </soap:Body>
    </soap:Envelope>
</xsl:template>
    
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM