Introduction to XSL Syntax

Source: Internet
Author: User
Tags html tags sort xml parser xmlns xpath xsl xsl file xsl stylesheet
Grammar

The example is already on the top, and we can look at the grammatical results and the key points here:

The first thing to notice is that the XSL file itself is an XML file, so at the beginning of the XSL file there is the same declaration as the XML file. The standard organization for this XML has defined many tags (elements) for XSL, which is the combination of these tags and HTML tags. In an XSL file, you must have the following line of code:

<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl"

Here, the xsl:stylesheet is the root element of the XSL file, containing all the typographic styles in the root element, which are grouped by the style sheet; xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" This sentence is mainly used to illustrate that the XSL stylesheet is the XSL that is used by the consortium, and that the set value is the URL address of the XSL specification.

In fact, the "http://www.w3.org/TR/WD-xsl" Here is a namespace (namespace), which we've covered in the previous introduction to XML Schema syntax. This is a standard name space. The keywords "stylesheet", "template", "For-each" and so on are defined in this name space.

Of course in Xsl:stylesheet can also set other properties, the other properties are:

1. Default-space: Determines whether to keep white space in the XML file, reserved only when the value is "default".

2. Indent-result: Determines whether to keep white space in the XSL file and keep the value "yes".

3. Language: Sets the scripting language used in the XSL file.

Then we see the following code in the above code:

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

This actually represents the processing of an XML document by the XSL parser, which starts with the root node (match= "/", where "/" means the root node), traverses the XML document, and extracts the relevant content from the XML document based on the specific code. Here, the value of match is a more complicated problem. What it actually means is getting a specific set of nodes from an XML document (the XML document can be viewed as a tree structure, which is described in detail in the XML Parser analysis later). Here, we use a few simple examples to illustrate the value of match for a property.

For example, the following line of code:

<xsl:template match= "/" >

This line of code means to tell the XSL parser that the current node to be processed is the content under the root node ("/" to represent the root node), in which case the match content is written to conform to XPath semantics. We'll take a detailed description of XPath in a later section.

Let me cite one more example:

<xsl:template match= "Shoppingcart/item"

This line of code is to match the item element under the ShoppingCart element. Regardless of the location of the ShoppingCart under the XML document Structure tree. For example, a section of an XML document is like this.

<shoppingcart>
<item>
<itemNo> 3333 </itemNo>
<itemName> Dragon-Slayer knife </itemName>
</item>
<item>
<itemNo> 4444 </itemNo>
<itemName> Parting Hook </itemName>
</item>
</shoppingcart>

So what it matches is,
<item>
<itemNo> 3333 </itemNo>
<itemName> Dragon-Slayer knife </itemName>
</item>
<item>
<itemNo> 4444 </itemNo>
<itemName> Parting Hook </itemName>
</item>

And the following example:

<xsl:template match= "/shoppingcart/item"

Represents all item elements under the ShoppingCart node that matches only the root node of the XML document. That is, if the ShoppingCart is not directly under the root node, it does not conform to the matching criteria.

Look at one more example:

<xsl:template match= "price[@unit = ' USD ']" >

This example shows that the node to match is the element price with the unit attribute, and the unit value must be "USD". For example, a piece of XML code is like this.

<price>
<unit> USD </unit>
<amount> </amount>
</price>
<price>
<unit> RMB </unit>
<amount> $ </amount>
</price>

So what it matches is:

<price>
<unit> USD </unit>
<amount> </amount>
</price>

In fact, there are a number of symbols used to represent matching rules, which we'll cover in detail in an introduction to XPath syntax. Now know such a general concept is OK.

We use the <xsl:template match= "concrete matching expression" > This statement found some node set, we need to find a specific element or element attribute value from this set, then what statement? is to look for specific content with a xsl:value-of select = "" Statement.

For example, in the following example <xsl:value-of select= "name"/> this line of code is the content that represents the name element in the positioning XML document. There may be multiple name elements in the specified set, and if we need to enumerate them all, we need to use the statement Xsl:for-each select = "", note that there is a concept of scope, which means Xsl:for-each select = "" This statement is executed in a specified collection space. For example, the following code in the example above

The <xsl:for-each select= "words" Here are the "words" that are found in the set space specified in <xsl:template match= "set of network terms".

Also, we need to be aware that in the above code, a statement appears

<xsl:apply-templates select= "Network language collection"/>

What does it mean, it's actually equivalent to a procedure call in C + +, and when the XSL parser executes to that statement, it looks in the code for <?xml:namespace prefix = xsl/><xsl:template match= "Network term Collection" > So in the example program above, the following code can be seen as the implementation of the process.


Take it as a process call, put <xsl:template match= "Network language Collection"

.......
</xsl:template>

As an implementation of the process, it helps us to understand the process of XSL parser execution. Here match= "Network term set" can be understood as a parameter passed to the process, which indicates that the set scope of the process implementation body is the node set space ("Network term set") that match matches.

What do we do if we want to sort the elements in a table? In the example above, for example, we need to sort by name. Quite simply, rewrite it to the following form:

<xsl:for-each select= "word" order-by= "+ Name", where "+" means in descending order; "-" means in ascending order. "Order-by" is a keyword in the XSL syntax.

What if we just want to take a few lines out of the list? For example, we just want to remove the line named "Dinosaur", see the following code:

Here is a new syntax: <xsl:template match= "Network language Collection"
<table border= "1" >
<xsl:for-each select= "Words" order-by= "-name" >
<xsl:if test= ". [name = ' Dinosaur '] ">
<tr>
<td> <xsl:value-of select= name/> </td>
<td> <xsl:value-of select= "meaning"/> </td>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>



It means if ". if [name = ' Dinosaur '] is true, the statement inside the paragraph is executed, and if False (false) it is not executed. It is basically the same as the concept of an if statement in C + +.

We used the <xsl:value-of select= ""/> to take out the values of an element, but what do we do with the value of one of the attributes of an element? Take the following form:

<xsl:value-of select= "element name/@ property name"/>

For example, a piece of XML code is like this:

Wang Shuo Web site = "www.wangshuo.com" "The place of famous writers and renowned critics

We can use <xsl:value-of select= "Wang Shuo/@ url"/> to get the value "www.wangshuo.com".

The above includes most of the basic syntax for XSL, with a more detailed and complete introduction to the latest documents related to the WWW.W3C.ORG/TR, which can be found under the sun.

Based on the analysis above, we can see that XSL actually adopts the idea of a transformation, which eventually transforms the XML document into another document that can be used for output, while the CSS does not have any transformation action, and no new code is generated throughout the process. In addition, the 90% style specification in XSL is defined in CSS, but there are still some effects that CSS cannot describe and must be used with XSL. These features include the substitution of text, the way in which text is displayed, the sort of document content, and so on, which are unique to XSL. Furthermore, XSL conforms to the syntax of XML, and the CSS syntax is self-contained.

Choosing a style sheet also takes into account the degree to which different browsers support the style sheet. The latest versions of IE5 and Netscape support CSS, but the degree of support is limited. So far, IE5 has not yet fully supported CSS1, even the supporting parts have many errors, for CSS2 also provides only partial support. Netscape is already superior to IE5 in support of CSS, and it employs a new generation of Raptor/gecko engine technology that has been able to fully support CSS1, but the support plan for CSS2 is not yet clear. But for XSLT, only IE5 support, NETSCAPE5 does not support.





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.