xml入门2

4 xml中的样式表

  • css
  • Xml代码
    1. <?xml version ="1.0" encoding ="GB2312"?>  
    2.     <?xml-stylesheet type="text/css" href ="test.css"?>  
    3.     <book>  
    4.         <name>JS</name>  
    5.     </book>  
    <?xml version ="1.0" encoding ="GB2312"?> <?xml-stylesheet type="text/css" href ="test.css"?> <book> <name>JS</name> </book>
    test.css
    Css代码
    1. book{display:block; position:absolute;   top:20px;    400 px;    border:2px solid black; padding:5px;     background-color;yellow;     color:green;     font-family:times, serif;            font-style:italic;text-align:center;         }  
    2. name{display:block; position:relative;   top:10 px;   300 px;    height:60 px;    font-weight:bold;    font-size:50px; font-family:Arial,Helvetica,sans-serif; font-style:italic;   color:red;       }  
    book{display:block; position:absolute; top:20px; 400 px; border:2px solid black; padding:5px; background-color;yellow; color:green; font-family:times, serif; font-style:italic;text-align:center; } name{display:block; position:relative; top:10 px; 300 px; height:60 px; font-weight:bold; font-size:50px; font-family:Arial,Helvetica,sans-serif; font-style:italic; color:red; }
  • xsl
  • Xml代码
    1. <?xml version ="1.0" encoding ="GB2312"?>  
    2.     <?xml-stylesheet type="text/xls" href ="b.xsl"?>  
    3.     <book>  
    4.         <name>JS</name>  
    5.     </book>  
    <?xml version ="1.0" encoding ="GB2312"?> <?xml-stylesheet type="text/xls" href ="b.xsl"?> <book> <name>JS</name> </book>
    Xml代码
    1. <?xml version="1.0" encoding="GB2312"?>  
    2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    3.      version="1.0">  
    4.   <xsl:template match="/">   
    5.     <html>  
    6.      <head><title>XSL</title></head>  
    7.       <body>  
    8.       <xsl:apply-templates select="book" mode="1">  
    9.      </body>  
    10.     </html>  
    11.    </xsl:template>  
    12.   
    13. <xsl:template match="book" mode="1">  
    14. <h2>  
    15. <xsl:value-of select="name"/>  
    16. </h2>  
    17. </xsl:template>  
    18.   
    19. <xsl:template match="book" mode="2">  
    20. <h3>  
    21. <xsl:value-of select="name"/>  
    22. </h3>  
    23. </xsl:template>  
    24.   
    25.   </xsl:stylesheet>  
    <?xml version="1.0" encoding="GB2312"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head><title>XSL</title></head> <body> <xsl:apply-templates select="book" mode="1"> </body> </html> </xsl:template> <xsl:template match="book" mode="1"> <h2> <xsl:value-of select="name"/> </h2> </xsl:template> <xsl:template match="book" mode="2"> <h3> <xsl:value-of select="name"/> </h3> </xsl:template> </xsl:stylesheet>
    在上面的例子中我们大概的可以看到一个xsl文件的框架,它有xsl:stylesheet包围,里面包括了三个匹配模式(其中一个主匹配模式,两个从匹配模式),还有一个执行模式xsl:apply-templates

  • xsl中的标签

  • xsl:text
    Xml代码
    1. <xsl:text>xx</xsl:text>  
    <xsl:text>xx</xsl:text>
    xsl:for-each
    Xml代码
    1. <xsl:for-each select = "book">  
    2.      ...  
    3. </xsl:for-each>   
    <xsl:for-each select = "book"> ... </xsl:for-each>

    xsl:value-of
    Xml代码
    1. <xsl:value-of select="name"/>  
    <xsl:value-of select="name"/>
    xsl:for-each
    Xml代码
    1. <xsl:for-each select = "book">  
    2.      ...  
    3. </xsl:for-each>   
    <xsl:for-each select = "book"> ... </xsl:for-each>
    xsl:if
    Xml代码
    1. <xsl:if test=“表达式” >  
    2. ...  
    3. </xsl:if>  
    <xsl:if test=“表达式” > ... </xsl:if>
    疑问:xsl:if能否实现正值表达式。
    xsl:choose, xsl:when和xsl:otherwise
    Xml代码
    1. <xsl:choose>  
    2.      <xsl:when test="表达式''  
    3.         …  
    4.      </xsl:when>  
    5.      <xsl:otherwise>  
    6.          …  
    7.      </xsl:otherwise>  
    8. </xsl:choose>  
    <xsl:choose> <xsl:when test="表达式'' … </xsl:when> <xsl:otherwise> … </xsl:otherwise> </xsl:choose>

    xsl:sort
    Xml代码
    1. <xsl:for-each select = "book">  
    2.       <xsl:sort select = "@title"/>  
    3.             <b><xsl:value-of select = "title"/></b>  
    4.             <br/>  
    5.             <b><xsl:value-of select = "price"/></b>  
    6.             <br/>  
    7. </xsl:for-each>  
    <xsl:for-each select = "book"> <xsl:sort select = "@title"/> <b><xsl:value-of select = "title"/></b> <br/> <b><xsl:value-of select = "price"/></b> <br/> </xsl:for-each>

    引:xsl:sort能否实现自定义排序。大案是是可以得
    Xml代码
    1. <xsl:sort  select=string-expression  
    2. data-type={"text"|"number"}  
    3. order={"ascending"|"descending"}  
    4. case-order={"upper-first"|"lower-first"}/>  
    <xsl:sort select=string-expression data-type={"text"|"number"} order={"ascending"|"descending"} case-order={"upper-first"|"lower-first"}/>
    参数:data-type它的值常用的是text,number。text是指数据被当作字符串进行排序,number则是把数据转换为数字,按照数值进行排序。缺省的值是text
    Order属性指定排序时是按照升序(ascending)还是降序(descending),缺省的情况是升序。
    case-order属性是指按字母表顺序排时,大写在前还是小写在前

    xsl读取某个节点的属性
    Xml代码
    1. <xsl:value-of   select="nodeName/@attributeName"/>或者  
    2. <xsl:for-each   select="price">     
    3.   <h1>:<xsl:value-of   select="@unit"/></h1>   
    4. </xsl:for-each>  
    <xsl:value-of select="nodeName/@attributeName"/>或者 <xsl:for-each select="price"> <h1>:<xsl:value-of select="@unit"/></h1> </xsl:for-each>
原文地址:https://www.cnblogs.com/danghuijian/p/4400895.html