初学XML入门经典记下笔记之XSLT

XSLT的基本元素。(很多代码源自书中。)

1:<xsl:stylesheet>与<xsl:transfrom> 元素

一般而言每个完整的XSLT样式表都有一个<xsl:stylesheet>或<xsl:transfrom>元素作为其文档元素,其使用根据个人习惯。

如:

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

 2:<xsl:template>元素

一般XSLT处理器在样式表里查找一个match属性值为/的xsl:template元素(代表源树的XPath模型的根结点)

如:<xsl:template match="/">

每当在源树中找到一个根节点时,根据它的模版内容,在目标树种添加与之相对的树形结构,但XPath模型中只有一个根结点,因此只允许根节点在目标树中添加一次。

3:<xsl:apply-templates>元素

如:<xsl:apply-templates  select="/../..">

它促使XSLT处理器在源树中找到与之相匹配的节点,其中select属性定位XPath的定位路径。

利用<xsl:value-of select="/../..">元素来进行其模版子节点的匹配。

具体如下:

<!--调用模版-->

<xsl:apply-templates select="/../..">

<!--定义模版-->

<xsl:template match="元素名">

<xsl:value-of select="子元素名">

<xsl:template>

4:<xsl:value-of>元素

此元素可以提供源树中某一部分的值,其必须有select属性,该属性提供一个XPath的定位路径。在select属性中还可以用上XPath中的函数,如count。

<xsl:value-of select="count(/../..)"/>这可以显现根元素下有几个子元素。

当元素结点的模版被实例化时就由<xsl:apply-templates>元素的select属性决定。

在XSLT1.0中,xsl:value-of选取的结点的顺序组时,只能有第一个结点而在XSLT2.0中却可以依次输出各个子结点并用空格分开。如需像1.0中只有一个则可以用下标给出如:<xsl:value-of select="..[1]"/>

5:<xsl:copy>元素

由于xsl:value-of元素只能取出文档顺序的第一个结点值,而不是全部的结点值。要利用重构元素。

<xsl:copy>元素把一个节点复制到目标树中,但并不复制它的子孙结点,如果上下文结点是一个元素结点,也不会复制其任何属性。

当我们想使用某元素,但又想改变它的结构,如增加或删除它的属性时。<xsl:copy>元素就派上用场了。

如有Persons.xml文档

<Persons>
 <Person>
  <FirstName>科比</FirstName>
  <LastName>布莱恩特</LastName>
 </Person>
 <Person>
  <FirstName>姚</FirstName>
  <LastName>明</LastName>
 </Person>
 <Person>
  <FirstName>Paul</FirstName>
  <LastName>Cathedral</LastName>
 </Person>
</Persons>
当我们想改变它,把FirstName与LastName元素作为属性时可以如下转换。

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

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

<xsl:template match="Person">
 <xsl:copy>
  <xsl:attribute name="FirstName"><xsl:value-of select="FirstName"/>
</xsl:attribute>
  <xsl:attribute name="LastName"><xsl:value-of select="LastName"/>
</xsl:attribute>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>
就可转换为:

<Persons>
<Person FirstName=“科比”LastName=“布莱恩特”/>

<Person FirstName=“姚”LastName=“明”/>

<Persons>

当然,我们也可用重构元素用相反的方法,即对其添加子元素。
如下代码:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:template match="/">
 <Persons>
  <xsl:apply-templates select="/Persons/Person" />
 </Persons>
</xsl:template>
<xsl:template match="Person">
 <xsl:copy>
  <xsl:element name="FirstName"><xsl:value-of select="@FirstName"/>
</xsl:element>
  <xsl:element name="LastName"><xsl:value-of select="@LastName"/>
</xsl:element>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>
就可对其添加子元素。

6:有<xsl:copy>就有<xsl:copy-of>元素

<xsl:copy-of>元素可以解决<xsl:copy>元素只可复制一个子节点的问题,从而可以实现深复制,即把某一结点的所有属性及其子孙结点都复制到目标树中。如:

<?xml version="1.0" encoding="UTF-8"?>

<Invoice>

  <From>XMML.com</From>

  <To>Example.org</To>

  <Address>

    <Street>234 Any Street</Street>

     <City>Any Town</City>

     <State>MO</State>

     <ZipCode>98765</ZipCode>

 </Address>

  <!--The rest of the Invoice would go here.-->

</Invoice>
当我们要创建另一份文件时
如:
<xsl:shtlesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<dizhi>
<xsl:apply-templates select="/Invoice/Address"/>
</dizhi>
</xsl:template>
<xsl:template match="Address">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
则其会转换为:
<dizhi>

    <Street>234 Any Street</Street>

     <City>Any Town</City>

     <State>MO</State>

     <ZipCode>98765</ZipCode>

 </dizhi>
7:<xsl:output>元素
XSLT可以生成XML HTML文档或文本文档,我们可以利用此元素中的method属性从这些文档中选择一种
输出格式如:
<xsl:output method="xml"/>注意必须小写
<xsl:output method="text"/>
8:条件处理(1)<xsl:if>元素
此元素测试一个布尔条件,当条件为真时就实例化此元素内的内容,否则此元素内的内容不会
添加到目标树中。
如我们有一文档。
<Characters>
 <Character age="99">Julius Caesar</Character>
 <Character age="23">Anne Boleyn</Character>
 <Character age="41">George Washington</Character>
 <Character age="45">Martin Luther</Character>
 <Character age="800">Methuselah</Character>
 <Character age="119">Moses</Character>
 <Character age="50">Asterix the Gaul</Character>
</Characters>
当我们发现有些年龄大的出奇,就可以用xsl:if对其进行判别。如:
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

<xsl:template match="/">
 <html>
  <head>
   <title>Age check on Characters.</title>
  </head>
  <body>
  <h3>The recorded age is unusually high. Please check original data.</h3>
  <xsl:apply-templates select="/Characters/Character" />
  </body>
 </html>
</xsl:template>
<xsl:template match="Character">
 <xsl:if test="@age &gt; 110 " >
 <p><b><xsl:value-of select="." /></b> is older than expected. Please check if this

character’s age, <b><xsl:value-of select="@age" /></b>, is correct.</p>
 </xsl:if>
</xsl:template>

</xsl:stylesheet>
条件处理(2)<xsl:choose>元素
<xsl:if>元素只可作一种判别,当我们需要有多个条件时就可以用<xsl:choose>元素来处理
此元素有两个子元素<xsl:when>(可多次)与<xsl:otherwise>
在某个元素中都有text属性对其判断。
9:<xsl:for-each>元素
此元素把嵌入其中的XSLT指令用到结点集里的每个元素上,可以用来迭代处理整个结点集并为每个节点

集生成或输出文档,相当于C/C++中的for循环。
如有下文档
<?xml version="1.0"?>
<Objects>
 <Object name="Car">
  <Characteristic>Hard</Characteristic>
  <Characteristic>Shiny</Characteristic>
  <Characteristic>Has 4 wheels</Characteristic>
  <Characteristic>Internal Combustion Engine</Characteristic>
 </Object>
</Objects>
当我们用下面转换文档时。
?xml version="1.0"?>
<xsl:stylesheet

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

<xsl:template match="/">
 <html>
  <head>
   <title>Object Characteristics</title>
  </head>
  <body>
  <xsl:apply-templates select="/Objects/Object" >
   <xsl:sort select="@name" />
  </xsl:apply-templates>
  </body>
 </html>
</xsl:template>

<xsl:template match="Object">
  <h3>Characteristics of <xsl:value-of select="@name" /></h3>
 <ul>
 <xsl:for-each select="Characteristic">
 <xsl:sort select="." order="descending" />
  <li><xsl:value-of select="." /></li>
 </xsl:for-each>
 </ul>
</xsl:template>

</xsl:stylesheet>
就可以对其转换。

还有未完。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值