我有一个要求,我试图从节点<payload>
中提取转义的XML字符。我遇到的挑战是如何在转义的XML消息中使用XML声明。
这是输入 (XML声明)
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:processInboundMessage xmlns:ns2="http://integration.nexuse2e.org/BackendDeliveryInterface/">
<choreographyId>InventoryActualUsage</choreographyId>
<businessPartnerId>Development</businessPartnerId>
<actionId>SendFile</actionId>
<conversationId>943fe0e5-2f88-48ce-ae3c-f7124ee498fd</conversationId>
<messageId>e9b3af1d-70fd-43c0-b467-df21fd903672</messageId>
<payload><?xml version="1.0" encoding="utf-8"?>
<test>somethinginformation</test>
<test1>someotherinfo</test1>
;</payload>
</ns2:processInboundMessage>
</soap:Body>
</soap:Envelope>
挑战:如果代码具有XML声明,则代码正在运行,但是可能存在不具有XML声明的情况
示例:没有XML声明
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:processInboundMessage xmlns:ns2="http://integration.nexuse2e.org/BackendDeliveryInterface/">
<choreographyId>InventoryActualUsage</choreographyId>
<businessPartnerId>Development</businessPartnerId>
<actionId>SendFile</actionId>
<conversationId>943fe0e5-2f88-48ce-ae3c-f7124ee498fd</conversationId>
<messageId>e9b3af1d-70fd-43c0-b467-df21fd903672</messageId>
<payload>
<test>somethinginformation</test>
<test1>someotherinfo</test1>
;</payload>
</ns2:processInboundMessage>
</soap:Body>
</soap:Envelope>
代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:date="http://exslt.org/dates-and-times" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes=" date">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>
<xsl:template match="payload">
<xsl:variable name="xmlencode" select="'<?xml version="1.0" encoding="utf-8"?>'"/>
<xsl:variable name="message">
<xsl:choose>
<xsl:when test="contains($xmlencode,'?>')">
<xsl:value-of select="substring-after(text(),'?>')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<xsl:value-of select="$message" disable-output-escaping="yes"/>
</soapenv:Body>
</soap:Envelope>
</xsl:template>
</xsl:stylesheet>
如果XML声明不可用,我遇到第二种情况的问题我没有得到任何东西。
得到这样的输出:
<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body/>
</soap:Envelope>
预期输出
<soap:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<test>somethinginformation</test>
<test1>someotherinfo</test1>
</soapenv:Body>
</soap:Envelope>