无法使用Xslt获取Extract元素

时间:2016-03-07 18:33:22

标签: xml web-services xslt soap

我正在尝试编写xslt来修改xml。这是同步调用接收的响应。需要修改此xml的结构,并且需要传输数据,以便原始系统可以使用响应。

输入XML如下:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
   <SOAP-ENV:Body>
      <ns0:ValidateMeterItemResponse xmlns:ns0="http://www.apsc.com/CCB/ValidateMeterItem/InOut">
         <ns1:ValidateMeterItemRes xmlns:ns1="http://www.apsc.com/CCB/MeterServices/InOut">
            <ns1:verificationStatus>M2IV</ns1:verificationStatus>
            <ns1:errorCode>256.0</ns1:errorCode>
            <ns1:errorText>Service Point ID 1245765566 field invalid</ns1:errorText>
            <ns1:readingDetails/>
         </ns1:ValidateMeterItemRes>
      </ns0:ValidateMeterItemResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

预期输出XML是:

<responseMessage/">
    <response>
        <errorCode>256.0</errorCode>
    </response>
</responseMessage>

我现在只想提取一个节点。

下面是我写的XSLT。我首先按原样复制了所有元素,然后尝试取消<ValidateMeterItemResponse>节点。然后我尝试使用xsl value of从输入xml中选择某些元素来构建我的xml(具有不同的结构)。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://xmlns.oracle.com/OUMWM/Message"
xmlns:ns1="http://xmlns.oracle.com/OUMWM/Message1">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*/*/*">
        <xsl:apply-templates select="node()" />
    </xsl:template>
       <xsl:strip-space elements="*"/>
    <xsl:template match="/*">
        <responseMessage>
            <response>
                <errorCode>
                    <xsl:value-of select="SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:ValidateMeterItemRes/ns1:errorCode" />
                </errorCode>
            </response>
        </responseMessage>
    </xsl:template>
</xsl:stylesheet>

即使所有路径都正确,我也无法提取元素。我确信我已经错过了一些尝试完成此事的东西。我哪里出错了?

1 个答案:

答案 0 :(得分:1)

您遇到的第一个问题是您的命名空间URI在XML和XSLT之间有所不同。在XML中,您已经定义了这些..

xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"       
xmlns:ns0="http://www.apsc.com/CCB/ValidateMeterItem/InOut"
xmlns:ns1="http://www.apsc.com/CCB/MeterServices/InOut"

但是在XSLT中你有这些......

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns0="http://xmlns.oracle.com/OUMWM/Message"
xmlns:ns1="http://xmlns.oracle.com/OUMWM/Message1"

必须匹配的名称空间URI,而不是前缀。

另一个问题是,在最终模板的xpath表达式中,您错过了ns0:ValidateMeterItemResponse。 xpath表达式也以SOAP-ENV:Envelope开头,因为模板已经与根元素匹配,所以不需要<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:ns0="http://www.apsc.com/CCB/ValidateMeterItem/InOut" xmlns:ns1="http://www.apsc.com/CCB/MeterServices/InOut"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> <xsl:template match="/*"> <responseMessage> <response> <errorCode> <xsl:value-of select="SOAP-ENV:Body/ns0:ValidateMeterItemResponse/ns1:ValidateMeterItemRes/ns1:errorCode" /> </errorCode> </response> </responseMessage> </xsl:template> </xsl:stylesheet> ,因此xpath表达式与之相关。

试试这个XSLT

request.META
相关问题