以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML基础 』  (http://bbs.xml.org.cn/list.asp?boardid=1)
----  Qr可以再解答一下xml合并问题么?[求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=60800)


--  作者:fangel2000
--  发布时间:4/2/2008 5:10:00 PM

--  Qr可以再解答一下xml合并问题么?[求助]
第一个xml文档:1.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<?xml:stylesheet type="text/xsl" href="3.xsl"?>
<rss version="2.0">
    <channel>
        <item>
            <title> Nikon Coolpix S600 </title>
            <category> Digital Cameras </category>
        </item>

        <item>
            <title> Griffin TuneBuds </title>
            <category> Headphones </category>
        </item>
    </channel>
</rss>

第二个XML文档:2.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0">
    <channel>
         <item>
            <title> Lawn Aerator Sandals </title>
            <category> Garden </category>
        </item>

        <item>
            <title> Belkin Laptop Hideaway </title>
            <category> Notebook Accessories </category>
        </item>
    </channel>
</rss>

xsl文件:3.xsl
<?xml version="1.0" encoding="gb2312"?>
<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('2.xml')//item" />

<xsl:template match="/">
 <html>
  <head></head>
  <body>
   <table border="1">
    <xsl:apply-templates select="//item" />
   </table>
  </body>
 </html>
</xsl:template>


<xsl:template match="item">
 <tr>
  <td><xsl:value-of select="title" /></td>
  <td><xsl:value-of select="category" /></td>
 </tr>
 <tr bgcolor="#FF0000">
  <td><xsl:value-of select="$temp/title" /></td>      
  <td><xsl:value-of select="$temp/category" /></td>
 </tr>
</xsl:template>
</xsl:stylesheet>

为什么合并以后出现的结果是这个????
Nikon Coolpix S600          Digital Cameras
Lawn Aerator Sandals       Garden    //第二行和第四行重复
Griffin TuneBuds              Headphones
Lawn Aerator Sandals      Garden   
//原本这个第四行应该为:Belkin Laptop Hideaway         Notebook Accessories

ps:还有知道怎么样做才能让第二个XML文档的内容出现在第一个文档的后面,而不是像我这样的交替显示的么?


--  作者:Qr
--  发布时间:4/2/2008 6:38:00 PM

--  
<xsl:apply-templates select="//item" />处理的只是1.XML的节点,因其有两条记录,因此2.xml中的第一条记录输出两次,原因是你把$temp的处理写到<xsl:template match="item">
这个模板中了。应该把$temp的处理作为一个独立的模板来写代码。
--  作者:Qr
--  发布时间:4/2/2008 6:51:00 PM

--  
<?xml version="1.0" encoding="gb2312"?>
<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('2.xml')//item" />

<xsl:template match="/">
<html>
  <head></head>
  <body>
   <table border="1">
    <xsl:apply-templates select="//item" />
    <xsl:call-template name="temp"/><!--或者干脆直接用for-each-->
   </table>
  </body>
</html>
</xsl:template>


<xsl:template match="item">
<tr>
  <td><xsl:value-of select="title" /></td>
  <td><xsl:value-of select="category" /></td>
</tr>
</xsl:template>
<xsl:template name="temp">
<xsl:for-each select="$temp">
<tr bgcolor="#FF0000">
  <td><xsl:value-of select="title" /></td>      
  <td><xsl:value-of select="category" /></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


--  作者:fangel2000
--  发布时间:4/2/2008 7:11:00 PM

--  
非常感谢Qr的帮助!
--  作者:Qr
--  发布时间:4/2/2008 7:13:00 PM

--  

--  作者:fangel2000
--  发布时间:4/2/2008 10:45:00 PM

--  
请教Qr最后一个问题啊
如果我要合并10个XML文档呢?1.xml 2.xml   .... 10.xml
我是不是要像这样
“<xsl:variable name="temp" select="document('2.xml')//item" />”
定义10个document()这样的变量啊?
有没有更好的方法?
--  作者:Qr
--  发布时间:4/3/2008 9:13:00 AM

--  
document()10次或更多实在是麻烦,可以使用<xsl:call-template>模板递归来定义,实现循环的功能,动态document()不同的XML。我的BLOG上也有模板递归来实现循环计数的相关示例。
另外,不用XSLT而改用SAX来处理也是不错的选择,更适合大文件。DOM也可以,但只适合小文件。
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
109.375ms