XSLT Transformation创建了一个soap错误

时间:2016-11-08 11:41:16

标签: xml xslt xslt-1.0

我需要创建一个xslt,它将响应消息转换为soap错误。 我尝试创建xslt但结果不如预期。在xslt转换后附加了输入,预期输出,xslt和Result。你能帮忙吗?

输入:

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

帮助感谢

1 个答案:

答案 0 :(得分:0)

<强>予。错误匹配:

以下代码错误:

<soap:Detail>
    <soap:Text  xml:lang="en"><xsl:apply-templates/></soap:Text>
</soap:Detail>

您的上下文节点是&#34; /&#34;,因此您的文档节点。您将处理整个input-xml(只要没有匹配的模板,它不会处理任何其他子项)。有了背景知识,xslt得到了一个默认的copy-text-to-output 技术,你会在元素中得到错误的内容。

建议更改:

<soap:Detail>
  <soap:Text  xml:lang="en"><xsl:apply-templates select="/response/message-body/errors/messages"/></soap:Text>
</soap:Detail>

<强> II。输出方法错误:

<xsl:output  method="text"/>

只会输出文本字符串。将方法更改为xml