XSL处理WebService报文

来源:互联网 发布:深圳知豆电动汽车 编辑:程序博客网 时间:2024/06/05 15:37
  • XML

<?xml version="1.0" encoding="UTF-8"?>

//IE中直接查看XSL样式
<?xml-stylesheet type="text/xsl" href="C:/mytest/myaxis/WebContent/comments.xsl"?>

//webservice包体
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>

//自定义默认命名空间http://www.com
  <getCommentsResponse xmlns="http://www.com">

//结果为对象数组
   <getCommentsReturn>
    <anonymity>true</anonymity>
    <commentId>1</commentId>
    <commenter>&#x738B;&#x4E00;</commenter>

//HTML标签
    <content>&#x533F;&#x540D;&#x8BC4;&#x8BBA;:)good&#xFF01;&lt;img src='1.jpg' /&gt;</content>
    <contentId>100</contentId>
   </getCommentsReturn>
   <getCommentsReturn>
    <anonymity>false</anonymity>
    <commentId>2</commentId>
    <commenter>&#x5F20;&#x4E09;</commenter>
    <content>&#x7F72;&#x540D;&#x8BC4;&#x8BBA;:)good&#xFF01;&lt;img src='2.jpg' /&gt;</content>
    <contentId>100</contentId>
   </getCommentsReturn>
  </getCommentsResponse>
 </soapenv:Body>
</soapenv:Envelope>

  • XSL

<?xml version="1.0" encoding="UTF-8"?>

//定义所以命名空间
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.com">

//匹配包体
 <xsl:template match="/soapenv:Envelope/soapenv:Body" >
  <table border="1px #000000" >
   <thead>
    <tr>
     <td>评论序号</td>
     <td>评论者</td>
     <td>内容</td>
    </tr>
   </thead>
   <tbody>

//查找自定义默认命名空间http://www.com

//多条数据循环处理
    <xsl:for-each select="p:getCommentsResponse/p:getCommentsReturn" >
    <tr>

//取值显示
     <td><xsl:value-of select="p:commentId"/></td>
     <td>

//条件判断
     <xsl:if test="p:anonymity = 'false' ">
      <xsl:value-of select="p:commenter"/>
     </xsl:if>
     </td>

//HTML标签不做转化
     <td><xsl:value-of select="p:content"  disable-output-escaping="yes"/></td>
    </tr>
    </xsl:for-each>
   </tbody>
   <tfoot>
    <tr>
     <td align='left' colspan='3' >

//CDATA中数据不做转化
      上一页<xsl:text  disable-output-escaping="yes"><![CDATA[&nbsp;&nbsp;]]></xsl:text>下一页
     </td>
    </tr>
   </tfoot>
  </table>
 </xsl:template>
</xsl:stylesheet>

  • XML调试工具

AltovaXMLspy

 

 

原创粉丝点击