当前位置: 移动技术网 > IT编程>网页制作>XML > 匹配模式 - XSL教程 - 4

匹配模式 - XSL教程 - 4

2017年12月01日  | 移动技术网IT编程  | 我要评论
<xsl:template> 元素定义了用于匹配节点的规则(match,其中"/"匹配整个文档),在apply-template使用  &n
<xsl:template> 元素定义了用于匹配节点的规则(match,其中"/"匹配整个文档),在apply-template使用
   语法规则为:
<xsl:template
name="name"
match="pattern"
mode="mode"
priority="number">  
<!-- content:(<xsl:param>*,template) -->
</xsl:template>

  其中:
  name 模板名称
  match xpath语句,指定条件
  mode模式,例如红,蓝等样式
  priority优先级,为数字
  例如如下的xml文件:<?xml version="1.0" encoding="gb2312"?>
<?xml:stylesheet type="text/xsl" href="userlist_template.xsl"?>
<users>
    <user isadmin='ok'>
        <name>5do8</name>
        <id>1</id>
        <contact>
            <qq>369987789</qq>
            <email>service@163.com</email>
        </contact>
    </user>
    <user>
        <name>cjjer</name>
        <id>2</id>
        <contact>
            <qq>369987789</qq>
            <email>service@163.com</email>
        </contact>
    </user>
    <user>
        <name>admin</name>
        <id>3</id>
        <contact>
            <qq>369987789</qq>
            <email>service@163.com</email>
        </contact>
    </user>
</users>
其中使用的模板(userlist_template.xsl)为:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:template match="/">
  <html>
      <body>
      <h2>all user list</h2>
        <xsl:apply-templates/> 
      </body>
  </html>
</xsl:template>
<xsl:template match="user">
  <p>
  <xsl:apply-templates select="name"/> 
  <xsl:apply-templates select="id"/>
  </p>
</xsl:template>
<xsl:template match="name">
  name: <span style="color:#bb0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
<xsl:template match="id">
  id: <span style="color:#808000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
</xsl:stylesheet>

  可以以列表的方式显示用户信息。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网