利用XSLT将两个XML文件合成一个XML文件

这个是论坛里的问题,但这个功能经常遇到,贴出来供以后参考,这里的关键是XPATH的使用。

file1.xml

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3. <?xml-stylesheet type="text/xsl" href="xslt1.xsl"?>  
  4.   
  5. <staffs>  
  6.   
  7.   <staff order = "1">  
  8.   
  9.     <name>wang </name>  
  10.   
  11.     <age>27 </age>  
  12.   
  13.   </staff>  
  14.   
  15.   <staff order = "2">  
  16.   
  17.     <name>Li </name>  
  18.   
  19.     <age>24 </age>  
  20.   
  21.   </staff>  
  22.   
  23.   <staff order = "3">  
  24.   
  25.     <name>Q1 </name>  
  26.   
  27.     <age>29 </age>  
  28.   
  29.   </staff>  
  30.   
  31. </staffs>  
  1. <PRE class=xml name="code"><?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3. <staffs>  
  4.   
  5.   <staff order = "1">  
  6.   
  7.     <name>wang </name>  
  8.   
  9.     <Money>100 </Money>  
  10.   
  11.   </staff>  
  12.   
  13.   <staff order = "2">  
  14.   
  15.     <name>Li </name>  
  16.   
  17.     <Money>200 </Money>  
  18.   
  19.   </staff>  
  20.   
  21.   <staff order = "3">  
  22.   
  23.     <name>Q1 </name>  
  24.   
  25.     <Money>400 </Money>  
  26.   
  27.   </staff>  
  28.   
  29. </staffs>  
  30. </PRE>  
  31. <PRE class=xml name="code">xslt1.xsl</PRE>  
  32. <PRE class=xml name="code"><PRE class=xml name="code"><?xml version="1.0" encoding="utf-8" ?>  
  33.   
  34. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  35.   
  36. <xsl:output  method="xml" omit-xml-declaration="yes"/>  
  37.   
  38. <xsl:variable name="file2" select="document('file2.xml')"/>  
  39.   
  40. <xsl:template match="/">  
  41.   
  42.     <staffs>  
  43.   
  44.         <xsl:for-each select="/staffs/staff">  
  45.   
  46.         <xsl:variable name="CurrentOrder"><xsl:value-of select = "attribute::order" /></xsl:variable>  
  47.   
  48.             <staff>  
  49.   
  50.                 <xsl:attribute name="order"><xsl:value-of select="@order" /></xsl:attribute>  
  51.   
  52.                 <xsl:copy-of select="name" />  
  53.   
  54.                 <xsl:copy-of select="age" />  
  55.   
  56.                 <xsl:copy-of select="$file2/staffs/staff[@order=$CurrentOrder]/Money" />  
  57.   
  58.             </staff>  
  59.   
  60.         </xsl:for-each>  
  61.   
  62.     </staffs>  
  63.   
  64. </xsl:template>  
  65.   
  66. </xsl:stylesheet>  
  67.   
  68. </PRE>  
  69.   
  70. </PRE>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值