XSL Tutorial learning notes

Source: Internet
Author: User
Tags expression xmlns xsl xsl file xslt xslt processor

One. Hello World Try:

=============hello.xml:=================

<?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
<FistNode>
,World
</FistNode>

===============test.xsl:==================

<?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:template match="/">
                                  <title>First node</title>
                      <body>
               <p>
                  <xsl:value-of select="/FistNode"/>
               </p>
           </body>
            </xsl:template>
</xsl:stylesheet>

Run Result:

Hello,world

Two. XSLT element list

Element name Meaning
Xsl:apply-imports Calling the re-template rule
Xsl:apply-template Instructs the XSLT processor to find the appropriate template based on the type and context of each selected node
Xsl:attribute Create an attribute node and attach it to an OUTPUT element
Xsl:attribute-set To define a named property set
Xsl:call-template Calling templates by name
Xsl:choose Provides multiple conditional tests related to <xsl:otherwise> elements and <xsl:when> elements
Xsl:comment Generate comments in output
Xsl:copy Copy the current node from the source to the output
xsl:copy-of Insert subtree and result tree fragments into the result tree
Xsl:decimal-fomat Declares a decimal format used to control the format pattern interpretation used by the Formate-number function
Xsl:element Create an element of the specified name in the output
Xsl:fallback To invoke a template that provides the appropriate alternative behavior for the behavior of new elements encountered
Xsl:for-each Apply templates repeatedly to each node in the contact set
Xsl:if Allow simple conditional template fragments
Xsl:import Import another XSLT file
Xsl:include Contains another XSL file
Xsl:key Declares a named key for the key () function in an XPath expression
Xsl:message Send a text message to a message buffer or dialog box
Xsl:namespace-alias Replace the prefix associated with a given namespace with a different prefix
Xsl:number Inserts and formats a number into the result tree
Xsl:otherwise Provides multiple conditional tests related to <xsl:choose> elements and <xsl:when> elements
Xsl:ouput Specifies the options to use when serializing the result tree
Xsl:param Declares a named parameter that is used in a <xsl:stylesheet> element or in a <xsl:template> element. Allow default values to be set
Xsl:preserve-space Leave blank in document
Xsl:sort Specify the sorting criteria for the <xsl:for-each> or <xsl:apply-template> selected Node list
Xsl:stylesheet Specifies the document element of the XSLT file. Document element contains other XSLT elements
Xsl:template Defines a template that can be reused to generate the required output for a node of a particular type and context
Xsl:text Generate text in output
Xsl:transform Perform the same functions as <xsl:stylesheet>
xsl:value-of Inserts the value of the selected node as text
Xsl:variable Specifies the value of the binding in an expression
Xsl:when Provides a number of test conditions related to <xsl:choose> elements and <xsl:otherwise>
Xsl:with-param Passing parameters to a template
Xsl:strip-space Remove white space from the document

Three Xsl:template and Xsl:apply-template

The template consists of 2 parts: matching mode and execution.

The match pattern specifies which template to use Xsl:template

execution specifies how to output the Xsl:apply-template

1.xsl:template syntax

<xsl:template natch=pattern name=qname priority=number mode=qname>
<!—执行内容à
</xsl:template>

Name: Unique names for easy referencing

Match: Original node mode and name must have one

Priority:-9-9 of the numbers

Mode: Allows you to process an element multiple times, each time producing a different result. There can be no mode with match.

eg

==========test.xml=============

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <department>
        <name language="Chinese">Tech Department</name>
        <leader>Zhang San</leader>
        <quantity>20</quantity>
        <target>software develop</target>
    </department>
    <department>
        <name language="Chinese">Sale Department</name>
        <leader>Li Si</leader>
        <quantity>20</quantity>
        <target>Sale work</target>
    </department>
</company>

1. Match all the Departments

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

2. Match all the leaders and all the number of elements

<xsl:template match= "Leader|quantity" ></xsl:template>

3. Match all parent nodes to the leader element of the department

<xsl:template match= "Department//leader" ></xsl:template>

3. Match root node

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

2.xsl:apply-templates

Grammar:

<xsl:apply-templates Select=node set-expression mode=qname></xsl:apply-templates>

Used to indicate which node is specifically handled by the template. You can understand that the call to the child function select is used to select the exact node name. Xsl:apply-templates always in the xsl:template.

1. The template matches the entire document (the root node), which, when executed, processes all departmental elements under the root node.

<xsl:template match=”/”>
    <xsl:apply-template select=”department”/>
</xsl:template>

2. Template Matching Department node. All child elements under the department will be processed at the exact time of execution

<xsl:template match=”para”>
    <p><xsl:apply-template /></p>
</xsl:template>

Four xsl:value-of

Used to write element text from the document to the output document.

1. Extract the name of each leader in the department

<xsl:template match=”department”>
    < xsl:value-of select=”leader”/>
</xsl:template>

Experience: <xsl:template match= "department" > denotes a range, then value-of and apply-template clearly indicate which elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.