SOAP消息的XSL转换

时间:2015-09-21 13:02:53

标签: xml xslt soap

我有一条XML消息需要嵌入到SOAP消息中。基本上,我有一个XML消息作为输入。我需要使用XSL将其转换为SOAP消息。

以下是我的输入XML消息的样子:

<Transmission xmlns="http://xmlns.oracle.com/apps/otm">
    <TransmissionHeader>
        <Version>6.3.4</Version>
        <TransmissionCreateDt>
            <ExpressDate>20150916120546</ExpressDate>
            <TZId>Zulu</TZId>
            <TZOffset>+00:00</TZOffset>
        </TransmissionCreateDt>
        <TransactionCount>1</TransactionCount>
        <SenderTransmissionNo>355</SenderTransmissionNo>
        <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
        <ExpressXMLElementName>ExpressShipment</ExpressXMLElementName>
        <NotifyInfo>
            <ContactGid>
                <Gid>
                    <DomainName>Express</DomainName>
                    <Xid>Contact1</Xid>
                </Gid>
            </ContactGid>
            <ExternalSystemGid>
                <Gid>
                    <DomainName>Express</DomainName>
                    <Xid>External1</Xid>
                </Gid>
            </ExternalSystemGid>
        </NotifyInfo>
    </TransmissionHeader>
    <TransmissionBody>
        <GLogXMLElement>
            <PlannedShipment xmlns="http://xmlns.oracle.com/apps/otm">
                <Shipment>
                    <ShipmentHeader>
                        <ShipmentGid>
                            <Gid>
                                <DomainName>Express</DomainName>
                                <Xid>28</Xid>
                            </Gid>
                        </ShipmentGid>
                    </ShipmentHeader>
                </Shipment>
            </PlannedShipment>
        </GLogXMLElement>
    </TransmissionBody>
</Transmission>

这需要转变为:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header/>
    <env:Body>
        <SubmitXml xmlns="http://interface.bwlog.com/">
            <message>
                <Transmission xmlns="http://xmlns.oracle.com/apps/otm">
                    <TransmissionHeader>
                        <Version>6.3.4</Version>
                        <TransmissionCreateDt>
                            <ExpressDate>20150916120546</ExpressDate>
                            <TZId>Zulu</TZId>
                            <TZOffset>+00:00</TZOffset>
                        </TransmissionCreateDt>
                        <TransactionCount>1</TransactionCount>
                        <SenderTransmissionNo>355</SenderTransmissionNo>
                        <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
                        <ExpressXMLElementName>ExpressShipment</ExpressXMLElementName>
                        <NotifyInfo>
                            <ContactGid>
                                <Gid>
                                    <DomainName>Express</DomainName>
                                    <Xid>Contact1</Xid>
                                </Gid>
                            </ContactGid>
                            <ExternalSystemGid>
                                <Gid>
                                    <DomainName>Express</DomainName>
                                    <Xid>External1</Xid>
                                </Gid>
                            </ExternalSystemGid>
                        </NotifyInfo>
                    </TransmissionHeader>
                    <TransmissionBody>
                        <GLogXMLElement>
                            <PlannedShipment xmlns="http://xmlns.oracle.com/apps/otm">
                                <Shipment>
                                    <ShipmentHeader>
                                        <ShipmentGid>
                                            <Gid>
                                                <DomainName>Express</DomainName>
                                                <Xid>28</Xid>
                                            </Gid>
                                        </ShipmentGid>
                                    </ShipmentHeader>
                                </Shipment>
                            </PlannedShipment>
                        </GLogXMLElement>
                    </TransmissionBody>
                </Transmission>
            </message>
            <referenceId>Express.28</referenceId>
        </SubmitXml>
    </env:Body>
</env:Envelope>

我已经像这样构建了我的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="no" indent="yes" method="xml"/>
    <xsl:template match="/">
        <SubmitXml xmlns="http://interface.bwlog.com/">
            <message>
                <xsl:copy-of select="/Transmission" />
            </message>
            <reference> 
                <xsl:value-of select="/Transmission/TransmissionBody/GLogXMLElement/PlannedShipment/Shipment/ShipmentHeader/ShipmentGid/Gid/DomainName" />
            </reference>
        </SubmitXml>
    </xsl:template> 

</xsl:stylesheet>

但我得到的输出是:

<?xml version="1.0"?>
<SubmitXml xmlns="http://interface.bwlog.com/">
  <message/>
  <reference/>
</SubmitXml>

显然我在XSL上遗漏了一些东西。如果你能指出它会很高兴。

1 个答案:

答案 0 :(得分:0)

输入XML中的节点位于名称空间中。您需要在样式表中声明此命名空间,为其分配前缀,并在寻址节点时使用此前缀。

以下是对现有样式表的更正:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:otm="http://xmlns.oracle.com/apps/otm">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <SubmitXml xmlns="http://interface.bwlog.com/">
        <message>
            <xsl:copy-of select="otm:Transmission" />
        </message>
        <reference> 
            <xsl:value-of select="otm:Transmission/otm:TransmissionBody/otm:GLogXMLElement/otm:PlannedShipment/otm:Shipment/otm:ShipmentHeader/otm:ShipmentGid/otm:Gid/otm:DomainName" />
        </reference>
    </SubmitXml>
</xsl:template> 

</xsl:stylesheet>

结果并不完全是您发布的结果,但这是另一个问题。