用xslt合并两个xml文件

link.xml 文件 <?xml version="1.0" encoding="utf-8"?>
<root>
  <stu>
    <sno>001</sno>
    <sname>张三</sname>
    <ssex>男</ssex>
    <sadd>上海</sadd>
    <sid>1</sid>

  </stu>
  <stu>
    <sno>002</sno>
    <sname>李四</sname>
    <ssex>男</ssex>
    <sadd>湖南</sadd>
    <sid>2</sid>
  </stu>
</root> 

link2.xml 文件  <?xml version="1.0" encoding="utf-8"?>
<root>
  <info>
    <sid>1</sid>
    <sexamid>2008002</sexamid>
  </info>
  <info>
    <sid>2</sid>
    <sexamid>2008001</sexamid>
  </info>
  <info>
    <sid>3</sid>
    <sexamid>2008003</sexamid>
  </info>
  <info>
    <sid>4</sid>
    <sexamid>2008004</sexamid>
  </info>
</root>

link.xsl 文件<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method ="xml" version ="1.0" encoding ="gb2312" indent="yes"/>
  <xsl:variable name ="temp" select ="document('link2.xml')//info"/>
  <xsl:template match="/">
    <xsl:call-template name ="stu"/>
  </xsl:template>

  <xsl:template name ="stu">
    <root>
      <xsl:for-each select="//stu">
        <xsl:variable name ="id" select ="sid"/>
        <stu>
          <xsl:copy-of  select ="./*"/>
          <xsl:copy-of select="$temp[sid=$id]/sexamid"/>
        </stu>
      </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet>

结合后的结果是:

<?xml version="1.0" encoding="gb2312"?>
<root>
  <stu>
    <sno>001</sno>
    <sname>张三</sname>
    <ssex>男</ssex>
    <sadd>上海</sadd>
    <sid>1</sid>
    <sexamid>2008002</sexamid>
  </stu>
  <stu>
    <sno>002</sno>
    <sname>李四</sname>
    <ssex>男</ssex>
    <sadd>湖南</sadd>
    <sid>2</sid>
    <sexamid>2008001</sexamid>
  </stu>
</root>

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页