使用XSL的select 有选择的显示数据

本文档展示了如何使用XSL样式表从XML文档中有选择地显示数据。通过匹配不同元素并应用模板,实现了XML中Title、Author的name、Press和Price元素的特定样式显示,如字体大小、颜色等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.XML文档

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="book2.xsl"?>
<BookLib>
 <Book>
  <Title>Windows程序设计</Title>
  <Author>
   <name>好孩子</name>
   <Email>haohaizi@163.com</Email>
  </Author>
  <Press>
  <PressDate>2000年5月1日</PressDate>
  <PressCompany>南京出版社</PressCompany>
  </Press>
  <Price>49.00元</Price>
 </Book>
 <Book>
  <Title>深入潜出XML</Title>
  <Author>
   <name>老虎工作室</name>
   <Email>laohu@163.com</Email>
  </Author>
  <Press>
  <PressDate>2006年5月12日</PressDate>
  <PressCompany>北京出版社</PressCompany>
  </Press>
  <Price>28.00元</Price>
 </Book>
 <Book>
  <Title>人工智能技术导论</Title>
  <Author>
   <name>廉师友</name>
   <Email>laolian@163.com</Email>
  </Author>
  <Press>
  <PressDate>2006年7月12日</PressDate>
  <PressCompany>上海出版社</PressCompany>
  </Press>
  <Price>18.00元</Price>
 </Book>
</BookLib>

上面的XML文档定义了根元素为BookLib,三个子元素为Book的树,

其中Book元素又有Title,Author,Press,Price 四个元素,其中Author元素又有name和Email两个子元素,

Press元素又有PressDate和PressCompany元素,

2.XSL文档的内容

<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="
http://www.w3.org/TR/WD-xsl">
 <xsl:template match="/">
  <html>
   <xsl:apply-templates/>
  </html>
 </xsl:template>
 <xsl:template match="BookLib">
  <body>
   <xsl:apply-templates/>
  </body>
 </xsl:template>
 <xsl:template match="Book">
  <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="Title">
  <Font size="3" color="#0000FF">
   <BR/>
   <xsl:value-of select="."/>
  </Font>
 </xsl:template>
 <xsl:template match="Author">
  <Font size="3" color="#FF0000">
   <BR/>
   <xsl:value-of select="name"/>
  </Font>
 </xsl:template>
 <xsl:template match="Press">
  <Font size="3" color="#FF00FF">
   <BR/>
   <xsl:value-of select="."/>
  </Font>
 </xsl:template>
 <xsl:template match="Price">
  <Font size="3" color="#999999">
   <BR/>
   <xsl:value-of select="."/>
  </Font>
 </xsl:template>
</xsl:stylesheet>

其中


xsl:value-of select="name" 这行表示我们显示的是Author元素的name子元素,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值