XML 在标记之前使用 XSLT 数据进行转换

XML transformation with XSLT data before tags

我正在尝试对负载应用转换,但结果有一些松散的值,我做错了什么?

起初我对原始命名空间有疑问,并且做了一些研究发现我必须声明它,但现在发生了这种情况,我不明白。 我去匹配 pbc:operaciones 以便我可以处理来自该数组的数据,为什么它显示其他值?

XML 输入:

<?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>

结果:

<?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>
        
    

谢谢!

why does it show the other values?

因为您的样式表只有一个模板匹配 operaciones。其他节点 - 即 estadonumeroOperaciones - 由 built-in template rules 处理,将它们的文本值复制到输出。

尝试:

<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>

或者(最好是恕我直言):

<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>