要说接触这些W3C的语言或是标准,肯定都是从HTML语言开始的.我也不例外,而且因为HTML的简单易懂(当然说的是10年前,现在已经扩展的非常难懂了),因此对于这些标记语言比如XML,就没有太在意.最近项目中就有个模块是吧普通的网页抓取过来,然后用XPath去分析HTML的脚本,然后把需要的结果使用XML进行保存,这样就把数据和显示分开了,最后再根据客户端的不同,使用不同的XSLT进行转换,生成和设备端对应的HTML脚本供浏览.
可以看出来里边涉及了很多的标记语言,因此今天就打算粗略介绍一些这些知识.就我个人观点,除非马上就要用,否则这种浅尝辄止的了解就足够了.我是在W3Schools里系统的了解了一番,简要介绍在这里.
1. XSL
XSL应该是和XML相对应的,一个是描述数据的,一个是描述怎样表示和处理数据的.XSL分为三个部分.第一,XSLT,就是怎样表现数据的语言. 第二,XPath,怎样搜索数据的语言.第三,XSL-FO,格式化数据的语言(好像没听说过). 一个比较特殊的实例就是CSS和HTML,HTML应该说是一种特殊的XML,只是有了预定义的标签.而CSS,是表示如何显示HTML的语言,而HTML的臃肿定义已经包含了很多的展示数据的标签比如<B></B><I></I>等.
2. XSLT
XSLT使用XPath进行XML数据的检索.根节点是<xsl:stylesheet> 或者是 <xsl:transform>.
<xsl:template match="/"> 元素指定了在什么位置应用模板中定义的转换方法进行转换.
<xsl:value-of select="..."/>元素,其中value-of表示把XML中的数据显示在这里,后边的select是XPath语句,表示只显示XPath筛选出来的数据.
<xsl:for-each select="aa[bb='cc']">这个元素是循环检索显示的符合标准的数据,XPath指在aa节点下,所有bb属性等于cc的数据.
<xsl:sort select="aa"/> 指定了最终数据的现实按照aa节点的值进行排序.
<xsl:if test="price > 10">条件判断元素,只显示满足条件的元素,注意逻辑判断符号<,>,都要用>,<来代替.
<xsl:choose><xsl:when test="price > 10"></xsl:when><xsl:otherwise></xsl:otherwise></xsl:choose> 和编程语言中的Case...When语句一样,对数据按条件进行归类.
<xsl:apply-templates>指对当前元素以及当前元素的子元素应用符合要求的Template.
在客户端,可以使用JavaScript应用XSLT吧XML翻译成XHTML进行显示.这样会依赖客户端浏览器的规范.
在服务端,可以用ASP,.Net等语言使用平台的XML功能支持进行转换.
3.XPath
一系列的其他语言XQuery,XLink,XPointer和XSLT都依赖于XPath进行数据检索.XPath功能强大内置了上百个function.
Nodes 包括Document Node, Element Node 和Attribute Node.
一些数据检索语法如下:
| Expression | Description |
| nodename | Selects all child nodes of the named node |
| / | Selects from the root node |
| // | Selects nodes in the document from the current node that match the selection no matter where they are |
| . | Selects the current node |
| .. | Selects the parent of the current node |
| @ | Selects attributes |
检索语句中可以包含一个用[]指定的逻辑判断语句,表示只获取符合条件的数据.
通配符
| Wildcard | Description |
| * | Matches any element node |
| @* | Matches any attribute node |
| node() | Matches any node of any kind |
使用 | 把两个或者多个路径条件组合起来,结果是单独检索获得的结果的和.
XPath Axis,表示节点之间的关系条件.
| AxisName | Result |
| ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node |
| ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself |
| attribute | Selects all attributes of the current node |
| child | Selects all children of the current node |
| descendant | Selects all descendants (children, grandchildren, etc.) of the current node |
| descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself |
| following | Selects everything in the document after the closing tag of the current node |
| following-sibling | Selects all siblings after the current node |
| namespace | Selects all namespace nodes of the current node |
| parent | Selects the parent of the current node |
| preceding | Selects everything in the document that is before the start tag of the current node |
| preceding-sibling | Selects all siblings before the current node |
| self | Selects the current node |
XPath 逻辑判断
| Operator | Description | Example | Return value |
| | | Computes two node-sets | //book | //cd | Returns a node-set with all book and cd elements |
| + | Addition | 6 + 4 | 10 |
| - | Subtraction | 6 - 4 | 2 |
| * | Multiplication |
6 * 4 |
24 |
| div | Division | 8 div 4 | 2 |
| = | Equal | price=9.80 | true if price is 9.80 false if price is 9.90 |
| != | Not equal | price!=9.80 | true if price is 9.90 false if price is 9.80 |
| < | Less than | price<9.80 | true if price is 9.00 false if price is 9.80 |
| <= | Less than or equal to | price<=9.80 | true if price is 9.00 false if price is 9.90 |
| > | Greater than | price>9.80 | true if price is 9.90 false if price is 9.80 |
| >= | Greater than or equal to | price>=9.80 | true if price is 9.90 false if price is 9.70 |
| or | or | price=9.80 or price=9.70 | true if price is 9.80 false if price is 9.50 |
| and | and | price>9.00 and price<9.90 | true if price is 9.80 false if price is 8.50 |
| mod | Modulus (division remainder) | 5 mod 2 | 1 |
4. XQuery
XQuery应用XPath对XML数据进行检索,结果不是节点集合,而是一个子XML数据.类似于使用SQL语句检索数据库
5. XLink和XPointer
XLink定义了在XML中加入超链接的基本方法,而XPointer定义了更有针对性的定义超链接到指定XML数据.
浙公网安备 33010602011771号