Danh mục

Introducing XSLT

Số trang: 5      Loại file: pdf      Dung lượng: 37.87 KB      Lượt xem: 15      Lượt tải: 0    
Hoai.2512

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (5 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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ế...
Nội dung trích xuất từ tài liệu:
Introducing XSLTIntroducing XSLTXML is a great way to represent data in a portable format, but XML doesnt containinformation on how to format that data for display. The Extensible Stylesheet LanguageTransformation (XSLT) allows you to control the formatting of XML data, and may beused 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 thatdescribe 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 rulesdefined in the XSLT file are applied to the data in the XML file, and the final result isoutput by the XSLT processor. Microsoft Internet Explorer contains an XSLT processor,and youll see examples in this section that display the results of processing an XML andXSLT 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 youllfind in the xml directory. Later, youll see how to apply this XLST file to an XML filecontaining customer data.Listing 16.9: CUSTOMERSSTYLESHEET.XSL Customers Customer: As you can see, the CustomersStylesheet.xsl file contains HTML tags and xsl tags. Thexsl tags are instructions that indicate how XML is to be transformed. You can referencethis XSLT file in an XML file; the rules in the XSLT file are then applied to the data inthe XML file. Youll see how to do that later in this section.Lets take a closer look at the lines in the CustomersStylesheet.xsl file. This file startswith 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 thehttp://www.w3.org/1999/XSL/Transform namespace is to be used. The second line usesthe xsl:template tag and sets the match attribute to /, which specifies that the entire XMLdocument 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: CustomersThe next lines are the real meat of the XSLT file and use the xsl:for-each tag to iterateover the Customers nodes: Customer: The xsl:value-of tag is used to retrieve the element values from the XML file. Forexample, 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 providethe XML data itself. Listing 16.10 shows an XML file namedCustomersUsingStylesheet.xml, which contains XML data for two customers.Listing 16.10: CUSTOMERSUSINGSTYLESHEET.XML ALFKI Alfreds Futterkiste 12209 Germany 030-0074321 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 withthe addition of the following line that references the CustomersStylesheet.xsl file:This line causes the XSLT processor to read and apply the rules in theCustomersStylesheet.xsl file to the XML data in the CustomersUsingStylesheet.xml file.Figure 16.5 shows how CustomersUsingStylesheet.xml looks when viewed with InternetExplorer. To view this file, right-click on CustomersUsingStylesheet.xml in WindowsExplorer and select Open With ➣ Internet Explorer.Figure 16.5: Viewing CustomersUsing-Stylesheet.xml in Internet ExplorerWhen you open CustomersUsingStylesheet.xml, Internet Explorers XSLT processoropens the CustomersStylesheet.xsl file and applies the rules in it to the XML data inCustomersUsingStylesheet.xml. The output generated by the XSLT processor is thendisplayed in Internet Explorer.Ive only touched on using XSLT in this section. XSLT is a very powerful language thatcontains 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 theWorld Wide Web Consortiums Web site at www.w3.org; just look for XSLT in the tableof contents.

Tài liệu được xem nhiều: