XML is formatted using XSL (XML is collapsed to custom nodes), xmlxsl
Introduction
Sometimes we want to see the XML files generated by the system (such as Project files in XML format). If the file structure is simple, our browser looks more convenient, however, the complexity of XML schema makes it a headache. It is a little difficult to write a program separately for presentation. In this case, XSL is used to reflect its value. Of course, XSL is not only used to display, but also to generate another XML or other format file from one format. This article only describes formatting.
Solutions to problems encountered during the process
See XLT formatting XML (2)
<Xsl: template match = "/"> </xsl: template>Xsl: for-each loop display select = "xpath expression" select node conversion (similar to the foreach statement in programming languages ),
In the following example, child elements under pets are selected and the names of child elements are displayed cyclically:
<xsl:for-each select=”/pets/*”><xsl:value-of select=”name()”/></xsl:for-each>
Xsl: if element condition display node (similar to the if statement in programming languages). Note that & lt; and & gt; should be used to replace nodes smaller than numbers.
<xsl:if test=”@weight < 10”><i>its weight is less than 10 km</i></xsl:if>
Xsl: choose multi-branch condition display (similar to the switch statement in programming languages)
<xsl:choose ><xsl:when test=”name() = ‘pig’”><i>this is a pig</i></xsl:when><xsl:otherwise> <i>this is not a pig</i></xsl:otherwise></xsl:choose>
Xsl: value-of displays the value of the selected node or attribute.
Select sub-node price
<xsl:value-of select=”pets/*/price”/>
Select weight
<xsl:value-of select=”pets/*/@weight”/>
Xsl: attribute: Construct the attributes of an xml node.
Used to add attributes to a node, for example:
<font><xsl:attribute name=”color”><xsl:value-of select=”pets/*/@color”/></xsl:attribute></font>
<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> label Application template, xsl: apply-templates you can specify the select = "xpath" attribute to select the template of the application, or do not specify select to select the template of the current node.
All XSL Elements
| Element |
Description |
IE |
N |
| Apply-imports |
Apply the template rules from the imported style sheet. |
6.0 |
|
| Apply-templates |
Apply the template to the current element or the child element of the current element. |
5.0 |
6.0 |
| Attribute |
Add attributes to an element. |
5.0 |
6.0 |
| Attribute-set |
Create a named property set. |
6.0 |
6.0 |
| Call-template |
Call a specified template. |
6.0 |
6.0 |
| Choose |
Used in collaboration with <when> and <otherwise> to express multi-condition tests. |
5.0 |
6.0 |
| Comment |
Create a comment node in the result tree. |
5.0 |
6.0 |
| Copy |
Create a backup for the current node (no sub-nodes and attributes ). |
5.0 |
6.0 |
| Copy- |
Create a backup (with sub-nodes and attributes) for the current node ). |
6.0 |
6.0 |
| Decimal-format |
Defines the characters and symbols used to convert a number to a string using the format-number () function. |
6.0 |
|
| Element |
Create an element node in the output document. |
5.0 |
6.0 |
| Fallback |
If the processor does not support an XSLT element, it requires a piece of spare code to run. |
6.0 |
|
| For-each |
Traverses each node in the specified node set. |
5.0 |
6.0 |
| If |
Contains a template, which is applied only when a specified condition is set. |
5.0 |
6.0 |
| Import |
It is used to add the content of a style sheet to another style sheet. |
6.0 |
6.0 |
| Include |
Include the content in one style sheet to another. |
6.0 |
6.0 |
| Key |
Declare a named key. |
6.0 |
6.0 |
| Message |
Write a message to the output (used for error reporting ). |
6.0 |
6.0 |
| Namespace-alias |
Replace the namespace in the style sheet with different namespaces in the output. |
6.0 |
|
| Number |
Measure the integer position of the current node and format the number. |
6.0 |
6.0 |
| Otherwise |
Specifies the default action of the <choose> element. |
5.0 |
6.0 |
| Output |
Define the output document format. |
6.0 |
6.0 |
| Param |
Declare a local or global parameter. |
6.0 |
6.0 |
| Preserve-space |
Defines elements that retain white space. |
6.0 |
6.0 |
| Processing-instruction |
Generate a processing command node. |
5.0 |
6.0 |
| Sort |
Sort the results. |
6.0 |
6.0 |
| Strip-space |
Defines the elements that should be deleted from white spaces. |
6.0 |
6.0 |
| Stylesheet |
Defines the root element of the style sheet. |
5.0 |
6.0 |
| Template |
The rule applied when the specified node is matched. |
5.0 |
6.0 |
| Text |
Use a style sheet to generate a text node. |
5.0 |
6.0 |
| Transform |
Defines the root element of the style sheet. |
6.0 |
6.0 |
| Value- |
Extract the value of the selected node. |
5.0 |
6.0 |
| Variable |
Declare local or global variables. |
6.0 |
6.0 |
| When |
Specifies the action of the <choose> element. |
5.0 |
6.0 |
| With-param |
Specifies the parameter value of a template. |
6.0 |
6.0 |
XSL Introduction: IE browser supports direct introduction of XSL files in XML
<?xml-stylesheet type="text/xsl" href="icoreNested.xsl"?>
The Chorme browser cannot directly load local XSL files
See Can Chrome be made to perform an XSL transform on a local file
<Html>
Source code download
Reference
Can Chrome be made to perform an XSL transform on a local file?
Nested foldable Block