新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论XSL,XSLT,XSL-FO,CSS等技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 XSL/XSLT/XSL-FO/CSS 』 → 一个有趣的问题,大家快来看看,这个xsl转换结果如此神奇!! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2440 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 一个有趣的问题,大家快来看看,这个xsl转换结果如此神奇!! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     highshow 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:135
      门派:XML.ORG.CN
      注册:2004/11/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给highshow发送一个短消息 把highshow加入好友 查看highshow的个人资料 搜索highshow在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看highshow的博客楼主
    发贴心情 一个有趣的问题,大家快来看看,这个xsl转换结果如此神奇!!

    问题:如何写出trans.xsl,将下面的student.xml转换为student.html?
    student.xml的内容如下:
    <?xml version="1.0" encoding="GB2312" ?>
    <dsStudent>
      <StudentInfo>
        <StudentName>吴大帅</StudentName>
        <StudentAge>22</StudentAge>
        <StudentSex>男</StudentSex>
        <StudentEMail>213@sina.com</StudentEMail>
      </StudentInfo>
      <Lesson>
        <Name>Math</Name>
      </Lesson>
      <Lesson>
        <Name>English</Name>
      </Lesson>
      <Lesson>
        <Name>Primary</Name>
      </Lesson>  
    </dsStudent>
    要求转换成的student.html如下:
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <body>
    <table border="1" width="37%">
      <tr>
        <td width="18%">姓名:</td>
        <td width="25%">吴大帅</td>
        <td width="22%">年龄:</td>
        <td width="37%">22</td>
      </tr>
      <tr>
        <td width="18%">性别:</td>
        <td width="25%">男</td>
        <td width="22%">EMAIL:</td>
        <td width="37%">213@sina.com</td>
      </tr>
    </table>
    <table border="1" width="38%" height="65">
      <tr>
        <td width="100%" height="16">该学生所学课程如下:</td>
      </tr>
      <tr>
        <td width="100%" height="15">Math</td>
      </tr>
      <tr>
        <td width="100%" height="16">English</td>
      </tr>
      <tr>
        <td width="100%" height="16">Primary</td>
      </tr>
    </table>
    </body>
    </html>
    本人写了一个,可就是不行,如下:
    <?xml version='1.0' encoding="GB2312"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="dsStudent">
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <body>
    <xsl:for-each select="StudentInfo">
    <table border="1" width="37%">
      <tr>
        <td width="18%">姓名:</td>
        <td width="25%"><xsl:value-of select="StudentName"/></td>
        <td width="22%">年龄:</td>
        <td width="37%"><xsl:value-of select="StudentAge"/></td>
      </tr>
      <tr>
        <td width="18%">性别:</td>
        <td width="25%"><xsl:value-of select="StudentSex"/></td>
        <td width="22%">EMAIL:</td>
        <td width="37%"><xsl:value-of select="StudentEMail"/></td>
      </tr>
    </table>
    <table border="1" width="38%" height="65">
      <tr>
        <td width="100%" height="16">该学生所学课程如下:</td>
      </tr>

      <xsl:for-each select="Lesson">
      <tr>
        <td width="100%" height="15"><xsl:value-of select="Name"/></td>
      </tr>
      </xsl:for-each>

    </table>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    上面转换后的.html结果如下(就是不显示课程信息):
    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>New Page 1</title>
      </head>
      <body>
        <table border="1" width="37%">
          <tr>
            <td width="18%">姓名:</td>
            <td width="25%">吴大帅</td>
            <td width="22%">年龄:</td>
            <td width="37%">22</td>
          </tr>
          <tr>
            <td width="18%">性别:</td>
            <td width="25%">男</td>
            <td width="22%">EMAIL:</td>
            <td width="37%">213@sina.com</td>
          </tr>
        </table>
        <table border="1" width="38%" height="65">
          <tr>
            <td width="100%" height="16">该学生所学课程如下:</td>
          </tr>
        </table>
      </body>
    </html>


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/21 20:21:00
     
     doubleG 帅哥哟,离线,有人找我吗?
      
      
      威望:7
      等级:大三(面向对象是个好东东!)
      文章:591
      积分:4119
      门派:XML.ORG.CN
      注册:2004/5/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给doubleG发送一个短消息 把doubleG加入好友 查看doubleG的个人资料 搜索doubleG在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看doubleG的博客2
    发贴心情 
    StudentInfo与Lesson的所处的Level是相同的,但是你却
    <xsl:for-each select="StudentInfo">
    ........
      <xsl:for-each select="Lesson">

    ......
    </xsl:for-each>
    </xsl:for-each>
    这样子选取的Lesson是StudentInfo之下的,但是StudentInfo下面是没有Lesson节点,所以取不到,你要并行放置才行啊。
    <xsl:for-each select="StudentInfo">
    ........
    </xsl:for-each>
      <xsl:for-each select="Lesson">
    ......
    </xsl:for-each>

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/22 9:14:00
     
     highshow 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:135
      门派:XML.ORG.CN
      注册:2004/11/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给highshow发送一个短消息 把highshow加入好友 查看highshow的个人资料 搜索highshow在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看highshow的博客3
    发贴心情 
    非常感谢doubleG的神奇指点:
    本人按照这位大侠的指点写了下面的xsl文档:
    <?xml version='1.0' encoding="GB2312"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <body>
    <table border="1" width="37%">
    <xsl:for-each select="dsStudent/StudentInfo">
      <tr>
        <td width="18%">姓名:</td>
        <td width="25%"><xsl:value-of select="StudentName"/></td>
        <td width="22%">年龄:</td>
        <td width="37%"><xsl:value-of select="StudentAge"/></td>
      </tr>
      <tr>
        <td width="18%">性别:</td>
        <td width="25%"><xsl:value-of select="StudentSex"/></td>
        <td width="22%">EMAIL:</td>
        <td width="37%"><xsl:value-of select="StudentEMail"/></td>
      </tr>
    </xsl:for-each>
    </table>
    <table border="1" width="38%" height="65">
      <tr>
        <td width="100%" height="16">该学生所学课程如下:</td>
      </tr>
      <xsl:for-each select="dsStudent/Lesson">
      <tr>
        <td width="100%" height="15"><xsl:value-of select="Name"/></td>
      </tr>
      </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    转换后的html如下:
    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>New Page 1</title>
      </head>
      <body>
        <table border="1" width="37%">
          <tr>
            <td width="18%">姓名:</td>
            <td width="25%">吴大帅</td>
            <td width="22%">年龄:</td>
            <td width="37%">22</td>
          </tr>
          <tr>
            <td width="18%">性别:</td>
            <td width="25%">男</td>
            <td width="22%">EMAIL:</td>
            <td width="37%">213@sina.com</td>
          </tr>
        </table>
        <table border="1" width="38%" height="65">
          <tr>
            <td width="100%" height="16">该学生所学课程如下:</td>
          </tr>
          <tr>
            <td width="100%" height="15">Math</td>
          </tr>
          <tr>
            <td width="100%" height="15">English</td>
          </tr>
          <tr>
            <td width="100%" height="15">Primary</td>
          </tr>
        </table>
      </body>
    </html>
    得到了本人想要的结果!!!
    可是本人的问题现在所处的问题是:两个 for each不能分开
    <xsl:for-each select="StudentInfo">
    ........
    </xsl:for-each>
      <xsl:for-each select="Lesson">
    ......
    </xsl:for-each>

    因为要转换成的html形式是这样的:
    <table border="1" width="37%">
      <tr>
        <td width="18%">姓名:</td>
        <td width="25%">吴大帅</td>
        <td width="22%">年龄:</td>
        <td width="37%">22</td>
      </tr>
    </table>
    <table border="1" width="38%" height="65">
      <tr>
        <td width="100%" height="16">该学生所学课程如下:</td>
      </tr>
      <tr>
        <td width="100%" height="15">Math</td>
      </tr>
      <tr>
        <td width="100%" height="16">English</td>
      </tr>
      <tr>
        <td width="100%" height="16">Primary</td>
      </tr>
    </table>
    <table border="1" width="37%">
      <tr>
        <td width="18%">性别:</td>
        <td width="25%">男</td>
        <td width="22%">EMAIL:</td>
        <td width="37%">213@sina.com</td>
      </tr>
    </table>
    就是说,学生的基本信息中间夹杂着学生的选课信息!这个问题该如何多解决??

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/22 12:38:00
     
     doubleG 帅哥哟,离线,有人找我吗?
      
      
      威望:7
      等级:大三(面向对象是个好东东!)
      文章:591
      积分:4119
      门派:XML.ORG.CN
      注册:2004/5/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给doubleG发送一个短消息 把doubleG加入好友 查看doubleG的个人资料 搜索doubleG在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看doubleG的博客4
    发贴心情 
    那也很简单,StudentInfo和Lesson是属于同一层的是吧。
    那么就
    <xsl:for-each select="StudentInfo">
      ..........
    <xsl:for-each select="../Lesson">
    .......
    </xsl:for-each>
    </xsl:for-each>
    但是你的XML文件结构具体是怎样的呢?如果只有一组student的话可以这样,要是
    <StudentInfo/>
    <Lesson/>
    <Lesson/>
    <StudentInfo/>
    <Lesson/>
    ......这样的结构就不行。这样的话要具体处理,呵呵
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/22 13:30:00
     
     highshow 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:16
      积分:135
      门派:XML.ORG.CN
      注册:2004/11/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给highshow发送一个短消息 把highshow加入好友 查看highshow的个人资料 搜索highshow在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看highshow的博客5
    发贴心情 
    一觉醒来,doubleG就帮我解决了,太神奇了,非常感谢这位大侠!!!
    我的这个xml只可能出现一对<StudentInfo></StudentInfo>,就是说一个xml只对一个学生进行描述,呵呵,我还是三年前自学的xml,都忘得差不多了!!
    <?xml version="1.0" encoding="GB2312" ?>
    <dsStudent>
      <StudentInfo>
        <StudentName>吴大帅</StudentName>
        <StudentAge>22</StudentAge>
        <StudentSex>男</StudentSex>
        <StudentEMail>213@sina.com</StudentEMail>
      </StudentInfo>
      <Lesson>
        <Name>Math</Name>
      </Lesson>
      <Lesson>
        <Name>English</Name>
      </Lesson>
      <Lesson>
        <Name>Primary</Name>
      </Lesson>  
    </dsStudent>
    呵呵,如果大侠不出此招,我就只能编写如下的xsl了,不过有失水准:
    <?xml version='1.0' encoding="GB2312"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <body>
    <table border="1" width="37%">
    <xsl:for-each select="dsStudent/StudentInfo">
      <tr>
        <td width="18%">姓名:</td>
        <td width="25%"><xsl:value-of select="StudentName"/></td>
        <td width="22%">年龄:</td>
        <td width="37%"><xsl:value-of select="StudentAge"/></td>
      </tr>
    </xsl:for-each>
    </table>
    <table border="1" width="38%" height="65">
      <tr>
        <td width="100%" height="16">该学生所学课程如下:</td>
      </tr>
      <xsl:for-each select="dsStudent/Lesson">
      <tr>
        <td width="100%" height="15"><xsl:value-of select="Name"/></td>
      </tr>
      </xsl:for-each>
    </table>
    <xsl:for-each select="dsStudent/StudentInfo">
      <tr>
        <td width="18%">性别:</td>
        <td width="25%"><xsl:value-of select="StudentSex"/></td>
        <td width="22%">EMAIL:</td>
        <td width="37%"><xsl:value-of select="StudentEMail"/></td>
      </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    呵呵,看来我还是要借几个xml的书,好好的学习了,不过现在这个系统赶得要紧,所以心情没有那个静,不能静下心来好好研读xsl 的资料,以后常来这!再次感谢!!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/22 14:47:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/30 16:20:28

    本主题贴数5,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    3,140.625ms