簡體   English   中英

使用 XSLT 在 XML 下轉換

[英]Transform below XML using XSLT

你能幫我用 XSLT 代碼來轉換下面的 XML -

輸入 XML:

 <?xml version="1.0" encoding="UTF-8"?> <ns0:PrintCertificateByContractNumber xmlns:ns0="http://tempuri.org/"> <ns0:ContractNumber>300001111</ns0:ContractNumber> <ns0:Credentials> <ns1:Password xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">uuuuuuuuuuuuuu</ns1:Password> <ns1:ResponseStatusList xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"> <ns2:ResponseStatus xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities"> <ns2:ErrorMessage/> <ns2:ResponseType>Error</ns2:ResponseType> </ns2:ResponseStatus> </ns1:ResponseStatusList> <ns1:UserName xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">xxxxxxxxxxxxxxxx</ns1:UserName> <ns1:UserReferenceNumber xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"/> </ns0:Credentials> </ns0:PrintCertificateByContractNumber>

輸出所需的 XML:

 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities"> <soap:Header/> <soap:Body> <tem:PrintCertificateByContractNumber> <tem:ContractNumber>?</tem:ContractNumber> <tem:Credentials> <ep4:Password>uuuuuuuu</ep4:Password> <ep4:ResponseStatusList> <ep41:ResponseStatus> <ep41:ErrorMessage></ep41:ErrorMessage> <ep41:ResponseType>Error</ep41:ResponseType> </ep41:ResponseStatus> </ep4:ResponseStatusList> <ep4:UserName>xxxxxxxx</ep4:UserName> <ep4:UserReferenceNumber></ep4:UserReferenceNumber> </tem:Credentials> </tem:PrintCertificateByContractNumber> </soap:Body> </soap:Envelope>

我正在使用的 XSLT 代碼:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" exclude-result-prefixes="ns0 ns1 ns2" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ns0="http://tempuri.org/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output version="1.0" encoding="UTF-8" method="xml"/> <xsl:template match="/"> <soapenv:Envelope xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <tem:PrintCertificateByContractNumber> <tem:ContractNumber> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:ContractNumber"/> </tem:ContractNumber> <tem:Credentials> <ep4:Password> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:Password"/> </ep4:Password> <ep4:ResponseStatusList> <ep41:ResponseStatus> <ep41:ErrorMessage> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ErrorMessage"/> </ep41:ErrorMessage> <ep41:ResponseType> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ResponseType"/> </ep41:ResponseType> </ep41:ResponseStatus> </ep4:ResponseStatusList> <ep4:UserName> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserName"/> </ep4:UserName> <ep4:UserReferenceNumber> <xsl:value-of select="ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserReferenceNumber"/> </ep4:UserReferenceNumber> </tem:Credentials> </tem:PrintCertificateByContractNumber> </soapenv:Body> </soapenv:Envelope> </xsl:template> </xsl:stylesheet>

我得到的輸出:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <tem:PrintCertificateByContractNumber xmlns:tem="http://tempuri.org/"> <tem:ContractNumber/> <tem:Credentials> <ep4:Password xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" /> <ep4:ResponseStatusList xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"> <ep41:ResponseStatus xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities"> <ep41:ErrorMessage/> <ep41:ResponseType/> </ep41:ResponseStatus> </ep4:ResponseStatusList> <ep4:UserName xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" /> <ep4:UserReferenceNumber xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" /> </tem:Credentials> </tem:PrintCertificateByContractNumber> </soapenv:Body> </soapenv:Envelope>

無法從我正在使用的映射中從“密碼”等中刪除命名空間。 您能否讓我知道如何從最終輸出 XML 中刪除命名空間。

此外,非常感謝任何人可以告訴我一個通用代碼來實現這一點,這樣我就可以刪除 XLST 而無需為每個節點復制數據。

AFAICT,您獲得的輸出在語義上與您想要的輸出相同。 命名空間聲明的放置完全無關緊要,您幾乎無法控制它,至少在 XSLT 1.0 中是這樣。

對於接收應用程序來說應該無關緊要 - 如果確實如此,則問題應該在那里解決。 您可以嘗試通過制作標題來強制命名空間聲明到根元素:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://tempuri.org/" 
xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" 
xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/"
xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" 
xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" 
exclude-result-prefixes="ns0 ns1 ns2" >

然后改變:

<soapenv:Envelope xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

到:

<soapenv:Envelope>
    <xsl:copy-of select="document('')/xsl:stylesheet/namespace::tem | document('')/xsl:stylesheet/namespace::ep4 | document('')/xsl:stylesheet/namespace::ep41"/>

這可能有效也可能無效,這取決於您的 XSLT 處理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM