以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  紧急救助xslt和xpath处理两个xml文档的问题,救命啊  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=84946)


--  作者:abc911
--  发布时间:5/27/2010 4:20:00 PM

--  紧急救助xslt和xpath处理两个xml文档的问题,救命啊
用xslt和xpath处理fc.xml和footyresult.xml, 要求:
在fc.xml里面找出各个<team>里面的<name>和<headCoach>然后在footyresult.xml里面的/season/round路径下找出 math里面的team的name对应fc.xml里面的team name. 输出:team的分数score里的<goal>和<behind>

文件如下:
[color=#FF0000]fc.xml:[/color]

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="q4b.xsl"?>
<footballClub xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FC.xsd">
 <team>
  <name>Brisbane Lions</name>
  <address>Brisbane</address>
  <captain>S.Black</captain>
  <headCoach>L.Matthews</headCoach>
 </team>
 <team>
  <name>Carlton Blues</name>
  <address>Carlton</address>
  <captain>N.Stevens</captain>
  <headCoach>D.Pagan</headCoach>
 </team>
 <team>
  <name>Collingwood Magpies</name>
  <address>Collingwood</address>
  <captain>N.Buckley</captain>
  <headCoach>M.Malthouse</headCoach>
 </team>
 <team>
  <name>Essendon Bombers</name>
  <address>Essendon</address>
  <captain>M.Loyd</captain>
  <headCoach>K.Sheedy</headCoach>
 </team>
 <team>
  <name>Fremantle Dockers</name>
  <address>Fremantle</address>
  <captain>M.Pavlich</captain>
  <headCoach>C.Connolly</headCoach>
 </team>
 <team>
  <name>Geelong Cats</name>
  <address>Geelong</address>
  <captain>T.Harley</captain>
  <headCoach>M.Thompson</headCoach>
 </team>
 <team>
  <name>Hawthorn Hawks</name>
  <address>Hawthorn</address>
  <captain>S.Mitchell</captain>
  <headCoach>A.Clarkson</headCoach>
 </team>
 <team>
  <name>Melbourne Demons</name>
  <address>Melbourne</address>
  <captain>D.Neitz</captain>
  <headCoach>N.Danniher</headCoach>
 </team>
 <team>
  <name>North Melbourne Kangaroos</name>
  <address>North Melbourne</address>
  <captain>A.Simpson</captain>
  <headCoach>D.Laidley</headCoach>
 </team>
 <team>
  <name>Port Adelaide Powers</name>
  <address>Port Adelaide</address>
  <captain>W.Treadrea</captain>
  <headCoach>M.Williams</headCoach>
 </team>
 <team>
  <name>Richmond Tigers</name>
  <address>Richmond</address>
  <captain>K.Jonhson</captain>
  <headCoach>T.Wallace</headCoach>
 </team>
 <team>
  <name>Saint Kilda Saints</name>
  <address>Saint Kilda</address>
  <captain>N.Riewoldt</captain>
  <captain>L.Ball</captain>
  <headCoach>G.Thomas</headCoach>
 </team>
 <team>
  <name>Sydney Swans</name>
  <address>Sydney</address>
  <captain>B.Kirk</captain>
  <captain>L.Barry</captain>
  <captain>B.Hall</captain>
  <headCoach>P.Roos</headCoach>
 </team>
 <team>
  <name>West Coast Eagles</name>
  <address>Perth</address>
  <captain>C.Judd</captain>
  <headCoach>J.Worsfold</headCoach>
 </team>
 <team>
  <name>Western Bulldogs</name>
  <address>Footscray</address>
  <captain>B.Johnson</captain>
  <headCoach>R.Eade</headCoach>
 </team>
 <team>
  <name>Adelaide Crows</name>
  <address>Adelaide</address>
  <captain>S.Goodwin</captain>
  <headCoach>N.Craig</headCoach>
 </team>
</footballClub>

[/QUOTE]

[color=#FF0000]footyresult.xml:[/color]
[QUOTE]
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="q2a.xsl"?>
<season year="2007" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="footyResults.xsd">
 <round no="1">
  <match gameNo="1">
   <team home="yes">
    <name>Melbourne Demons</name>
    <score>
     <goal>9</goal>
     <behind>8</behind>
    </score>
    <highestGoalKicker>
     <name>D.Neitz</name>
     <goal>3</goal>
    </highestGoalKicker>
    <highestGoalKicker>
     <name>R.Robertson</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Saint Kilda Saints</name>
    <score>
     <goal>13</goal>
     <behind>15</behind>
    </score>
    <highestGoalKicker>
     <name>F.?Gehrig</name>
     <goal>4</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="2">
   <team home="yes">
    <name>Fremantle Dockers</name>
    <score>
     <goal>16</goal>
     <behind>0</behind>
    </score>
    <highestGoalKicker>
     <name>M.Pavlich</name>
     <goal>5</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Port Adelaide Powers</name>
    <score>
     <goal>19</goal>
     <behind>7</behind>
    </score>
    <highestGoalKicker>
     <name>D.White</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="3">
   <team home="no">
    <name>Collingwood Magpies</name>
    <score>
     <goal>12</goal>
     <behind>10</behind>
    </score>
    <highestGoalKicker>
     <name>P.Medhurst</name>
     <goal>2</goal>
    </highestGoalKicker>
   </team>
   <team home="yes">
    <name>North Melbourne Kangaroos</name>
    <score>
     <goal>10</goal>
     <behind>19</behind>
    </score>
    <highestGoalKicker>
     <name>B.Harvey</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="4">
   <team home="yes">
    <name>Brisbane Lions</name>
    <score>
     <goal>9</goal>
     <behind>15</behind>
    </score>
    <highestGoalKicker>
     <name>A.McGrath</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Hawthorn Hawks</name>
    <score>
     <goal>6</goal>
     <behind>8</behind>
    </score>
    <highestGoalKicker>
     <name>L.Franklin</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="5">
   <team home="yes">
    <name>Sydney Swans</name>
    <score>
     <goal>10</goal>
     <behind>13</behind>
    </score>
    <highestGoalKicker>
     <name>N.Davis</name>
     <goal>2</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>West Coast Eagles</name>
    <score>
     <goal>11</goal>
     <behind>8</behind>
    </score>
    <highestGoalKicker>
     <name>M.Seaby</name>
     <goal>2</goal>
    </highestGoalKicker>
    <highestGoalKicker>
     <name>C.Judd</name>
     <goal>2</goal>
    </highestGoalKicker>
    <highestGoalKicker>
     <name>A.Hunter</name>
     <goal>2</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="6">
   <team home="yes">
    <name>Adelaide Crows</name>
    <score>
     <goal>10</goal>
     <behind>14</behind>
    </score>
    <highestGoalKicker>
     <name>N.Bock</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Essendon Bombers</name>
    <score>
     <goal>16</goal>
     <behind>9</behind>
    </score>
    <highestGoalKicker>
     <name>S.Lucas</name>
     <goal>7</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="7">
   <team home="yes">
    <name>Western Bulldogs</name>
    <score>
     <goal>17</goal>
     <behind>11</behind>
    </score>
    <highestGoalKicker>
     <name>B.Johnson</name>
     <goal>8</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Geelong Cats</name>
    <score>
     <goal>13</goal>
     <behind>15</behind>
    </score>
    <highestGoalKicker>
     <name>C.Mooney</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
  </match>
  <match gameNo="8">
   <team home="yes">
    <name>Carlton Blues</name>
    <score>
     <goal>15</goal>
     <behind>25</behind>
    </score>
    <highestGoalKicker>
     <name>B.Fevola</name>
     <goal>3</goal>
    </highestGoalKicker>
   </team>
   <team home="no">
    <name>Richmond Tigers</name>
    <score>
     <goal>15</goal>
     <behind>8</behind>
    </score>
    <highestGoalKicker>
     <name>K.Pettifer</name>
     <goal>4</goal>
    </highestGoalKicker>
   </team>
  </match>
 </round>
</season>



--  作者:jj_long
--  发布时间:5/27/2010 11:59:00 PM

--  
看一下能否解决你的问题:

XSLT:
============================================
<?xml version='1.0'?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>


<xsl:variable name="v_footyresult" select="document('footyresult.xml')"></xsl:variable>

<xsl:template match="/">
 <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Score</title>
  <style type="text/css">
   body{
    font-family:Arial, Helvetica, sans-serif;
    font-size:9px;
   }
   table{
    font-family:Arial, Helvetica, sans-serif;
    font-size:99x;
   }
  </style>
  </head>
  <body>
   <table border="1" width="90%" align="center">
    <caption>Score</caption>
    <tr>
     <th>name</th>
     <th>headCoach</th>
     <th>goal</th>
     <th>behind</th>
    </tr>
    <xsl:apply-templates/>
   </table>
  </body>
 </html>
</xsl:template>

<xsl:template match="team">
 <xsl:variable name="v_name" select="name"/>
 <tr>
  <td><xsl:value-of select="$v_name"/></td>
  <td><xsl:value-of select="headCoach"/></td>
  <td><xsl:value-of select="$v_footyresult/season/round/match/team[name=$v_name]/score/goal"/></td>
  <td><xsl:value-of select="$v_footyresult/season/round/match/team[name=$v_name]/score/behind"/></td>
 </tr>
</xsl:template>


</xsl:stylesheet>
=============================================

输出结果:
=============================================
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Score</title><style type="text/css">
   body{
    font-family:Arial, Helvetica, sans-serif;
    font-size:9px;
   }
   table{
    font-family:Arial, Helvetica, sans-serif;
    font-size:99x;
   }
  </style></head>
   <body>
      <table border="1" width="90%" align="center">
         <caption>Score</caption>
         <tr>
            <th>name</th>
            <th>headCoach</th>
            <th>goal</th>
            <th>behind</th>
         </tr>
         
         <tr>
            <td>Brisbane Lions</td>
            <td>L.Matthews</td>
            <td>9</td>
            <td>15</td>
         </tr>
          
         <tr>
            <td>Carlton Blues</td>
            <td>D.Pagan</td>
            <td>15</td>
            <td>25</td>
         </tr>
          
         <tr>
            <td>Collingwood Magpies</td>
            <td>M.Malthouse</td>
            <td>12</td>
            <td>10</td>
         </tr>
          
         <tr>
            <td>Essendon Bombers</td>
            <td>K.Sheedy</td>
            <td>16</td>
            <td>9</td>
         </tr>
          
         <tr>
            <td>Fremantle Dockers</td>
            <td>C.Connolly</td>
            <td>16</td>
            <td>0</td>
         </tr>
          
         <tr>
            <td>Geelong Cats</td>
            <td>M.Thompson</td>
            <td>13</td>
            <td>15</td>
         </tr>
          
         <tr>
            <td>Hawthorn Hawks</td>
            <td>A.Clarkson</td>
            <td>6</td>
            <td>8</td>
         </tr>
          
         <tr>
            <td>Melbourne Demons</td>
            <td>N.Danniher</td>
            <td>9</td>
            <td>8</td>
         </tr>
          
         <tr>
            <td>North Melbourne Kangaroos</td>
            <td>D.Laidley</td>
            <td>10</td>
            <td>19</td>
         </tr>
          
         <tr>
            <td>Port Adelaide Powers</td>
            <td>M.Williams</td>
            <td>19</td>
            <td>7</td>
         </tr>
          
         <tr>
            <td>Richmond Tigers</td>
            <td>T.Wallace</td>
            <td>15</td>
            <td>8</td>
         </tr>
          
         <tr>
            <td>Saint Kilda Saints</td>
            <td>G.Thomas</td>
            <td>13</td>
            <td>15</td>
         </tr>
          
         <tr>
            <td>Sydney Swans</td>
            <td>P.Roos</td>
            <td>10</td>
            <td>13</td>
         </tr>
          
         <tr>
            <td>West Coast Eagles</td>
            <td>J.Worsfold</td>
            <td>11</td>
            <td>8</td>
         </tr>
          
         <tr>
            <td>Western Bulldogs</td>
            <td>R.Eade</td>
            <td>17</td>
            <td>11</td>
         </tr>
          
         <tr>
            <td>Adelaide Crows</td>
            <td>N.Craig</td>
            <td>10</td>
            <td>14</td>
         </tr>
         
      </table>
   </body>
</html>
=============================================

注:我使用了Saxon 6.5.5调试此程序。


--  作者:abc911
--  发布时间:5/28/2010 6:19:00 AM

--  
非常感谢
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms