Introduction
XSLT is a language used to convert XML document structure. It is EXtensible Style Language Extensions Trans Abbreviation for formations.
XSLT is similar to CSS in HTML, but is more powerful than CSS.
According to the W3C specification, XSLT was first designed to help convert XML documents into other documents.
But with the deepening of application, XSLT is not only used to convert XML to HTML or other document formats, but has become a language used to convert the structure of XML documents.
Elements and Attributes of XSLT Provides declarations for processing XML data. You can use the XSLT vocabulary to extract the content of other documents, create new elements and attributes, and in many more cases An organic combination of these two methods.
XSLT 1.0 transformation requires two operation files (XML source document and XSLT stylesheet file) to generate a result document.
In the new XSLT 2.0 standard, it is allowed to combine the two into one.
XSLT can match each element and its attributes with HTML or XHTML, thereby achieving the correct display and output of document content.
An XSLT style sheet is a correct and valid XML document that follows the XML rule format, and its extension is .xsl.
The syntax for using XSLT style sheets in XML documents is as follows:
<?xml-stylesheet type="text/xsl" href="XSL样式表路径"?>
2 Application of XML conversion through XSLT
This example applies XSLT extensible style to convert the XML file into files in other formats before outputting.
This example converts XML through XSLT, and the output is the data in the XML file after XSLT style conversion.
First make an XML declaration and specify the document as an XML document.
Note that the XSLT style sheet itself is an XML document, so it also conforms to the rules of XML documents.
Then declare the XSLT style sheet and declare the prefix of the XSLT namespace (xsl:stylesheet).
There are two ways to write the prefix of the XSLT namespace: xsl:stylesheet and xsl:transform.
Its meaning and function are exactly the same, except that the former one is more commonly used. Specify the XSLT namespace through the xmlns:xsl attribute.
Then define the rules of the template, which is encapsulated using the xsl:template element. The Math attribute specifies a pattern that describes what input the rule matches.
Finally realize the conversion of XML documents.
Three codes
1. The code to create the cdcatalog.xsl file is as follows:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="artist"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
2. The code to create cdcatalog.xml is as follows:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> </catalog>
Four runs result
The above is the detailed content of Example code for how to transform XML via XSLT. For more information, please follow other related articles on the PHP Chinese website!