2016-03-07 61 views
0

我正在尝试编写xslt来修改xml。这是由同步呼叫接收的响应。这个XML的结构需要被修改,并且数据需要被传输,这样响应可以被原始系统消耗。无法使用Xslt获取提取元素

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

您遇到的第一个问题是您的名称空间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开头,这是不需要的,因为你的模板已经与根元素相匹配,所以xpath表达式与此相关。

试试这个XSLT

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

喜添,非常感谢您!!感谢你的帮助!!有效! –