XSLT 编写要点一(学习笔记)

XSLT 一种类似于CSS 的东西,主要是将XML 转变为以HTML格式输出的样式表(姑且这样说,肯能还有更深层次的意思,我还没有理解到)。
要将XML中的内容以HTML格式输出,主要是要在XSLT中建立模板,让模板来设置XML中数据的格式或者说它的显示样式吧。
XSLT的加工器(PROCESSOR)在处理一个XSLT 样式表时会先由定义“/”根这个模板开始处理,如果在XSLT样式表中没有定义这个处理根元数的模板,加工器就会处理紧接着的模板。而在该模板中定义的节点范围以外的所有节点都会忽略不进行处理。
example:

a.xml

<root>
   <aaa id="a1">
          <bbb id="b1"/>
          <bbb id="b2"/>
   </aaa>
   <aaa id="a2">
       <bbb id="b3" />
       <bbb id="b4" />
       <ccc id="c1" >
              <ddd id="d1" />
       </ccc>
      <bbb id="b5">
           <ccc id="c2" />
      </bbb>
     </aaa>
 </root>
xslt 文件
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="aaa">
     <div style="color:purple">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
</xsl:template>

<xsl:template match="bbb">
     <div style="color:blue">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
</xsl:template>

<xsl:template match="ccc">
     <div style="color:maroon">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
</xsl:template>

<xsl:template match="ddd">
     <div style="color:green">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
</xsl:template>


</xsl:stylesheet> 
输出结果:
<div style="color:purple">aaa id=a1</div>

<div style="color:purple">aaa id=a2</div>
因为在XSLT中并没有定义根模板"/" 所以XSLT加工器只使用了紧跟着的模板,并且在使用的该模板以外的所有其他节点都忽略不进行处理。
如果使用了有定义根“/”模板的XSLT样式表,效果就完全不同了,加工器就会将所有定义的节点都进行了处理。
XSLT样式表

<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/xslTutorial">
     <xsl:apply-templates/>
</xsl:template>

<xsl:template match="aaa">
     <div style="color:purple">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
     <xsl:apply-templates/>
</xsl:template>

<xsl:template match="bbb">
     <div style="color:blue">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
     <xsl:apply-templates/>
</xsl:template>

<xsl:template match="ccc">
     <div style="color:maroon">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
     <xsl:apply-templates/>
</xsl:template>

<xsl:template match="ddd">
     <div style="color:green">
          <xsl:value-of select="name()"/>
          <xsl:text> id=</xsl:text>
          <xsl:value-of select="@id"/>
     </div>
</xsl:template>


</xsl:stylesheet>
输出的效果:
<div style="color:purple">aaa id=a1</div>

<div style="color:blue">bbb id=b1</div>

<div style="color:blue">bbb id=b2</div>


<div style="color:purple">aaa id=a2</div>

<div style="color:blue">bbb id=b3</div>

<div style="color:blue">bbb id=b4</div>

<div style="color:maroon">ccc id=c1</div>

<div style="color:green">ddd id=d1</div>


<div style="color:blue">bbb id=b5</div>

<div style="color:maroon">ccc id=c2</div>

转载于:https://www.cnblogs.com/xivi/archive/2004/12/20/79575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值