XSLT 学习记录(一些语法和知识,备忘)

本文档详细介绍了 XSLT 的使用方法和技术细节,包括节点选择、模板匹配、变量设置、条件处理等功能,并提供了丰富的示例代码。

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

Enjoy ^_^

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
<xsl:output method="html"/>
    
<xsl:template match="/">
        
<xsl:apply-templates select="//CCC"/>
    
</xsl:template>
    
<xsl:template match="CCC" priority="4">
        
<div style="color: green">
            
<!-- node name -->
            
<xsl:value-of select="name()"/>
            
<!-- text -->
            
<xsl:text> id=</xsl:text>
            
<!-- attribute -->
            
<xsl:value-of select="@id"/>
            
<xsl:value-of select="data/@color"/>
        
</div>
    
</xsl:template>
    
<!--match attribute-->
    
<xsl:template match="@id">
        
<b>
            
<i>
                
<xsl:value-of select="."/>
            
</i>
        
</b>
    
</xsl:template>
    
<!--hava attribute or not-->
    
<xsl:template match="car[@checked]">
        
<p>
            
<xsl:text>Car: </xsl:text>
            
<xsl:value-of select="@id"/>
        
</p>
    
</xsl:template>
    
<xsl:template match="car[not(@checked)]">
        
<style="color: red;">
            
<xsl:text>Car: </xsl:text>
            
<xsl:value-of select="@id"/>
        
</p>
    
</xsl:template>
    
<!--any child -->
    
<!-- xsl:for-each -->
    
<xsl:for-each select="/source//*">
        
<!-- call template not apply-template-->
        
<xsl:call-template name="print"/>
    
</xsl:for-each>
    
<!-- called template-->
    
<xsl:template name="print">
        
<xsl:value-of select="./@id"/>
        
<!-- for each child -->
        
<xsl:for-each select="child::*">
            
<xsl:value-of select="./@id"/>
        
</xsl:for-each>
        
<!-- for each descendant -->
        
<xsl:for-each select="descendant::*">
               
</xsl:for-each>
        
<!-- xsl:for-each parent -->
        
<xsl:for-each select="parent::*">
            
<!--xsl:if test="not(@id)"-->
            
<xsl:if test="not(@id)">
                
<xsl:value-of select="name()"/>
            
</xsl:if>
            
<xsl:value-of select="./@id"/>
        
</xsl:for-each>
        
<!-- xsl:for-each ancester -->
        
<xsl:for-each select="ancestor::*">
               
</xsl:for-each>
        
<!-- xsl:for-each following-sibling-->
        
<xsl:for-each select="following-sibling::*">
               
</xsl:for-each>
        
<!-- xsl:for-each preceding-sibling -->
        
<xsl:for-each select="preceding-sibling::*">
               
</xsl:for-each>
        
<!--xsl:following-->
        
<xsl:for-each select="following::*">
               
</xsl:for-each>
        
<!-- preceding -->
        
<xsl:for-each select="preceding::*">
               
</xsl:for-each>
        
<!-- attribute -->
        
<xsl:for-each select="attribute::*">
        
</xsl:for-each>
        
<!-- namespace -->
        
<xsl:for-each select="namespace::*">
        
</xsl:for-each>
        
<!-- self -->
        
<xsl:for-each select="self::*">
        
</xsl:for-each>
        
<!-- descendant-or-self -->
        
<xsl:for-each select="descendant-or-self::*">
        
</xsl:for-each>
        
<!-- ancestor-or-self -->
        
<xsl:for-each select="ancestor-or-self ::*">
        
</xsl:for-each>
        
<!-- abbreviated -->
        
<xsl:value-of select="child::BBB"/>
        
<xsl:value-of select="BBB"/>
        
<xsl:value-of select="attribute::id"/>
        
<xsl:value-of select="@id"/>
        
<xsl:value-of select="parent::BBB"/>
        
<xsl:value-of select="../BBB"/>
        
<xsl:value-of select="name(/descendant-or-self::*)"/>
        
<xsl:value-of select="name(//*)"/>
        
<!-- The xsl:for-each instruction contains a template, 
        which is applied to each node selected with select attribute. 
-->
        
<xsl:for-each select="//BBB">
        
</xsl:for-each>
        
<!-- Nodes selected with xsl:for-each or xsl:apply-templates 
        can be sorted.  
-->
        
<xsl:for-each select="//name">
            
<xsl:sort order="ascending" select="."/>
            
<xsl:value-of select="."/>
        
</xsl:for-each>
        
<xsl:for-each select="//name">
            
<xsl:sort order="descending" select="."/>
        
</xsl:for-each>
        
<xsl:apply-templates select="//name">
            
<xsl:sort order="descending" select="."/>
        
</xsl:apply-templates>
        
<!-- sorts can be in text and in numeric mode. -->
        
<xsl:sort data-type="text" select="@id"/>
        
<xsl:sort data-type="number" select="@id"/>
        
<!-- sorts upercase and lowercase letters firs -->
        
<xsl:sort case-order="upper-first" select="@id"/>
        
<xsl:sort case-order="lower-first" select="@id"/>
        
<!-- xsl:element generates elements in time of processing. -->
        
<!-- there are new element created around the value-->
        
<xsl:for-each select="//text">
            
<xsl:element name="{@size}">
                
<xsl:value-of select="."/>
            
</xsl:element>
        
</xsl:for-each>
        
<!-- xsl:attribute generates elements in time of processing. 
        It creates attribute in the element in which it is enclosed.  
-->
        
<!-- result is <TD style="color:green">green</TD>
        attribute is created dynamicly
-->
        
<TD>
            
<xsl:attribute name="style">
                
<xsl:text>color:</xsl:text>
                
<xsl:value-of select="."/>
            
</xsl:attribute>
            
<xsl:value-of select="."/>
        
</TD>
        
<!-- Copy and copy-of constructs are used for nodes copying. 
        Copy element copies only the current node without children and attributes, 
        while copy-of copies everything. 
-->
        
<xsl:copy-of select="."/>
        
<xsl:copy/>
        
<!-- The xsl:copy element may have a use-attribute-sets attribute. 
        In this way attributes for copied element can be specified. 
-->
        
<!-- result is <h1 align="center" style="color:red">GREETING</h1> 
        add attributes
-->
        
<xsl:copy use-attribute-sets="H1">
            
<xsl:value-of select="."/>
        
</xsl:copy>
        
<xsl:attribute-set name="H1">
            
<xsl:attribute name="align">center</xsl:attribute>
            
<xsl:attribute name="style">color:red</xsl:attribute>
        
</xsl:attribute-set>
        
<!-- xsl:if instruction enables conditional processing.-->
        
<!-- position function and last function -->
        
<xsl:for-each select="entry">
            
<xsl:value-of select="@name"/>
            
<xsl:if test="not (position()=last())">
                
<xsl:text></xsl:text>
            
</xsl:if>
        
</xsl:for-each>
        
<!-- xsl:choose element is used for selection between several possibilities.  -->
        
<xsl:choose>
            
<xsl:when test="SUMMARY">
          
</xsl:when>
            
<xsl:otherwise>
          
</xsl:otherwise>
        
</xsl:choose>
        
<!-- How to find out that some text starts with a number.  -->
        
<xsl:if test="starts-with(translate(., '0123456789', '9999999999'), '9')">  
        
</xsl:if>
        
<!-- demonstrates the default behaviour of xsl:number element -->
        
<!-- Numbering of individual chapter elements depends on position of the chapter element.  -->
        
<!-- Each level of chapters is numbered independently. -->
        
<xsl:for-each select="//chapter">
            
<TR>
                
<TD>
                    
<xsl:number/>
                    
<!-- using level="multiple" will result a more reasonalbe answer -->
                    
<xsl:number level="multiple"/>
                
</TD>
                
<TD>
                    
<xsl:value-of select="./text()"/>
                
</TD>
            
</TR>
        
</xsl:for-each>
        
<!-- xsl:number inserts formated numbers into output.  -->
        
<!-- The format is given with format attribute. -->
        
<!-- The attribute starts with format identificator followed by separator characters. -->
        
<xsl:number value="position()" format="1. "/>
        
<xsl:number value="position()" format="001. "/>
        
<xsl:number value="position()" format="a# "/>
        
<xsl:number value="position()" format="i: "/>
        
<xsl:number value="position()" format="I... "/>
        
<!-- examples of formatting of multilevel numbers. -->
        
<xsl:number level="multiple" format="1.A.a "/>
        
<xsl:number level="multiple" format="I-1-a:"/>
        
<!-- different ways of setting xsl:variable -->
        
<!-- setting xsl:variable -->
        
<!-- call the variable -->
        
<xsl:variable name="totalChapters">
            
<xsl:value-of select="count(//chapter)"/>
        
</xsl:variable>
        
<xsl:value-of select="$totalChapters"/>
        
<!-- or -->
        
<xsl:variable name="totalChapters" select="count(//chapter)"/>
        
<xsl:value-of select="$totalChapters"/>
        
<!-- setting xsl:param. -->
        
<xsl:param name="totalChapters">
            
<xsl:value-of select="count(//chapter)"/>
        
</xsl:param>
        
<xsl:value-of select="$totalChapters"/>
        
<!-- or -->
        
<xsl:param name="totalChapters" select="count(//chapter)"/>
        
<xsl:value-of select="$totalChapters"/>
        
<!-- A stylesheet can contain several variables of the same name. -->
        
<!-- demonstrates a way how to recover the value of global variable which has the same name as a local one. -->
        
<xsl:variable name="text">Chapter</xsl:variable>
        
<xsl:for-each select="//chapter">
            
<TR>
                
<TD>
                    
<xsl:variable name="text">
                        
<xsl:choose>
                            
<xsl:when test="position() = 1">First chapter</xsl:when>
                            
<xsl:when test="position()=last()">Last chapter</xsl:when>
                            
<xsl:otherwise>
                                
<xsl:value-of select="$text"/>
                            
</xsl:otherwise>
                        
</xsl:choose>
                    
</xsl:variable>
                    
<xsl:value-of select="$text"/>
                
</TD>
            
</TR>
        
</xsl:for-each>
        
<!-- Parameters for a template can be passed with xsl:with-param element. -->
        
<!-- If the template contains a xsl:param element with the same name as name attribute of xsl:with-param, 
        this value is used.  
-->
        
<xsl:apply-templates select=".">
            
<!-- this param will be passed -->
            
<xsl:with-param name="type">odd</xsl:with-param>
        
</xsl:apply-templates>
        
<xsl:apply-templates select="."/>
        
<xsl:template match="number">
            
<xsl:param name="type">even</xsl:param>
            
<xsl:value-of select="."/>
            
<xsl:text> (</xsl:text>
            
<xsl:value-of select="$type"/>
            
<xsl:text>)</xsl:text>
        
</xsl:template>
        
<!-- A variable can hold a result tree fragment. -->
        
<!-- The operations permitted on a result tree fragment are a subset of those permitted on a node-set. -->
        
<!-- An operation is permitted on a result tree fragment only if that operation would be permitted on a string 
        (the operation on the string may involve first converting the string to a number or boolean).  
-->
        
<!-- In particular, it is not permitted to use the /, //, and [] operators on result tree fragments. 
        When a permitted operation is performed on a result tree fragment, 
        it is performed exactly as it would be on the equivalent node-set. 
-->
        
<xsl:variable name="A1">
            
<xsl:copy-of select="//TABLE[1]"/>
        
</xsl:variable>
        
<xsl:variable name="A2">
            
<xsl:copy-of select="//TABLE[2]"/>
        
</xsl:variable>
        
<xsl:template match="/">
            
<xsl:copy-of select="$A2"/>
            
<xsl:copy-of select="$A1"/>
            
<xsl:copy-of select="$A2"/>
        
</xsl:template>
        
<!-- important difference in variable value specification -->
        
<!-- 1st and 3rd are valued first, 2nd and 4th are text -->
        
<xsl:with-param name="path1" select="//AAA/CCC/DDD"/>
        
<xsl:with-param name="path2">//AAA/CCC/DDD</xsl:with-param> 
        
<xsl:variable name="var1" select="//AAA/CCC/text()"/>
        
<xsl:variable name="var2">//AAA/CCC/text()</xsl:variable> 
        
<!-- Functions number transforms its argument into a number -->
        
<!-- string conversion -->
        
<!-- conversion of boolean values true and false. -->
        
<xsl:value-of select="number()"/>  
        
<xsl:value-of select="number(false())"/>  
        
<xsl:value-of select="number(true())"/>  
        
<xsl:value-of select="number(5 > 7)"/>  
        
<!-- subtraction and multiplication uses common syntax -->
        
<!-- Division syntax is less usual. Slash / is used in patterns 
        and so keyword div is used instead 
-->
        
<!-- Operator mod returns the remainder from a truncating division. -->
        
<xsl:value-of select="//number[1] + //number[2]"/>  
        
<xsl:value-of select="//number[3] - //number[4]"/>  
        
<xsl:value-of select="//number[5] * //number[6]"/>  
        
<xsl:value-of select="//number[5] div //number[6]"/>  
        
<xsl:value-of select="//number[5] mod //number[6]"/>  
        
<!-- Function sum() sums all numbers in selected nodes -->
        
<xsl:value-of select="sum(//number)"/>  
        
<!-- only odd ones -->
        
<xsl:value-of select="sum(//number[text() mod 2 = 1])"/>  
        
<!-- Functions ceilng(), floor() and round() transform floating point 
        numbers into integers in the specified way. 
-->
        
<xsl:value-of select="floor(.)"/>  
        
<xsl:value-of select="ceiling(.)"/>  
        
<xsl:value-of select="round(.)"/>  
        
<!-- Function string() transforms its argument into string.
        This function is not usualy directly used in stylesheets as it is in most cases called by default 
-->
        
<xsl:value-of select="string(number($A))"/>  
        
<!-- Test, if element value is a number -->
        
<xsl:if test="string(number(.))='NaN'"> is not a number</xsl:if>  
        
<!-- strings are arguments of boolean() function. 
        A string is true if and only if its length is non-zero. 
        node-sets as arguments for boolean() function.  
-->
        
<xsl:value-of select="boolean(text())"/>  
        
<!-- text transformed into numbers and then subjected to boolean() function.  -->
        
<xsl:value-of select="boolean(number(text()))"/>  
        
<!-- The boolean value of "0" is true if "0" is a string, but false if "0" is a number. -->
        
<xsl:value-of select="boolean(//text[text()='0'])"/>
        
<xsl:value-of select="boolean(number((//text[text()='0'])))"/>  
        
<!-- compares "0" as a string and as a number.  -->
        
<xsl:value-of select="boolean(/)"/>  
        
<xsl:value-of select="boolean(//text)"/>  
        
<xsl:value-of select="boolean(//text[23])"/>  
        
<!-- The not function returns true if its argument is false, and false otherwise.  -->
        
<xsl:value-of select="car[not(@checked)]"/>  
        
<xsl:value-of select="car[@checked]"/>  
        
<!-- Functions true() and false() are useful, 
        when some conditions are tested during programming. 
-->
        
<xsl:if test="true()">
            
<xsl:text>true </xsl:text>
        
</xsl:if>
        
<xsl:if test="not(false())">
            
<xsl:text>not false</xsl:text>
           
</xsl:if> 
           
<!-- The lang function returns true or false depending on 
           whether the language of the context node as specified by xml:lang attributes 
           is the same as or is a sublanguage of the language specified by the argument string. 
-->
           
<xml:lang="de">
             
<text xml:lang="cs">a</text>
             
<text xml:lang="en">and</text>
             
<text>und</text>
           
</P>
           
<xsl:choose>
               
<xsl:when test='lang("cs")'>
                
<xsl:text>Czech: </xsl:text>
               
</xsl:when>
               
<xsl:when test='lang("en")'>
                
<xsl:text>English: </xsl:text>
               
</xsl:when>
               
<xsl:when test='lang("de")'>
                
<xsl:text>German: </xsl:text>
               
</xsl:when>
            
</xsl:choose> 
        
<!-- The string-length function returns the number of characters in the string. -->
        
<xsl:value-of select="string-length(.)"/>  
        
<!-- The normalize-space function returns the argument string with white space normalized 
        by stripping leading and trailing whitespace 
        and replacing sequences of whitespace characters by a single space. 
-->
        
<xsl:value-of select="string-length(normalize-space(.))"/>  
        
<!-- The translate function returns the first argument string with occurrences of characters 
        in the second argument string replaced by the character at the corresponding position 
        in the third argument string. 
-->
        
<xsl:value-of select="translate(//text,'egos','EGOS')"/>
        
<xsl:value-of select="translate(//text,'se','d')"/>  
        
<!-- The position function returns a number equal to the context position and 
        the last function returns a number equal to the context size from the expression evaluation context. 
-->
        
<xsl:if test="position()=1">
              
<xsl:value-of select="name()"/>
        
</xsl:if> 
        
<xsl:apply-templates select="//AAA[last()]//CCC"/>
        
<xsl:value-of select="last()"/>  
        
<!-- The count function returns the number of nodes in the argument node-set. -->
        
<xsl:value-of select="count(//AAA)"/>  
        
<xsl:value-of select="count(//CCC[text()])"/> 
        
<xsl:value-of select="count(//doc/*) - count(following::ref)"/>  
        
<!-- The id function selects elements by their unique ID -->
        
<xsl:value-of select="id('body')/text"/>  
        
<xsl:value-of select="id('text1')"/>  
        
<xsl:for-each select="//*[@*]">  
        
</xsl:for-each>
        
<!-- Several id's can be provided at once -->
        
<xsl:apply-templates select="id('intro body end')"/>
        
<xsl:value-of select="id('intro body end')"/>  
        
<xsl:value-of select="id('in bod end')"/>  
        
<!-- An example of id function usage.  -->
        
<xsl:apply-templates select="id(@id)">
               
<xsl:with-param name="nmbr">
                
<xsl:value-of select="position()"/>
               
</xsl:with-param>
            
</xsl:apply-templates> 
        
<!-- Functions name, local-name, and namespace-uri() are used to 
        get informations about element and attribute names and namespaces.  
-->
        
<!-- xml source is on: http://www.zvon.org/xxl/XSLTutorial/Output/example53_ch13.html -->
        
<xsl:value-of select="name()"/>  
        
<xsl:value-of select="local-name()"/>  
        
<xsl:value-of select="namespace-uri()"/>  
        
<!-- The xsl:output element allows stylesheet authors to specify how they wish the result tree to be output.-->
        
<!-- The html output method should not perform escaping for the content of the script and style elements  -->
        
<xsl:output method="html"/>  
        
<xsl:output method="xml"/>  
        
<!-- The encoding attribute specifies the preferred encoding to be used. --> 
        
<xsl:output method="html" encoding="UTF-8"/>  
        
<xsl:output method="html" encoding="ISO-8859-1"/> 
        
<!-- The text output method outputs the result tree by 
        outputting the string-value of every text node in the result tree in document order 
        without any escaping. Look at the source in your browser to see the output.  
-->
        
<xsl:output method="text"/>  
        
<!-- The current function returns a node-set that has the current node as its only member. -->
        
<!-- For an outermost expression (an expression not occurring within another expression), 
        the current node is always the same as the context node.  
-->
        
<xsl:value-of select="./@name"/>  
        
<xsl:value-of select="current()/@name"/>
        
<!-- However, within square brackets the current node is usually different from the context node. -->
        
<xsl:apply-templates select="BBB[./@name='first']"/>  
        
<xsl:apply-templates select="BBB[current()/@name='first']"/>  
        
<!-- Function generate-id generates id conforming to XML spec. -->
        
<xsl:value-of select="generate-id(//AAA) "/>  
        
<xsl:value-of select="generate-id(//*[1]) "/>  
        
<xsl:value-of select="generate-id(//AAA[1]) "/>  
        
<xsl:attribute name="id">
               
<xsl:value-of select="generate-id()"/>
        
</xsl:attribute> 
        
<xsl:attribute name="{name()}">
                    
<xsl:value-of select="."/>
               
</xsl:attribute> 
           
<!-- Other stylesheets can be imported (xsl:import) or included (xsl:include) into a stylesheet.. 
           Importing a stylesheet is the same as including it except that definitions and template rules 
           in the importing stylesheet take precedence over template rules and definitions in the imported stylesheet. 
-->
           
<!-- included xsl could be included in other xsl -->
           
<xsl:import href="id3.xsl"/>
           
<xsl:include href="id2.xsl"/> 
           
<xsl:value-of select="$id2"/>  
           
<xsl:value-of select="$id3"/>  
           
<!-- pay attention to how to add value to html attribute -->
           
<!-- result is <h3 style="color:red">red</h3> -->
           
<xsl:template match="AAA">
             
<h3 style="color:{.}">
              
<xsl:value-of select="."/>
             
</h3>
           
</xsl:template>
          
<!-- always finish xsl analyze, then match in xml, so when include or import the sequence may result 
          overite rules 
-->
          
<!-- You can use xsl:apply-imports element to get information from an imported template, 
          whose behaviour you are changing. 
-->
          
<!-- xsl-apply-imports works only for templates imported with xsl:import, 
          not for templates included with xsl:include  
-->
          
<!-- works -->
          
<xsl:import href="id2.xsl"/>
        
<xsl:template match="/*/*">
             
<EM>
              
<xsl:apply-imports/>
             
</EM>
        
</xsl:template>
          
<!-- not work -->
          
<xsl:include href="id2.xsl"/>
        
<xsl:template match="/*/*">
             
<EM>
              
<xsl:apply-imports/>
             
</EM>
        
</xsl:template>
          
<!-- overwrite -->
          
<xsl:import href="id2.xsl"/>
        
<xsl:template match="/*/*">
             
<EM>
              
<xsl:value-of select="name()"/>
             
</EM>
        
</xsl:template>
          
<!-- Import precedence is more important than priority precedence.  -->
          
<!-- overwrite still work first, although included things maybe have higher priority,
          but it could be overwrite by who include it. 
-->
    
</xsl:template>
</xsl:stylesheet>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值