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