XSLT

Dari Wikipedia Bahasa Melayu, ensiklopedia bebas.

Gambaran mengenai unsur asas dan aliran proses bagi Extensible Stylesheet Language Transformations.
Gambaran mengenai unsur asas dan aliran proses bagi Extensible Stylesheet Language Transformations.

Extensible Stylesheet Language Transformations (XSLT) merupakan bahasa berasaskan XML yang digunakan bagi transformasi dokumen XML.

XSLT direka bagi menukar dokumen XML kepada dokumen XML lain. Dokumen asal tidak bertukar; sebaliknya, dokumen baru dicipta berasaskan kandungan sedia ada.[1] Dokumen baru boleh dioutput bersiri oleh pemprosesor dalam sintak XML piwaian atau dalam format lain seperti HTML atau teks biasa.[2] XSLT sering kali digunakan bagi menukar data antara dua skema XML berlainan atau menukar data XML kepada dokumen HTML atau XHTML bagi laman web, atau kepada format perantaraan yang boleh ditukar kepada dokumen Format Dokumen Mudah-alih (Portable Document Format- PDF documents.


Jadual isi kandungan

[Sunting] XSLT dan XPath

XSLT relies upon the W3C's XPath language for identifying subsets of the source document tree, as well as for performing calculations. XPath also provides a range of functions, which XSLT itself further augments. This reliance upon XPath adds a great deal of power and flexibility to XSLT.

XSLT 2.0 relies on XPath 2.0; both specifications were published on the same date. Similarly, XSLT 1.0 works with XPath 1.0.

[Sunting] Examples

Sample of incoming XML document

<?xml version="1.0" ?>
<persons>
  <person username="JS1">
    <name>John</name>
    <family_name>Smith</family_name>
  </person>
  <person username="ND1">
    <name>Nancy</name>
    <family_name>Davolio</family_name>
  </person>
</persons>

[Sunting] Contoh 1 (menukar XML kepada XML)

This XSLT stylesheet provides templates to transform the XML document:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/">
       <root> <xsl:apply-templates/> </root>
</xsl:template>

<xsl:template match="//person">
        <name username="{@username}">
           <xsl:value-of select="name" />
        </name>
</xsl:template>

</xsl:stylesheet>

Its evaluation results in a new XML document, having another structure:

<?xml version="1.0" encoding="UTF-8"?>
<root>
      <name username="JS1">John</name>
      <name username="ND1">Nancy</name>
</root>

[Sunting] Contoh 2 (menukar XML kepada XHTML)

Contoh sheet stail XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/persons">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head> <title>Testing XML Example</title> </head>
        <body>
                <h1>Persons</h1>
                <ul> 
                <xsl:apply-templates select="person">
                        <xsl:sort select="family_name" />
                </xsl:apply-templates>
                </ul>
        </body>
        </html>        
</xsl:template>

<xsl:template match="person">
        <li>
                <xsl:value-of select="family_name"/>, 
                <xsl:value-of select="name"/>           
        </li>        
</xsl:template>

</xsl:stylesheet>

XHTML output that this would produce (whitespace has been adjusted here for clarity):

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>xsl wiki ex.</title> </head>
<body>
        <h1>Persons</h1>
        <ul>
           <li>Davolio, Nancy</li>
           <li>Smith, John</li>
        </ul>
</body>
</html>
  Imej:Xslt ex2.gif
How the XHTML appears when rendered in a web browser.


[Sunting] Pautan luar

For implementations, see XML template engine.
Documentation
XSLT 1.0 W3C Recommendation
XSLT 2.0 W3C Recommendation
Zvon XSLT 1.0 Reference
XSL Concepts and Practical Use by Norman Walsh
Tutorial from developerWorks by IBM (1 hour)
Zvon XSLT Tutorial
XSLT Tutorial by W3 Schools
Quick tutorial
What kind of language is XSLT?
XSLT and Scripting Languages
Mailing lists
The XSLT mailing list hosted by Mulberrytech
Blogs
A commentary, news, and evangelism weblog devoted to XSLT
Books
XSLT by Doug Tidwell, published by O’Reilly (ISBN 0-596-00053-7)
XSLT Cookbook by Sal Mangano, published by O’Reilly (ISBN 0-596-00974-7)
XSLT 2.0 Programmer's Reference by Michael Kay (ISBN 0-764-56909-0)
XSLT 2.0 Web Development by Dmitry Kirsanov (ISBN 0-13-140635-3)
XSL Companion, 2nd Edition by Neil Bradley, published by Addison-Wesley (ISBN 0-201-77083-0)
XSLT and XPath on the Edge (Unlimited Edition) (ISBN 0-7645-4776-3) by Jeni Tennison, published by Hungry Minds Inc, U.S. (ISBN 0-7645-4776-3)
XSLT & XPath, A Guide to XML Transformations (ISBN 0-13-040446-2) by John Robert Gardner and Zarella Rendon, published by Prentice-Hall (ISBN 0-13-040446-2)
XSLT code libraries
EXSLT is a widespread community initiative to provide extensions to XSLT.
FXSL is a library implementing support for Higher-order functions in XSLT. FXSL is written in XSLT itself.