Introducing XSLT

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:5

lượt xem

Introducing XSLT

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

XML là một cách tuyệt vời để đại diện cho dữ liệu trong một định dạng xách tay, nhưng XML không chứa thông tin về định dạng dữ liệu như thế nào để hiển thị. Các Extensible Stylesheet Language Transformation (XSLT) cho phép bạn kiểm soát các định dạng của dữ liệu XML, và có thể được sử dụng để chuyển đổi dữ liệu XML để định dạng thích hợp để hiển thị nó như là một tài liệu. An XSL stylesheet-còn được gọi là một tập tin XSLT-là một mẫu có chứa các quy tắc mô tả làm thế...

Chủ đề:

Nội dung Text: Introducing XSLT

  1. Introducing XSLT XML is a great way to represent data in a portable format, but XML doesn't contain information on how to format that data for display. The Extensible Stylesheet Language Transformation (XSLT) allows you to control the formatting of XML data, and may be used to transform XML data to a format suitable for displaying it as a document. An XSL stylesheet-also known as an XSLT file-is a template that contains the rules that describe how the data in the XML file is to be formatted for viewing. The XML and XSLT files are processed together by an XSLT processor. The rules defined in the XSLT file are applied to the data in the XML file, and the final result is output by the XSLT processor. Microsoft Internet Explorer contains an XSLT processor, and you'll see examples in this section that display the results of processing an XML and XSLT file in Internet Explorer. Note Internet Explorer actually comes with a default XSLT file, which causes XML files to use different colors for the parts of the XML document and to be displayed with + and - icons to expand and collapse nested XML data. Listing 16.9 shows an example XSLT file named CustomersStylesheet.xsl, which you'll find in the xml directory. Later, you'll see how to apply this XLST file to an XML file containing customer data. Listing 16.9: CUSTOMERSSTYLESHEET.XSL Customers Customer:
  2. As you can see, the CustomersStylesheet.xsl file contains HTML tags and xsl tags. The xsl tags are instructions that indicate how XML is to be transformed. You can reference this XSLT file in an XML file; the rules in the XSLT file are then applied to the data in the XML file. You'll see how to do that later in this section. Let's take a closer look at the lines in the CustomersStylesheet.xsl file. This file starts with the following line, which indicates that the file uses XML version 1.0: The next lines are xsl tags: The first line uses the xsl:stylesheet tag and specifies that the namespace is to be used. The second line uses the xsl:template tag and sets the match attribute to /, which specifies that the entire XML document is to be selected and used by the XSLT processor, starting at the root node. Note You can set the match attribute to any XPath expression. For example, if you set match to //Customers, then all the Customers nodes would be selected. The next lines are HTML tags, which start the HTML part of the file, define a header, and start the body: Customers
  3. The next lines are the real meat of the XSLT file and use the xsl:for-each tag to iterate over the Customers nodes: Customer: The xsl:value-of tag is used to retrieve the element values from the XML file. For example, retrieves the CustomerID element. The remaining lines close up the HTML and the xsl parts of the file: The XSLT file contains only the rules to transform XML data; we still need to provide the XML data itself. Listing 16.10 shows an XML file named CustomersUsingStylesheet.xml, which contains XML data for two customers. Listing 16.10: CUSTOMERSUSINGSTYLESHEET.XML ALFKI Alfreds Futterkiste 12209 Germany 030-0074321
  4. ANATR Ana Trujillo Emparedados y helados 05021 Mexico (5) 555-4729 This listing is identical to the Customers.xml file shown earlier in Listing 16.7 but with the addition of the following line that references the CustomersStylesheet.xsl file: This line causes the XSLT processor to read and apply the rules in the CustomersStylesheet.xsl file to the XML data in the CustomersUsingStylesheet.xml file. Figure 16.5 shows how CustomersUsingStylesheet.xml looks when viewed with Internet Explorer. To view this file, right-click on CustomersUsingStylesheet.xml in Windows Explorer and select Open With ➣ Internet Explorer. Figure 16.5: Viewing CustomersUsing-Stylesheet.xml in Internet Explorer When you open CustomersUsingStylesheet.xml, Internet Explorer's XSLT processor opens the CustomersStylesheet.xsl file and applies the rules in it to the XML data in CustomersUsingStylesheet.xml. The output generated by the XSLT processor is then displayed in Internet Explorer. I've only touched on using XSLT in this section. XSLT is a very powerful language that contains many functions you can use to format your XML data. For more information, see Mastering XSLT by Chuck White (Sybex, 2002). You can also learn more at the
  5. World Wide Web Consortium's Web site at; just look for XSLT in the table of contents.
Đồng bộ tài khoản