XML formatted by XSL (XML to custom node collapsed display)

Source: Internet
Author: User
Tags processing instruction xsl xsl file

Introduction

Sometimes we want to see the system generated XML files (such as the XML format of the project file), if the file structure is simple, our browser seems to be more convenient, but as the XML Schema complex becomes a headache, write a program alone to show and a little fuss. XSL is then used to embody its value, and of course XSL is not just for display, but for more purposes to generate another format of XML or other format files from one format. This article only speaks of formatting.

The XSL common element xsl:template defines the transformation template for the matching node, and the attribute match= "XPath expression" to define the element that the template matches

The template that matches the root node is defined as follows

<xsl:template match= "/" ></xsl:template>
The Xsl:for-each loop shows the conversion of the select= "XPath expression" selection node (similar to the foreach statement in the programming language),

The following example selects the sub-elements below pets and loops through the names of the child elements:

<xsl:for-each select= "/pets/*" ><xsl:value-of select= "name ()"/></xsl:for-each>
xsl:if element Condition display nodes (like if statements in programming languages) Note that the less than sign greater than sign is used &lt; and &gt; instead
<xsl:if test= "@weight &lt; Ten "><i>its weight is less than km</i></xsl:if>
Xsl:choose Multi-branch conditional display (similar to a switch statement in a programming language)
<xsl:choose ><xsl:when test= "name () = ' pig '" ><i>this is a pig</i></xsl:when><xsl:o Therwise>  <i>this is not a pig</i></xsl:otherwise></xsl:choose>
XSL:VALUE-OF displays the value of the selected node or attribute

Select child node Price

<xsl:value-of select= "Pets/*/price"/>

Select attribute Weight

<xsl:value-of select= "pets/*/@weight"/>
Xsl:attribute constructing the attributes of an XML node

Used to add attributes to a node, such as:

<font><xsl:attribute name= "Color" ><xsl:value-of select= "pets/*/@color"/></xsl:attribute> </font>

Output <font color= "Red" ></font>

Xsl:apply-templates Application Template

If the XML file structure is complex, you can define multiple templates and then use the <xsl:apply-templates> tag to apply the template, Xsl:apply-templates can specify properties select= "XPath" To select the template to apply, or select to indicate the template that selects the current node.

XSL all elements

Elements Description IE N
Element ">apply-imports Apply template rules from the imported style sheet. 6.0
Element ">apply-templates Applies a template to the current element or to the child elements of the current element. 5.0 6.0
Element ">attribute Adds an attribute to an element. 5.0 6.0
Element ">attribute-set Creates a named set of properties. 6.0 6.0
Element ">call-template Invokes a specified template. 6.0 6.0
Element ">choose Used in conjunction with <when> and <otherwise> to express multiple condition tests. 5.0 6.0
Element ">comment Create a comment node in the result tree. 5.0 6.0
Element ">copy Creates a backup of the current node (No child nodes and attributes). 5.0 6.0
Element ">copy-of Creates a backup of the current node (with child nodes and attributes). 6.0 6.0
Element ">decimal-format Defines the characters and symbols to use when converting numbers to strings through the Format-number () function. 6.0
Element ">element Creates an element node in the output document. 5.0 6.0
Element ">fallback If the processor does not support an XSLT element, specify a backup code to run. 6.0
Element ">for-each Iterates through each node in the specified set of nodes. 5.0 6.0
Element ">if Contains a template that is applied only when a specified condition is true. 5.0 6.0
Element ">import Used to pour content from one style sheet into another style sheet. 6.0 6.0
Element ">include Include the contents of one style sheet in another style sheet. 6.0 6.0
Element ">key Declares a named key. 6.0 6.0
Element ">message Write a message to the output (for error reporting). 6.0 6.0
Element ">namespace-alias Replace namespaces in the style sheet with different namespaces in the output. 6.0
Element ">number The integer position of the current node is measured and the number is formatted. 6.0 6.0
Element ">otherwise Specifies the default action for the <choose> element. 5.0 6.0
Element ">output Defines the format of the output document. 6.0 6.0
Element ">param Declares a local or global parameter. 6.0 6.0
and element ">preserve-space Used to define elements that preserve whitespace. 6.0 6.0
Element ">processing-instruction Generates a processing instruction node. 5.0 6.0
Element ">sort Sort the results. 6.0 6.0
and element ">strip-space Defines an element that should remove whitespace characters. 6.0 6.0
and element ">stylesheet Defines the root element of a style sheet. 5.0 6.0
Element ">template The rule that is applied when the specified node is matched. 5.0 6.0
Element ">text Generates a text node from a style sheet. 5.0 6.0
and element ">transform Defines the root element of a style sheet. 6.0 6.0
Element ">value-of Extracts the value of the selected node. 5.0 6.0
Element ">variable Declares a local or global variable. 6.0 6.0
Element ">when Specifies the action of the <choose> element. 5.0 6.0
Element ">with-param Specifies the value of the parameter that needs to be passed into a template. 6.0 6.0

XSL Introduction mode IE browser supports direct introduction of XSL files directly into XML
<?xml-stylesheet type= "text/xsl" href= "icorenested.xsl"?>

Chorme Browser cannot load local XSL file directly

Reference can Chrome be made to perform an XSL transform on a local file

Source

SOURCE download

Reference

Can Chrome be made to perform an XSL transform on a local file?

Nested collapsible blocks

XML formatted by XSL (XML to custom node collapsed display)

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.