轉(zhuǎn)帖|其它|編輯:郝浩|2008-07-15 14:10:28.000|閱讀 882 次
概述:利用XSLT將兩個XML文件合成一個XML文件
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
這個是論壇里的問題,但這個功能經(jīng)常遇到,貼出來供以后參考,這里的關(guān)鍵是XPATH的使用。
file1.xml
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="xslt1.xsl"?>
<staffs>
<staff order = "1">
<name>wang </name>
<age>27 </age>
</staff>
<staff order = "2">
<name>Li </name>
<age>24 </age>
</staff>
<staff order = "3">
<name>Q1 </name>
<age>29 </age>
</staff>
</staffs>
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="xslt1.xsl"?>
<staffs>
<staff order = "1">
<name>wang </name>
<age>27 </age>
</staff>
<staff order = "2">
<name>Li </name>
<age>24 </age>
</staff>
<staff order = "3">
<name>Q1 </name>
<age>29 </age>
</staff>
</staffs>
view plaincopy to clipboardprint?
file2.xml
file2.xmlview plaincopy to clipboardprint?
<PRE class=xml name="code"><?xml version="1.0" encoding="utf-8" ?>
<staffs>
<staff order = "1">
<name>wang </name>
<Money>100 </Money>
</staff>
<staff order = "2">
<name>Li </name>
<Money>200 </Money>
</staff>
<staff order = "3">
<name>Q1 </name>
<Money>400 </Money>
</staff>
</staffs>
</PRE>
<PRE class=xml name="code">xslt1.xsl</PRE>
<PRE class=xml name="code"><PRE class=xml name="code"><?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:template match="/">
<staffs>
<xsl:for-each select="/staffs/staff">
<xsl:variable name="CurrentOrder"><xsl:value-of select = "attribute::order" /></xsl:variable>
<staff>
<xsl:attribute name="order"><xsl:value-of select="@order" /></xsl:attribute>
<xsl:copy-of select="name" />
<xsl:copy-of select="age" />
<xsl:copy-of select="$file2/staffs/staff[@order=$CurrentOrder]/Money" />
</staff>
</xsl:for-each>
</staffs>
</xsl:template>
</xsl:stylesheet>
</PRE>
</PRE>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:CSDN