我需要创建一个xslt,它将响应消息转换为soap错误。我尝试创建xslt,但结果不像预期的那样。附输入、预期输出、xslt和xslt转换后的结果。你能帮帮我吗?
输入: <response>
<metadata>
</metadata>
<message-body>
<errors>
<transaction-id>12345</transaction-id>
<claim-id>124545454</claim-id>
<suffix-id>545454</suffix-id>
<messages>
<message-id>123</message-id>
<message-type>E</message-type>
<message-description>Claim not found</message-description>
</messages>
<messages>
<message-id>256</message-id>
<message-type>M</message-type>
<message-description>Username not valid</message-description>
</messages>
</errors>
</message-body>
</response>
预期输出: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header />
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Sender</soap:Value>
</soap:Code>
<soap:Detail>
<soap:Text xml:lang="en">[E]100000:UserId Not Found;[M]100001:Source System Not Found</soap:Text>
</soap:Detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
XSLT: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header />
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Sender</soap:Value>
</soap:Code>
<soap:Detail>
<soap:Text xml:lang="en"><xsl:apply-templates/></soap:Text>
</soap:Detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
</xsl:template>
<xsl:template match="messages[position() > 1]">
<xsl:text>,</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="messages/*">
<xsl:value-of select="substring(';', 1, position() > 1)"/>
<xsl:value-of select="substring('[', 1, name()='message-id')"/>
<xsl:value-of select="."/>
<xsl:value-of select="substring(']', 1, name()='message-id')"/>
</xsl:template>
</xsl:stylesheet>
结果:<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header/>
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Sender</soap:Value>
</soap:Code>
<soap:Detail>
<soap:Text xml:lang="en">12345124545454545454[123];E;Claim not found,[256];M;Username not valid</soap:Text>
</soap:Detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
帮助欣赏
错误的匹配:
下面的代码是错误的:
<soap:Detail>
<soap:Text xml:lang="en"><xsl:apply-templates/></soap:Text>
</soap:Detail>
你的上下文节点是"/",所以你的文档节点。您将处理整个input-xml(只要没有匹配的模板,它就不会处理任何后续的子节点)。有了这些背景知识,xslt获得了默认的复制文本到输出技术,您可以将错误的内容放入元素中。
建议改变:
<soap:Detail>
<soap:Text xml:lang="en"><xsl:apply-templates select="/response/message-body/errors/messages"/></soap:Text>
</soap:Detail>
二世。错误的输出方法:
<xsl:output method="text"/>
将只输出文本字符串。将方法改为xml