Creating reports based on xsl transformations (xslt). How XSLT works


Senior Technology Writer

Someone emailed you an XSLT file and you don't know how to open it? Maybe you've found an XSLT file on your computer and are wondering what it is? Windows may tell you that you cannot open it, or in the worst case, you may encounter a corresponding error message associated with the XSLT file.

Before you can open an XSLT file, you need to find out what kind of file the XSLT file extension is.

Tip: Incorrect XSLT file association errors can be a symptom of other underlying issues within your Windows operating system. These invalid entries can also produce associated symptoms such as slow Windows startups, computer freezes, and other PC performance issues. Therefore, it is highly recommended that you scan your Windows registry for invalid file associations and other issues related to a fragmented registry.

Answer:

XSLT files have a Data File, which is primarily associated with an XSL Transform File.

Additional types of files may also use the XSLT file extension. If you are aware of any other file formats that use the XSLT file extension, please contact us so we can update our information accordingly.

How to open your XSLT file:

The fastest and easiest way to open your XSLT file is to double-click on it. IN in this case Windows will choose the necessary program to open your XSLT file.

In case your XSLT file does not open, it is very likely that you do not have the required application program installed on your PC to view or edit files with XSLT extensions.

If your PC opens the XSLT file, but it's the wrong program, you'll need to change your Windows registry file association settings. In other words, Windows associates XSLT file extensions with the wrong program.

Install optional products - FileViewPro (Solvusoft) | | | |

XSLT Multipurpose Internet Mail Extensions (MIME):
  • mime application/xslt+xml
XSLT File Analysis Tool™

Not sure what type your XSLT file is? Do you want to get precise information about a file, its creator and how it can be opened?

Now you can instantly get all necessary information about the XSLT file!

The revolutionary XSLT File Analysis Tool™ scans, analyzes and reports detailed information about an XSLT file. Our patent-pending algorithm quickly analyzes the file and provides detailed information within seconds in a clear, easy-to-read format.†

In just a few seconds, you'll know exactly what type of XSLT file you have, the application associated with the file, the name of the user who created the file, the file's security status, and other useful information.

To begin your free file analysis, simply drag and drop your XSLT file inside the dotted line below, or click "Browse My Computer" and select your file. The XSLT file analysis report will be shown below, right in the browser window.

Drag and drop your XSLT file here to start analyzing

View my computer »

Please also check my file for viruses

Your file is being analyzed... please wait.

This section does not pretend to be language documentation, but only briefly, step by step, explains how to create your own XSLT template.

The scheme described below has been successfully used by me for more than 3 years. At first, I was very wary of XSLT (especially when I was parsing other people’s sources), but once I understood what was what, I can’t imagine how you can work without it.

Desktop Let's determine what we need for work:
  • Input XML Document
  • XHTML template layout
  • XML parser for merging XML with XSL
My input XML document is issued by a CMS system, in which each page with material is assembled into an XML tree.

There are no restrictions on the XHTML layout. There are only certain recommendations for layout that will significantly save time on creating a template.

You can use a browser as a parser (collector) of the final document. You just need to specify the path to the template file in the XML documents:

Although, as practice has shown, this mechanism is quite buggy (I had to use IE). It is better to use XML parsing tools of the language in which the CMS system is written. I use Parser (in fact, my entire system runs on it).

Input XML Document First, let's look at the input XML document. In order to use XSL you need to have a complete understanding of its structure.

I'm using the following scheme:


/>


Start
/


News
news

The above example scheme does not claim to be optimal. For one reason or another, it is convenient for me. But first things first.

- XML ​​file header. Must go strictly from the beginning of the file. It specifies the version of the XML language used and the document encoding. I usually work in windows-1251 (so far it’s more convenient), but in theory UTF-8 is better.

The root element of the document (you can come up with your own name). Attributes:

  • Lang - document language. Needed to create multilingual templates.
  • Id - identifier of the current section.
- table of languages ​​used on the site.
- block of navigation elements:
- main navigation block (main site structure):
- element of the site structure. Attributes:
  • Id - section identifier.
  • Parent_id - identifier of the parent section.
  • Is_published - whether the section is published.
  • Dir - uri address of the section. It is used to generate complete addresses.
  • Section - section type. Used if it is necessary to split the menu into main and service.
- content block.
My CMS uses a modular structure: all content of the site consists of two types of modules:
  • Html - text module. Static modules that are filled in by the site editor.
  • Com - component module. Dynamic modules that form various software modules CMS: news, statistics, search blocks etc.
XSL templates contain block markup into which modules can be placed. I use simple numbering to define blocks.

When assembling a page, the CMS simply outputs to all modules that are used on the page in the form:
Attributes:

  • Id - module identifier.
    Container - purpose block (in which template block to be displayed).
    Sorting - output order in the block.
    Type - type:
    • Com - module-component
      Html - text module.
    Method - data processor.
    Title - module name.
    I practically don’t use DTD (only in the most general form):




















    ]>

    It can be inserted directly into an XML document. Right after .

    Preparing an XHML template An XSL template is created based on an XHTML template (a certain typical website page). The XHTML page code must be valid.

    Let's look at the process of creating a template step by step.

    After checking the validity of the XHML page of your template, to make your own work easier, indicate the position of all dynamic blocks in it:

    • Menus (and other navigation elements).
    • Information blocks of the page - the place in the template in which the site modules will be displayed.
    • Title/page title.
    The best way to do this is using regular HTML comments:
    ...
    Site administration
    • Start
    • News
    • Sections

    All sorts of news

    Basics of describing XSL templates All XSL template files look like this:


    template data

    Where: - defines the XML document type and encoding. I'm using UTF-8 (don't ask why).
    - the beginning and end of the XSL document.
    - the beginning and end of the template for the element element.

    Templates can be divided into three types:

    • element. Applies automatically to all element elements.
    • - a template describing the rules for transforming the element element in mode1 . This way you can describe different rules for processing element elements.
    • - template named template-name . Has no binding to any XML document element.
    If elements of the same type can occur in different parts of the XML document structure (for example, in an XML document generated by the system, the item element is used everywhere and has different meanings), then the “structural address” of such an element can be specified in the template:

    At the same time, the order of application of templates is hierarchical, i.e., first the template is applied to the root element, and then to its children, i.e. if we called the handler for navigation , then to call the handler for navigation/sections/item we just need to specify the address sections/item . Template folder structure In order to store several modules on one site, it is necessary to somehow think through the structure of their storage in folders. At the same time, it is more convenient to split the templates into modules across several xsl files. This approach will allow you to reuse them in the future when creating new templates.

    In the simplest version, you can create an xsl directory and store everything there.

    • template_folder - directory with template files. You can call it by the name of the template, for example my_template.
    • dtd - files describing the main entities. May be useful.
    • lang - message templates for different languages ​​(if the site uses several of them).
    • mod - module templates.
    First we need to create a directory xsl/my_template and in it, a file layout.xsl that looks like this:



    Where:
    - template for the /node() element (root). Instead of /node() you can specify //document , because it is our root node.

    Copy all XHTML code inside the block

    This template will automatically be applied to the entire XML document. In our case, the XSL transformation will replace all XML code with the XHTML code of your template.

    Next, you need to create a file template.xsl in the XSL directory (where template is the name of your template), in which we place the following code:


    Where:

    Directive for importing an external XSL file (processed by an XSL processor) from specified file. The path to the file is relative.

    Creating a template for the main navigation menu Our previous template does not have any dynamics, because... simply replaces the entire XML output document with our template code.

    The next step is to create a template for the menu.

    The site navigation menu is built on the basis of its structure, presented in the XML document in the following form:


    Start
    /


    News
    news

    The current partition is determined by two parameters:

    • The id attribute of the root document element is always equal to the id of the current section.
    • The hit attribute of the item element - if there is one, then this means we are on the “main page of the section”.
    Accordingly, in order to display the site menu, you need to create a template for the elements:
    • sections is the root element of the menu.
    • item - menu element.
    At the same time, it is necessary to take into account that item elements can contain other item elements if the section has subsections: 1. Create a navigation.xsl file in the xsl/my_template directory that looks like this:


    2. Insert the code of our menu from the file layout.xsl into the template:
    • Start
    • Sections
    • News
    3. ...and in its place in the layout.xsl file we insert a call to our menu template:

    Where:
    select="navigation/sections" - relative (relative to the current) path-address of the element. In this case, all navigation/sections elements will be processed.

    mode="global_menu" - use a template with the global_menu mode. We need this in case we also need to display the service menu, separately, or “breadcrumbs”, or something else based on the same navigation branch.

    4. Plus, add a directive to the layout.xsl file to import the navigation.xsl template file: 5. Next, create another template in the navigation.xsl file to process menu items:



    Where:
    - calling a template by name. In this case, the template has no binding to the element, i.e. is called randomly.

    - insertion and output of the value of the title element of the current element. If you put the @ symbol in front of the element name in the parameter, the attribute values ​​of the current element will be displayed.

    6. Let's slightly change the sections template:

    Where:
    - processing of all item elements of the sections element. In this case, the item elements of the item elements themselves (sections/item/item ) will not be processed, i.e. Only one level of section menu is displayed.

    We have moved the processing of item elements (menu items) into a separate template. At the same time, we also added a call to another template:

    This template will generate normal uri links for our menu items. More about him a little later.

    7. Now we need to complete the menu so that it takes into account which section is the current one. To do this, we'll have to add conditional processing to our item element template:






    Here we come across a new construction:



    ...which, in fact, specifies the conditional processing of XML elements. As a parameter we set the condition:

    In our case, this is the condition for the equality of the ID attributes of the root element ( document ) and the current element ( item ), which determines whether the element is current.

    Inside the block is what is displayed if the condition is met. In a block - if the condition is not met.

    8. Now, let's look at the href_attribute template:



    /


    /


    Here we come across the xsl:attribute statement. It allows you to create attributes for the elements within which it is called. In our case, we call it from the a element, respectively, it will create an href attribute for it, i.e. address.

    The instruction specifies a processing cycle for all elements that satisfy the condition. In our case, we select ancestor-or-self::item - the axis of elements from the root element to the current one in the chain. In our case, this allows us to select dir nodes for the entire chain, i.e. build the full address of the current partition node.



    Chapter 1

    XSLT Basics

    Welcome to the world of Extensible Stylesheet Language Transformations (XSLT). This book will serve as your guide to the vast world of XSLT, which is expanding in unpredictable ways every minute. We want this world to become your world. There's quite a lot of ground to cover because XSLT is being used in very interesting places and in very interesting ways these days. In this book you will see how it all works.

    XSLT itself is a means of processing and formatting the content of XML documents. XML has already become very popular, now it's XSLT's turn. XML gives you the ability to structure data in documents, and XSLT allows you to work with the content of XML documents - manipulate the content and create other documents (for example, when sorting XML employee database records or when saving data into an HTML document, as well as when formatting detailed data).

    You can work with the content of XML documents by writing your own program to interface with XML parser applications, but you must write the program code yourself. With XSLT, you can perform these same types of tasks without any programming. Instead of writing code to process the content of XML documents in Java, Visual Basic, or C++, you can simply use XSLT to specify what you want to do, and the XSLT processor will do the rest. This is exactly what XSLT is designed for, and it is emerging as a key player in the XML world.

    XSL = XSLT + XSL-FO

    XSLT itself is actually part of a larger specification, the Extensible Stylesheet Language, or XSL. XSL is designed to specify the precise, down to the millimeter, format of documents.

    A much larger specification than XSLT, the formatting portion of XSL is based on special formatting objects—this portion of XSL is often called XSL-FO (or XSL:FO, or XSLFO). XSL-FO is a complex topic because styling documents using formatting objects can be a very confusing process. In fact, XSLT was originally added to XSL to make it easier to transform XML documents into documents based on XSL-FO formatting objects.

    This book focuses on XSLT, but also covers an introduction to XSL-FO, including how to use XSLT to transform documents into XSL-FO form; after all, XSLT was first introduced to make working with XSL-FO easier. This chapter will begin with an overview of both XSLT and XSL-FO.

    Brief historical background

    XSL was created by a consortium World Wide Web Consortium (W3C, www.w3.org) - a coalition of groups originally led by Tim Berners-Lee. The W3C is the committee that produces specifications, such as the specification for XSL used in this book. Specifications are what make XML and XSL what they are.

    The W3C originally developed the granddaddy of XML, SGML (Standard Generalized Markup Language), in the 1980s, but it was too complex to gain widespread popularity, and in reality XML (like HTML) represents is a simplified version of SGML. The W3C also created the Document Style Semantics and Specification Language (DSSSL) to work with SGML - and just as XML was derived from SGML, XSL is based on the original DSSSL. As the W3C states: “The model XSL uses for displaying documents on screen is based on many years of work on a complex ISO style language called Document Style Semantics and Specification Language (DSSSL).”

    However, the original part of XSL, that is, XSL-FO, was still not simple enough to find widespread adoption, so XSLT was introduced as a means of making it easier to convert XML documents into XSL-FO form. As it turns out, XSLT was adopted because it is a complete transformation language that allows you to work with the content of XML documents without writing code, converting these documents into other XML documents, HTML, or other text-based formats. The great success of XSLT surprised even the W3C.

    XSLT-XSL Transformations

    XSLT allows you to work directly with the content of XML documents. For example, you might have a huge XML document containing all the baseball stats for the last season, but you might only be interested in the stats for pitchers. To extract just this data, you can write a program in Java, Visual Basic, or C++ that works with the XML parsers. Parsers are special software packages that read XML documents and pass all the document data sequentially into your code. You can then create a new XML document, pitchers.xml, containing only the pitcher data.

    This method works, but it requires quite a lot of programming and spending a lot of time, including testing. XSLT was designed to solve problems like these. XSLT can be read by XSLT processors that process XML documents: all you need to do is create an XSLT style sheet that defines the rules you need to apply to transform one document into another. No programming is required, which is an undoubted advantage in the eyes of many people, even experienced programmers. In the baseball example, you would write an XSLT style sheet that defines the required actions and let the XSLT processor do the rest.

    In addition to converting one XML document to another XML document, you can also transform XML documents into different types of documents - such as HTML, rich text (RTF) documents, documents that use XSL-FO, and others. You can also transform XML documents into other XML-based languages ​​- such as MathML, MusicML, VML, XHTML and others - all without programming.

    In many cases, XSLT can work similarly to a database language such as SQL (Structured Query Language, a well-known database access language), in that it allows you to extract required data from XML documents much like a statement SQL retrieves data from the database. XSLT is sometimes even referred to as the SQL of the Web, and if you are familiar with SQL, this will give you some idea of ​​the limitless possibilities of XSLT. For example, using an XSLT style sheet, you can extract a subset of data from an XML document, create a table of contents for a large document, find all elements that meet a certain condition (for example, suppliers with a certain index), and so on. And all this - in one step!

    XSL-FO: XSL Formatting Objects

    Another part of XSL is XSL-FO, the formatting part of the XSL language, which we'll also be introduced to in this book. With XSL-FO, you can define how data is represented in XML documents, down to margin sizes, fonts, alignment, header and subheader sizes, and page width. There are a lot of things to think about when formatting an XML document, and as such, XSL-FO is significantly more extensive than XSLT.

    On the other hand, due to its excessive complexity, XSL-FO is not very popular and cannot be compared to XSLT in this regard. There aren't many programs that support XSL-FO, and none of them come close enough to the standard. Just as the most common use of XSLT is to convert XML to HTML, the most common use of XSL-FO is to convert XML to text in the Portable Data Format (PDF) format used in Adobe Acrobat. An example of such a conversion is given at the end of this chapter and also in Chapter 11.

    W3C Specifications

    The W3C produces specifications for both XML and XSL, and these are the ones we'll be working with throughout this book. W3C specifications are not called standards because, according to international agreements, standards can only be created by government-authorized committees. Instead, the W3C starts with a release requirements(requirements) for the new specification. The requirements are a list of goals and overviews of the specification, but the specification itself has not yet been written at this point. Next, the W3C releases specifications: first in the form work projects(working drafts), which can be commented on by all interested parties, then in the form recommendations for candidates(candidate recommendations), which are still subject to change; and finally, in the form of final recommendations(recommendations), which can no longer be changed.

    The following list shows the XSLT-related W3C specifications that we will use in this book and where they can be found:

    XSLT 1.1 working draft at www.w3.org/TR/xslt11. This is a working draft of XSLT 1.1 and will not be further developed prior to recommendation - the W3C plans to add all XSLT 1.1 functionality to XSLT 2.0;

    XSLT 2.0 requirements at www.w3.org/TR/xslt20req. The W3C has released a group of targets for XSLT 2.0, including additional support for XML schemas;

    XPath 1.0 specification at www.w3.org/TR/xpath. XPath is used to find and point to specific sections and elements in XML documents so that they can be manipulated;

    XPath 2.0 requirements at www.w3.org/TR/xpath20req. XPath is currently being updated to add additional support for XSLT 2.0.

    XSLT versions

    The XSLT specifications have been developed much more actively than the specifications for all XSL. The XSLT 1.0 recommendation was finally adopted on November 16, 1999, and this version is the major version of XSLT today.

    Then a working draft of XSLT 1.1 appeared and, although it was initially considered as a prologue to a new recommendation, a number of W3C staff began working on XSLT 2.0 - and after some time the W3C decided to stop working on the XSLT 1.1 recommendation. This means that the XSLT 1.1 working draft will not be developed further - it will forever remain as a working draft and will never become a recommendation. In other words, it will not be released official version 1.1 for XSLT.

    However, the W3C also states that it plans to incorporate much of what was done in the XSLT 1.1 working draft into XSLT 2.0, and so in this book I will briefly review the XSLT 1.1 working draft. I will be sure to mark material as "XSLT 1.1 Working Draft Only" when discussing new material introduced in the XSLT 1.1 Working Draft.

    The following are the changes to XSLT 1.0 made in the XSLT 1.1 working draft; Please note that this list is provided here for reference only, as most of the material is unlikely to mean anything to you yet:

    The result tree fragment data type supported in XSLT 1.0 has been removed;

    The inference method can no longer arbitrarily add namespace nodes because the namespace setting process is applied automatically;

    Support for XML Base has been added;

    Multiple output documents are now supported using the element

    ;

    Element

    can now have parameters;

    Extension functions can now be defined using the function

    ;

    Extension functions can now return external objects that do not match any XPath data types.

    This book discusses the XSLT 1.0 recommendation. and a working draft of XSLT 1.1. In furtherance of this topic, the W3C released requirements for XSLT 2.0, which are also discussed in the book under the name XSLT 2.0. The following list provides an overview of the goals of XSLT 2.0:

    Add additional support for working with XML schema content using XSLT;

    Simplify working with strings;

    Simplify working with XSLT;

    Improve support for various languages;

    Support increased efficiency processor.

    Although XSLT 2.0 will not be released in its final form for some time, I will review what is known about it when discussing topics related to it. For example, the W3C's successor to HTML is the XML-based language XHTML. XSLT 1.0 and the XSLT 1.1 working draft do not directly support transformations from XML to XHTML, so we will have to create this transformation from scratch. However, such support is included in XSLT 2.0, and I will note this fact when discussing XHTML.

    Thus, we have considered a brief overview of the topic; let's get to work. XSL is designed to work with XML documents, so first I want to look at the structure of XML documents. You'll be working with XML documents, but XSL stylesheets themselves are also XML documents, which is something to keep in mind when writing them. This book assumes that you have knowledge of both XML and HTML.

    XML Documents

    You should understand how XML documents work, so check this section to see if you're ready to get started. Let's look at an example XML document:

    Here's how the document works: I started with an XML processing instruction (all XML processing instructions begin with ), meaning that it's using XML version 1.0, the only version currently defined, and a UTF-8 character encoding, meaning it's using an eight-bit compressed version of Unicode:

    Welcome to the wild and woolly world of XML.

    Then I create new tag(tag) with name

    . You can use any name for the tag, not necessarily DOCUMENT; All that is required is that the name must begin with a letter or underscore (_) and be followed by letters, numbers, underscores, periods (.), or hyphens (-), but not spaces. IN XML tags always start with< и заканчиваются >.

    XML documents are formed from XML elements; the latter begin with an opening tag such as

    (followed by the element's content, if any, such as text or other elements), and ends with a closing tag paired with the opening tag (this begins with the characters Welcome to the wild and woolly world of XML

    This book doesn't cover DTD declarations, but it does show that an element

    - the root one, and the elements can, firstly, be located inside it, and secondly, contain text.

    XML documents can contain all sorts of hierarchies, with one element nested within another element and so on through many levels of nesting. You can also assign attributes to elements, for example:

    , where the attribute contains the value "". Using such attributes, it is convenient to store additional data about elements. It is also possible to include comments in XML documents that explain certain elements with text inside the and tags.

    Below in Listing 1.1 is an example XML document

    , which uses these capabilities to store data about the planets Mercury, Venus and Earth - such as mass, day length, density, distance from the Sun, etc. We'll work with this document throughout the book because it contains most of the XML features you'll need in a compact form. Listing 1.1. planets.xml .0553 58.65 1516 .983 43.4 .815 116.75 37l6 .943 66.8 1 2107 1 128.4

    You also need to understand a number of XML definitions adopted in this book:

    CDATA. Simple character data (that is, text that does not contain any markup);

    ID. A valid XML name, which must be unique (that is, not used in any other ID type attributes);

    IDREF. Contains the value of the ID attribute of some element, usually different from the element with which the current element is associated;

    IDREFS. Multiple element identifiers (IDs), separated by spaces;

    NAME Symbol . Letter, place, period, hyphen, underscore, or colon;

    NAME. An XML name that must begin with a letter, underscore, or colon, possibly followed by additional name characters;

    NAMES. A list of names separated by a delimiter character;

    NMTOKEN. A token formed from one or more letters, numbers, hyphens, underscores, colons and periods;

    NMTOKENS. Multiple valid XML names in a list, separated by a delimiter character;

    NOTATION. Notation name (which must be declared in the DTD);

    PCDATA. Parsed character data. PCDATA does not contain any markup, and any entity references have already been expanded in PCDATA.

    We now have an understanding of XML documents, including what a well-formed and valid document is. If you don't feel confident enough with this material yet, read another book on the topic. You can also look at some XML resources on the Web:

    http://www.w3c.org/xml. The World Wide Web Consortium's main XML Web site, the starting point for all things XML;

    http://www.w3.org/XML/1999/XML-in-10-points, “XML in 10 steps” (actually only seven) - an overview of XML;

    http://www.w3.org/TR/REC-xml. Official W3C Recommendation for XML 1.0, current (and only) version. Not too easy to read

    http://www.w3.org/TR/xml-stylesheet/. All about how style sheets and XML work;

    http://www.w3.org/TR/REC-xml-names/. All about XML namespaces;

    http://www.w3.org/XML/Activity.html. Review of current XML activities at W3C;

    Http://www.w3.org/TR/xmlschema-0/, http://www.w3.org/TR/xmlschema-1/ and http://www.w3.org/TR/xmlschema-2/ . XML Schemas, alternative DTD declarations;

    http://www.w3.org/TR/xlink/. XLinks Specification;

    http://www.w3.org/TR/xptr. XPointers Specification;

    http://www.w3.org/TR/xhtml1/. XHTML 1.0 Specification;

    http://www.w3.org/TR/xhtml11/. XHTML 1.1 Specification;

    Http://www.w3.org/DOM/.W3C Document Object Model, DOM ( Document Object Model).

    Now we have learned how to create XML documents. What are they in visual form?

    What does XML look like in a browser?

    You can directly display XML documents using a browser such as Microsoft Internet Explorer versions 5 or later. For example, if we save the XML document we created as greeting.xml and open it in Internet Explorer, it will look like shown in Fig. 1.1.

    Rice. 1.1. XML document in Internet Explorer


    In Fig. 1.1 you can see the entire XML document. There is no formatting, the XML document looks exactly the same in Internet Explorer as if you printed it. (To display the screen shown in Figure 1.1, Internet Explorer used its default style sheet. It converts XML into Dynamic HTML, which Internet Explorer can work with.) What if we want to present the data in a different way? Let's say we want to present the data from planets.xml in an HTML document as an HTML table?

    This is where we need XSLT transformations. In this chapter we'll introduce them, and at the end of the chapter we'll look at the other side of XSL - XSL-FO.

    XSLT transformations

    XSLT has great capabilities for manipulating data in XML documents. For example, using an XSLT stylesheet, I could format data from

    to an HTML table. Style sheets contain rules set for transforming an XML document, and much of the book is devoted to creating style sheets and explaining how they work. Here's what an XSLT stylesheet looks like (Listing 1.2) that converts the data from planets.xml into an HTML table (we'll analyze it in Chapter 2). Listing 1.2. planets.xsl xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



    As you can see, this XSLT style sheet looks like an XML document - and for good reason, because that's what it is. All XSLT style sheets are also XML documents, and if so, they must be well-formatted XML documents. With these two documents -

    (Listing 1.1) and its associated style sheet, (Listing 1.2) - we'll work throughout the book looking at the various possible XSLT transformations.

    How do you link this style sheet to the XML document

    ? As we'll see in the next chapter, one way to do this is with an XML processing instruction that uses two attributes. The first attribute is , which should be set to "text/xml" to indicate that an XSLT stylesheet is being used. (To use another type of style sheet - cascading stylesheets (CSS), typically used with HTML - you should specify "text/css".) The second attribute is , which should be set to a URI (remember that XML does not use URLs, and URIs, Uniform Resource Identifier) ​​style sheets: .0553 58.65 1516 .983 43.4

    Now with the help processor(processor) XSLT table can be applied

    to and create a new document, . The XSLT processor creates a new file, which you can see in Fig. 1.2.

    Rice. 1.2. HTML document, cos this processor ohm XSLT


    As can be seen in Fig. 1.2, the XSLT processor reads data from

    , applies the rules from them and creates an HTML table in planets.html. This is our first example of an XSLT transformation.

    What's really going on here? We had an XML document,

    , and XSLT style sheet, . But how did they come together to create? What you need for XSLT transformation

    To perform XSLT transformation such as transform

    in , an XSLT processor is required. You can use XSLT in three ways to transform XML documents:

    Using separate programs called XSLT processors. There are several programs that perform XSLT transformations, usually based in Java, some of which we'll look at in this chapter;

    On the client side. A client program, such as a browser, can perform the conversion by reading the style sheet specified in the processing instruction

    . In particular, Internet Explorer can perform this kind of transformation;

    On the server side. A server program, such as a Java servlet, can transform the document automatically using a style sheet and send it to the client.

    In this book, we'll look at all three ways to perform XSLT transformations. Already in this chapter an overview of all these three different methods will be provided.

    Using separate XSLT processors

    Separate XSLT processors are one of the most common ways to perform XSLT transformations. There are quite a few such processors, although not all of them can handle all possible XSLT style sheets. To use the XSLT processor, simply run it from the command line (in a DOS window on Windows), specify the name of the XML source document, the name of the style sheet to use, and the name of the document you want to create.

    Below is a list of some XSLT processors available on the Internet, in alphabetical order - most (but not all) of them are free:

    4XSLT, http://Fourthought.com/4Suite/4XSLT. XSLT processor for Python;

    EZ/X, http://www.activated.com/products/products.html. Java package for both XML parsing and XSLT processing;

    iXSLT, http://www.infoteria.com/en/contents/download/index.html Command-line XSLT processor;

    Koala XSL Engine, http://www.inria.fr/koala/XML/xslProcessor. An XSLT processor for Java that uses the Simple API for XML (SAX 1.0) and the Document Object Model (DOM 1.0) API;

    LotusXSL, http://www.alphaworks.ibm.com/tech/LotusXSL. IBM's LotusXSL implements an XSLT processor in Java and can interface with APIs that conform to the Document Object Model (DOM) Level 1 Specification. The well-known XSLT processor, but now it is being replaced by Xalan 2.0;

    MDC-XSL, http://mdc-xsl.sourceforge.net. XSLT processor in C++, it can be used as a separate program;

    Microsoft XML Parser, http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp. XML parser from Microsoft, high-performance, available as a COM component. It can be used to implement XSLT support in applications;

    Sablotron, http://www.gingerall.com/charlie-bin/get/webGA/act/sablotron.act. Sablotron is a fast, compact and portable XSLT processor. On currently supports a subset of the XSLT recommendation. It can be used with C or Perl;

    SAXON, http://users.iclway.co.uk/mhkay/saxon/index.html. This XSLT processor fully implements XSLT 1.0 and XPath 1.0, as well as a number of extensions to those specifications. Note that this release also includes working draft support for XSLT 1.1;

    Transformiix, http://www.mozilla.org. Transformiix is ​​an XSLT component from Mozilla, currently partially implemented in Netscape 6.0;

    Unicorn XSLT processor (UXT), http://www.unicorn-enterprises.com. This XSLT processor supports XSLT transformations and is written in C++;

    Xalan C++, http://xml.apache.org/xalan-c/index.html. Implementation of W3C recommendations for XSLT and XML Path Language (XPath). Version famous processor Apache Xalan in C++;

    Xalan Java, http://xml.apache.org/xalan-j/index.html. Implementing W3C recommendations for XSLT and XML Path Language (XPath) in Java. Java version of the famous Apache Xalan processor. Also includes extension functions for accessing SQL databases via JDBC and much more;

    xesalt, http://www.inlogix.de/products.htmL This XSLT processor exists as a web server module (for both Apache and IIS web servers), as a Netscape 4.x extension, and as a processor command line;

    XML parser for C, http://technet.oracle.com/tech/xml/parser_c2. XSLT processor from Oracle. Supports XSLT 1.0 recommendation, designed for use with C;

    XML parser for Java, http://technet.oracle.com/tech/xml/parser_java2. XSLT processor from Oracle. Supports XSLT 1.0 recommendation, designed for use with Java;

    XML parser for PL/SQL, http://technet.oracle.com/tech/xml/parser_plsql. XSLT processor from Oracle. Supports XSLT 1.0 recommendation, designed for use with PL/SQL;

    XML:XSLT, http://xmlxslt.sourceforge.net. This XSLT parser is written in Perl. Partially implements the XSLT recommendation;

    Xport, http://www.timelux.lu. XSLT transformation processor, available as a COM object;

    XSL:P, http://www.clc-marketing.com/xslp/download.html. Modern XSLT processor;

    XT, http://www.jclark.com/xml/xt.html. XT is a well-known implementation of the XSLT recommendation in Java.

    In the following sections, we'll take a closer look at four of the XSLT processors listed above: XT, Saxon, Oracle's XSLT processor, and Xalan. They are all available for free on the Internet and can be used to test all the examples in the book. If you want to study examples, download one of these processors (the best known and widely used is the Xalan processor). To implement XSLT transformations, I will use these processors in the following discussion.

    The selected processors are Java based and require you to have Java installed. If you don't already have it installed, you can download it for free from Sun's website. Latest edition at the time of writing Java books 2 version 1.3 is available at http://java.sun.com/j2se/1.3. All you need to do is download Java for your operating system and follow the download instructions on the appropriate pages.

    While these XSLT processors require Java to run, there's no need to worry if you're not a programmer—no programming is required. Chapter 10 will include Java programming examples to show you how to create XSLT transforms in code, but XT, Saxon, Oracle's XSLT, and Xalan processors can all be run from the command line.

    If you're on Windows, there's an even easier way to use XT and Saxon - these processors come as .exe files (

    and ), which can be done directly on Windows - and you won't need Java at all. This way of working is also discussed in the book. Using the XSLT Processor in Java

    To work with the Java-based XSLT processor, simply download and unzip it and it's ready to go. Read the accompanying instructions, of course; but, as a rule, only two steps need to be carried out.

    First, you need to tell Java how to find the XSLT processor, which is stored in a Java Archive, or JAR, file. To tell Java where to look for a JAR file, you need to set the classpath environment variable to the path of that JAR file. For example, on any version of Windows, you would open a DOS window and run a command that sets the classpath variable to the path to Oracle's XSLT processor JAR file, xmlparserv2.jar, which in this case is stored in the directory

    :

    Now you can move on to the second stage - launch the XSLT processor; this will execute the Java class that supports the processor. For the Oracle XSLT processor, this class is called

    . On Windows, for example, you can go to the directory where the and files are located, and execute the class using Java like this: C:\planets>

    In this case the file

    will be converted to using . Note that this example assumes that the java.exe file, which runs the Java code, is located in your Windows directory. If it is not there, you can explicitly set its location, the Java bin directory, for example (JDK is short for Java Development Kit, and Java 2 version 1.3 installs itself by default in the directory), as in next team: C:\planets>c:\jdk1.3\bin\java oracle.xml.parser.v2.oraxsl planets.xmlplanets.xslplanets.html

    You can combine both stages (setting the variable

    and starting the XSLT processor) into one if you use the key along with Java, specifying which variable to use: C:\planets>c:\jdk1.3\bin\java -cf c:\oraclexml\lib\xmlparserv2.jar oracle .xml.parser.v2.oraxsl planets.xml planets.xsl planets.html

    These are quite long commands, which may seem overly complicated at first glance. However, there is a good reason why most XSLT processors are written in Java: Java is supported on many platforms, from Macintosh to UNIX, and therefore an XSLT processor can also run on all of these platforms.

    Of course, things get a lot easier if you're on Windows and using the compiled version of the XT processor (

    ) or Saxon(). Below is an example on Windows to accomplish the same conversion (assuming it's located in the current directory): C:\planets>

    We've covered the process briefly; Now I want to describe the four XSLT processors (XT, Saxon, Oracle's XSLT processor, and Xalan) in detail, demonstrating how to work with each of them. Please pay attention to two points: software that implements XML languages and XSL changes very quickly, so by the time you read this, some processors may already be outdated; Although all processors should support all standard XSLT, in some cases they produce different results.

    XT James Clark

    XT by James Clark can be downloaded from www.jclark.com/xml/xt.html. In addition to the XT processor itself, you will need an XML parser that will allow the XT to read your XML document. There is also a file in the XT download files

    , containing James Clark's XML parser; You can, however, use its XP parser for the same purposes by downloading it from the address www.jclark.com/xml/xp/index.html.

    My personal preference is to use the Xerces XML parser from the Apache Project, which is located at http://xml.apache.org. (At the time of writing, the current version, Xerces 1.3.0, could be downloaded from http://xml.apache.org/dist/xerces-j/ in UNIX zip format in a file

    and Windows format in the file.)

    The XT processor itself is a Java application included in the XT download package in a JAR file,

    . To use and, you must include both files in the variable, as shown in the following example for Windows (change the locations of these files accordingly): C:\>set classpath=C:\xerces-1_3_0\xerces.jar;C :\xt\xt.jar

    Then you can use the XT transform class,

    com.jclark.xsl.sax.Driver.class. You need to provide the name of the parser you want to use (in this case, org.apache.xerces.parsers.SAXParser in the archive) by setting a variable to the desired name on the command line. Here's how I use XT to do the conversion on Windows (assuming the files are contained in a directory and are located in your current directory): C:\planets>java -Dcom.jclark.xsl.sax.parser=org.apache.xerces. parsers.SAXParser com.jclark.xsl.sax.Driver planets.xml planets.xsl planets.html

    This line is quite long and the good thing is that XT is also packaged in executable file Win32

    . To work with, however, it is necessary to have installed on your computer virtual machine Java from Microsoft, Microsoft Java Virtual Machine (VM), is included with Internet Explorer. Here is an example on Windows that does the same conversion as the previous line (assuming it is located in the current directory): C:\planets>xt planets.xml planets.xsl planets.html is not located in the current directory, you can specify its location explicitly, as in the following example (if located in ): C:\planets>c:\xt\xt planets.xml planets.xsl planets.html Saxon

    Michael Kay's Saxon is one of the very first XSLT processors and can be downloaded for free from http://users.iclway.co.uk/mhkay/saxon/. All you need to do is download the saxon.zip file and extract the zip archive; the required file will be created Java JAR,

    .

    To perform an XSLT transformation, you first need to make sure that

    is in the variable. For example, on Windows, assuming that it is in , you can set the variable as follows: C:\>set classpath=c:\saxon\saxon.jar

    You can now use the Saxon XSLT class to perform the transformation,

    com.icl.saxon.StyleSheet.class : C:\planets>java com.icl.saxon.StyleSheet planets.xml planets.xsl

    By default, Saxon prints the results to the screen - not exactly what we want if we want to create a file

    . To create a file, you can use the UNIX or DOS output redirection symbol >: C:\planets>java com.icl.saxon.StyleSheet planets.xml planets.xsl > planets.html

    When running on Windows, it's easy to immediately invoke Saxon in the form of the Win32 executable, saxon.exe, which can be downloaded from http://users.iclway.co.uk/mhkay/saxon/. On Windows it can be launched as follows (part

    specifies the output file name): C:\planets>saxon -o planets.html planets.xml planets.xsl Oracle XSLT

    Oracle also provides a free XSLT processor that can be downloaded from http://technet.oracle.com/tech/xml/, although it requires a lengthy registration process. At the time of writing, downloading the XSLT processor was as easy as clicking on the link for the XDK for Java at http://technet.oracle.com/tech/xml/.

    After unpacking zip archive downloaded from the Oracle website, the required JAR file (at the time of writing) will be called

    . It can be placed in your variable on Windows like this: C:\>set classpath=c:\oraclexml\lib\xmlparserv2.jar

    Required Java class -

    , to convert to you can use it this way: C:\planets>java oracle.xml.parser.v2.oraxsl planets.xml planets.xsl planets.html Xalan

    Probably the most widely used single XSLT processor is the Apache Project's Xalan (Apache is a widely used web server). The Java version of Xalan can be downloaded from http://xml.apache.org/xalan-j/index.html - just click on the zip archive file you require (currently

    for Windows or for UNIX).

    When you unzip the downloaded file you will receive both XSLT processor,

    , and the XML parser, . Both of these JAR files can be included in Windows as follows (change the file paths to suit your system): C:\>set classpath=c:\xalan-j_2_0_0\bin\xalan.jar;c:\xalan-j_2_0_0 \bin\xerces.jar

    To then use

    to convert to , run the Java class: C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL planets.xsl -OUT planets.html

    Note that to specify the input file we use

    , to indicate the output - , and to indicate the XSLT style sheet - . We'll be working with the Xalan processor most often, so more details are provided below. The following list contains all the tokens that can be used with the class, as output by Xalan itself: . Use only carriage return characters in the output - CR/LF is used by default; . Time diagnostic output; . Copy stack on error; . Use HTML format; ; . The number of spaces to align (indent) each level in the output tree is 0 by default; . Use only linefeed characters in the output - CR/LF is used by default; ; . Sets the stylesheet option; . Mode with minimal output(quiet); . Quiet Pattern Conflicts Warnings, warnings of pattern conflicts; . Use a plain text formatter; . Track each event of generating the result tree; . Track each allocation event; . Track templates as they are called; . Track the “children” of the template as they are processed; . Version information; . Validate input XML and XSL (validation is disabled by default); . Use XML formatters and add an XML header; .

    We'll look at all of these processors in the book; As mentioned, I will be using Xalan most often. (The reason is that it has become the most popular and widely adopted XSLT processor.) Of course, you can use any XSLT processor as long as it meets the W3C XSLT specification.

    This concludes our discussion of individual XSLT processors. Another way to convert XML documents without using a separate program is to use a client program such as a browser.

    Converting XML Documents Using Browsers

    XSLT support is included in both Microsoft Internet Explorer and Netscape Navigator. Of the two browsers, Internet Explorer has much more support for XSLT, and I'll be using version 5.5 of that browser here. You can read about XSLT support in Internet Explorer at http://msdn.microsoft.com/xml/XSLGuide/.

    Internet Explorer 5.5 and earlier versions by default does not exactly support XSLT syntax, so we need to make a number of modifications to the files

    And . (We'll look at this topic in more detail in the next chapter. There are addresses where you can download updated XSLT support packages.) Internet Explorer 6.0 was already available when this book was going to press. I installed it and checked its operation; It appears to support standard XSLT syntax (except that it is still required to use the type "" for style sheets - such as instead of ""). If you are still using IE 5.5 or earlier, you will need to make the changes listed here and in the next chapter. If you want to avoid this, use IE 6.0: this browser seems to support full XSLT syntax.

    To work with

    In IE (including version 6.0), I had to convert the attribute in the processing instructions from "" to "" (assuming the file is located in the same directory as , as the attribute in Listing 1.3 indicates). Listing 1.3. Version of planets.xml for Microsoft Internet Explorer Mercury .0553 58.65 1516 .983 43.4 .815 116.75 3716 .943 66.8 1 2107 1 128.4

    Now if you are using IE version 5.5 or earlier you should also convert the stylesheet

    (but not for version 6.0 and older; in this case, you only need to change the type attribute in the processing instruction from "" to ""). We'll look at how to do this in the next chapter, but here's the new version of planets.xsl to use: Listing 1.4. Version of planets.xsl for Microsoft Internet Explorer




    Now you can directly open planets.xml in Internet Explorer, as seen in Fig. 1.3.

    Rice. 1.3. Performing XSLT transformation in Internet Explorer


    Although you can work with XSLT in this way using Internet Explorer, you must modify the style sheet to meet Internet Explorer's requirements. Because Internet Explorer doesn't currently actually support XSLT by opening XML documents during navigation, I won't be using that browser for XSLT transformations in the book (except where specifically noted). I'll use XSLT processors like Saxon and Xalan to perform the transformations, and if the results are in HTML format, we'll view them in Internet Explorer.

    It's interesting to note that there is a way to perform true XSLT transformations in Internet Explorer without making any special changes to the XML or XSL documents, without even downloading and installing the latest MSXML parser (as discussed in Chapter 2) - however, this requires no navigation XML document, and access Internet Explorer's built-in XSLT processor, MSXML3, directly using JavaScript.

    Working with XSLT and JavaScript in Internet Explorer

    The XSLT processor in Internet Explorer 5.5 is part of the MSXML3 XML parser, and if you work directly with MSXML3 using JavaScript, there is no need to modify the source files

    and (Listings 1.1 and 1.2), as we did in the previous section. We'll see how this works in Chapter 10, but here's a web page that uses JavaScript and MSXML3 to perform the conversion using and display the results (note that you can modify this document to use your own XML and XSLT documents without having to to writing code in JavaScript: just replace the names with the names of your XML and XSL documents) (Listing 1.5). Listing 1.5. Conversion to Internet Explorer using JavaScript XSLT Using JavaScript var XMLDocument = new ActiveXObject("MSXML2.DOMDocument.3.0"); var XSLDocument = new ActiveXObject("MSXML2.DOMDocument.3.0"); var HTMLtarget = document.all["targetDIV"]; XMLDocument.validateOnParse = true; XMLDocument.load("planets.xml"); if (XMLDocument.parseError.errorCode != 0) ( HTMLtarget.InnerHTML = "Error!"; XSLDocument.validateOnParse = true; XSLDocument.load("planets.xsl"); if (XSLDocument.parseError.errorCode != 0) ( HTMLtarget.innerHTML = "Error!"; HTMLtarget.innerHTML = XMLDocument.transformNode(XSLDocument);

    As a result of processing this web page, the same result will be obtained as in Fig. 1.3: it downloads files directly

    and applies the MSXML3 parser to them. These are exactly the same files we've been working with throughout this chapter - they don't have the changes necessary from the previous topic, which we looked directly at in Internet Explorer. See Chapter 10 for more information.

    USING VBSCRIPT

    If you prefer VBScript, try using this Internet Explorer scripting language to achieve similar results.

    XSLT transformations on web servers

    You can perform XSLT transformations on the web server so that the XML document is transformed before the web server sends it to the browser. The most common transformation here is to convert an XML document to HTML, but server-side XML-to-XML transformations are also becoming more and more common.

    Unlike the other XSLT transformations discussed so far in this chapter, running XSLT transformations on a Web server usually requires some programming. There are three common ways to perform XSLT transformations on web servers: using Java servlets, Java Server Pages (JSP), and Active Server Pages (ASP). In Chapter 10 we explore all three of these methods in detail. Several XSLT processors can be configured for use on web servers - here's a starting list:

    AXSL, www.javalobby.org/axsl.html. AXSL is a server-side tool that converts XML to HTML using XSLT;

    Microsoft XML Parser, http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp. MSXML3 provides secure server access via HTTP protocol for working with ASP;

    mod_xslt, http://modxslt.userworld.com. A simple Apache web server module that uses XSLT to retrieve XML content. XSLT processing uses the Sablotron processor;

    PXSLServlet, www.pault.com/Pxsl This servlet can be used to transform XML to HTML using XSLT. It also allows you to read and write data to a SQL-based database (via JDBC);

    xesalt, www.inlogix.de/products.html. This XSLT processor exists as a module for the Apache and IIS web servers;

    XML Enabler, www.alphaworks.ibm.com/tech/xmlenabler. XML Enabler allows you to send requests to a servlet, and when the servlet responds, XML Enabler can format the data using various XSLT style sheets;

    XT, can be used as a Java servlet. He needs a servlet engine that implements at least, version 2.1 Java Servlet API. The Java servlet class is called com.jclark.xsi.sax.XSLServlet.

    Next example demonstrates a JSP page that enables the Xalan processor on a web server. Xalan converts

    using a style sheet. The code then reads the file and sends it back from the web server to the browser: XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); processor.process(new XSLTInputSource("planets.xml"), new XSLTInputSource("planets.xsl"), new XSLTResultTarget("planets.html")); FileReader filereader = new FileReader("planets.html"); BufferedReader bufferedreader = new BufferedReader(filereader); while((instring = bufferedreader.readLine()) != null) ( %>

    The result can be seen in Fig. 1.4: This shows the planets.html file sent to Internet Explorer by the web server running the JSP. Chapter 10 provides more information about using Java, JSP, and ASP servlets for server-side XSLT transformations.

    Rice. 1.4. Converting XML on a web server


    We looked at how to perform XSLT transformations using separate XSLT processors in Internet Explorer and web servers. However, the only conversion we have done so far is the XML to HTML conversion. Although it is now the most popular transformation, XML to XML transformations are also becoming more common.

    XML to XML Conversions

    XML-to-XML transformations are sometimes thought of as SQL for the web because they allow database queries to be run on XML documents. Below is an example. The file we use

    contains quite a lot of data about each planet: .0553 58.65 1516 .983 43.4 .815 116.75 3716 .943 66.8

    What if we only needed a subset of this data - for example, the name and mass of each planet? In database terms

    is a table and we want to create new table, containing a subset of the data from the first. In databases this is done SQL language, and for XML documents we can use XSLT.

    Listing 1.6 shows the new version of the file

    , which performs the required transformation: only the name and mass of each planet are selected and sent to the output document. In particular, note that we are doing an XML-to-XML conversion, so I'm using an element whose attribute is set to "xml" (in fact, the output data type is usually XML, but if the XSLT processor sees the tag, it will usually generate HTML by default ). Listing 1.6. Selecting only name and mass xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



    to using Xalan to create a new XML document, : C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL planets.xsl -OUT new.xml

    This is what the resulting XML document looks like,

    : .0553(Earth = 1)

    Note that this file looks very similar to the original file

    , except that each element contains only the elements and . This way we were able to obtain a subset of the original XML document data.

    Of course, any number of these kinds of XML-XML transformations can be performed. You can manipulate the data in an XML document to create entirely new XML documents. For example, you can take an XML document containing student names and grades and create a new document that displays grade averages. XSLT has many built-in functions that allow you to work with data in this way, which we'll look at in Chapter 8.

    In addition, many programs use XML to exchange data on the Internet, and since they typically format their XML documents differently, another popular way to use XML-to-XML transformations on the Internet is to convert XML from a format used by one program , into the format of another program.

    Conversions from XML to XHTML

    While many books focus exclusively on XML to HTML conversions, the truth is that the W3C is not very happy about this. The consortium was trying to shift the focus from HTML (which they originally standardized) to their new specification, XHTML, which was an XML-compatible rewrite of HTML. XHTML documents are also well-formed valid XML documents, so a conversion from XML to XHTML is really a conversion from XML to a special kind of XML.

    Although the W3C is actively promoting XHTML, the format has not yet become widespread. Therefore, in this book I mainly cover HTML; but since the W3C says that XHTML should be used, I'll cover the topic briefly here and in Chapter 6. If you want to learn more about XHTML, read the W3C XHTML 1.0 recommendation at www.w3.org/TR/xhtml1/, as well as the XHTML 1.1 at www.w3.org/TR/xhtml11/.

    While the W3C says that XML should be converted to XHTML rather than HTML, I haven't seen a single working example on their website. The examples they provide do not actually generate valid XHTML documents. However, support for XML-XHTML transformations is expected to be built into XSLT 2.0, and the W3C's desired phenomenon is likely to be upon us soon.

    We'll look at this type of transformation in more detail in Chapter 6, but here I'll show a working version of the table

    , which produces a valid XHTML version (Listing 1.7). Note that this time you must use the attribute on the element, and although this is valid XSLT code, not all XSLT processors will be able to process it. Listing 1.7. XML-XHTML transformation xmlns:xsl="http://www.w3.org/1999/XSL/Transform">doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional .dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/>




    I'll convert

    into a valid XHTML document, using this new variant and the XSLT XT processor. First you need to set the variable correctly: С:\>set classpath=c:xerces\xerces-1_3_0\xerces.jar;с:\xt\xt.jar;

    Then I do the conversion:

    C:\planets>java -Dcom.jclark.xsl.sax.parser=org.apache.xerces.parsers.SAXParser.com.jclark xsl.sax.Driver planets.xml planets.xsl planets.html

    The result will be an XHTML file,

    :

    This document,

    , is indeed a well-formed and valid XHTML 1.0 document (the most popular form of XHTML) according to the W3C HTML and XHTML Validation Program. The HTML/XHTML Validator can be found on the Internet at http://validator.w3.org/file-upload.html. Chapter 6 provides more information about XML-XHTML transformations.

    So far, we've given an overview of how XSLT works, looking at transformations from XML to HTML, XML, and XHTML. Later in the book, we'll look at XSLT transformations such as XML to RTF (Rich Text Format), plain text, XSL-FO, JavaScript, SQL-based databases, and more. There's also a lot of additional material on XSLT that you should know about, and now we'll get comfortable with the different kinds of XSLT resources that can be found on the Internet.

    XSLT Resources

    You can find a huge amount of information we need on the Internet. Please note that all URLs listed below are subject to change; the lists will be relevant for as long as the people who support the listed web sites want it.

    XSLT Specifications, Guides, and Examples

    The starting point for XSLT resources is, of course, the W3C itself. Below are the URLs for the W3C specifications used in the book:

    www.w3.org/Style/XSL/. W3C main page on XSL;

    www.w3.org/TR/xslt. XSLT Specification 1.0;

    www.w3.org/TR/xslt11. Working draft XSLT 1.1, which simplifies working with XSLT extensions and adds support for the W3C XBase recommendation;

    www.w3.org/TR/xslt20req. XSLT 2.0 Requirements, which provides an overview of XSLT 2.0 including additional support for XML schemas;

    www.w3.org/TR/xsl/. XSL formatting objects;

    www.w3.org/Style/2000/xsl-charter.html. XSL Committee Goals;

    www.w3.org/TR/xpath20req. XPath Requirements, which provides an overview of XPath 2.0, which includes additional support for XSLT 2.0;

    Http://lists.w3.org/Archives/Public/www-xml-stylesheet-comments/. List of W3C XML style sheets.

    Many XSLT tutorials and examples are available from other sources as well - here's a starting list:

    Http://http.cs.berkeley.edu/~wilensky/CS294/xsl-examples.html. A number of examples on XSLT;

    http://msdn.microsoft.com/xml/reference/xsl/Examples.asp. Examples of XSLT templates used in matched elements;

    http://msdn.microsoft.com/xml/XSLGuide/xsl-overview.asp. An initial description of working with XSLT;

    www.lists.ic.ac.uk/hypermail/xml-dev/xml-dev-Nov-1999/0371.html. XSLT Guide as a PowerPoint Presentation;

    www.mulberrytech.com/xsl/xsl-list/. An open list dedicated to discussion of XSL;

    www.nwalsh.com/docs/tutorials/xsl/xsl/slides.html. XSLT Guide;

    www.oasis-open.org/cover/xsl.html. Description of current events around XSLT;

    www.w3.org/Style/Activity. An impressive list of pages dedicated to W3C style sheets;

    www.xml101.com/xsl/. A complete set of XSLT references;

    www.xslinfo.com. A useful set of XSLT resources compiled by James Tauber;

    www.zvon.org/xxl/XSLTutorial/Books/Bookl/bookInOne.html. References for XSLT, XPath, XML, WML and other languages.

    I am aware of only one Usenet newsgroup on XSLT, however, and it is maintained by Microsoft - microsoft.public.xsl. Over time, others will appear. You may also be interested in the list mailing list via XSL - www.mulberrytech.com/xsl/xsl-list.

    In addition to the W3C specifications, guides, and examples, there are a large number of XSLT stylesheet editors available on the Internet.

    XSLT editors

    To create the XML and XSL documents used in this book, a standard text editor such as vi, emacs, pico, Windows Notepad, or Windows WordPad. By default, XML and XSL documents are expected to be written in Unicode, although in practice they can be written in ASCII, and virtually all documents to date have been written in ASCII. Just make sure that when you create a document, you save it in your editor's plain text format.

    WORKING WITH WORDPAD

    Windows text editors such as WordPad have an annoying feature - if they don't recognize the extension you give to a file, they append the .txt extension to the file name. For .xml and .xsl files this is not a problem because WordPad understands these extensions, but if you try to save documents created while working on a book with an extension that WordPad does not recognize, the editor will add the .txt extension to all of them. To prevent this from happening, when saving the document, enclose the file name in quotation marks: "file.abc".

    It will, however, be much easier to use a real XML editor specifically designed for processing XML documents. Here is a list of XML document editing programs:

    Adobe FrameMaker, www.adobe.com. Adobe introduces the excellent but expensive FrameMaker editor with XML support;

    XML Pro, www.vervet.com/. Expensive but powerful XML editor;

    XML Writer, on disk, XMLWriter http://xmlwriter.net/. Selection keywords color, user-friendly interface;

    XML Notepad, msdn.microsoft.com/xml/notepad/intro.asp. Free editor XML from Microsoft, a little awkward to work with;

    eNotepad, www.edisys.com/Products/eNotepad/enotepad.asp. An analogue of WordPad, which has good means work with XML and convenient user interface;

    XMetal from SoftQuad, www.xmetal.com. An expensive but very powerful XML editor, the favorite editor of many authors;

    XML Spy, www.xmlspy.com/. Has a good user interface and is easy to use;

    Arbortext's Epic, www.arbortext.com/. Powerful editor, expensive, has rich customization options.

    The XML Spy editor is shown in Fig. 1.5, XML Writer - in Fig. 1.6, and XML Notepad - in Fig. 1.7.


    Rice. 1.5. Editing XML in XML Spy



    Rice. 1.6. Editing XML in XML Writer



    Rice. 1.7. Editing XML in XML Notepad


    There are also special XSLT editors. Here's the starting list:

    Http://lists.w3.org/Archives/Public/xsl-editors/. XSL editor discussion list on the W3C website;

    IBM XSL Editor, www.alphaworks.ibm.com/tech/xsleditor. XSLT stylesheet editor in Java with visual interface for writing style sheets and selection and matching expressions. However, Java 2 version 1.1 (not 1.2 or 1.3) support must be installed;

    Stylus, www.exceloncorp.com/products/excelon_stylus.html. Stylus includes an XSLT stylesheet editor;

    Visual XML Transformation Tool, www.alphaworks.ibm.com/aw.nsf/techmain/visualxmltools. The Visual XML Transformation Tool generates XSLT for you to transform source documents into output documents;

    Whitehill Composer, www.whitehill.com/products/prod4.html. WYSIWYG drag-and-drop XSLT stylesheet generator;

    XL-Styler, www.seeburger.de/xml. Includes keyword highlighting, tag completion, HTML preview and more;

    XML Cooktop, http://xmleverywhere.com/cooktop/. This editor has just been released and looks decent. Includes tools for developing and validating XSLT style sheets;

    XML Spy, www.xmlspy.com/. You can also edit XSLT using this XML editor;

    XML Style Wizard, www.infoteria.com/en/contents/download. A tool that generates XSLT files using a wizard that examines the XML data and asks questions to the user;

    xslide, www.mulberrytech.com/xsl/xslide. Supports XSLT editing mode for Emacs;

    XSpLit, www.percussion.com/xmlzone/technology.htm. Allows you to separate HTML documents into XML DTD definitions and XSLT style sheets.

    XSLT Utilities

    There are also many XSLT utilities on the Internet - the following list highlights the most famous:

    Microsoft XSL API Extension, http://msdn.microsoft.com/downloads/webtechnology/xml/xslisapi.asp. Simplifies the task of performing XSLT transformations on the server side;

    Microsoft XSL-to-XSLT Converter, http://msdn.microsoft.com/downloads/webtechnology/xml/xsltconv.asp. Converts XSL to XSLT;

    XSL Lint, www.nwalsh.com/xsl/xslint. XSL Lint checks XSLT syntax, allowing you to detect many types of errors;

    XSL Trace, www.alphaworks.ibm.com/tech/xsltrace. This product allows the user to walk through the steps of XSLT visually;

    XSLT Compiler, www.sun.com/xml/developers/xsltc. Converts XSLT files into Java classes for converting XML files;

    XSLT test tool, www.netcrucible.com/xslt/xslt-tool.htm. This tool gives you the ability to run XSLT on a variety of popular processors, allowing you to test whether your transformation works correctly on all systems. It is possible to call Microsoft's MSXML3 from the command line, just like any other XSLT processor;

    XSLTC, www3.cybercities.com/x/xsltc. Compiles XSLT style sheets into C++ code. Based on Transformix, Mozilla's XSLT processor;

    XSLTracer, www.zvon.org/xxl/XSLTracer/Output/introduction.html. XSLTracer is a Perl tool that demonstrates how to process XML files using XSLT style sheets.

    This concludes the overview of XSLT in this chapter. As you can see, a huge set of information awaits us. The remainder of the chapter will provide an overview of XSL-FO.

    XSL Formatting Objects: XSL-FO

    The most popular part of XSL is XSLT transformations, which we already introduced in this chapter. Another, significantly larger part is XSL formatting objects, XSL-FO (XSL Formatting Objects).

    With XSL-FO, you can specify the formatting and display of an XML document with millimeter precision. You can set anything you want for documents: text font, location, alignment, color, indexes, margin size, and much more. Working with XSL-FO is similar to the process of creating a word processor by hand, and due to the complexity of XSL-FO, some avoid using them. In Chapters 11 and 12, we'll explore what XSL-FO has to offer and how to work with it.

    XSL-FO Resources

    A number of XSL-FO resources are available on the Internet, but there are far fewer of them than XSLT resources. Here are the main ones:

    Http://lists.w3.org/Archives/Public/www-xsl-fo/. List of W3C notes for XSL-FO.

    Just as there are XSLT processors, there are also XSL-FO processors. None of them, however, come close to fully implementing the standard. Here is the initial list of XSL-FO processors:

    FOP, http://xml.apache.org/fop. Java Application, which reads the tree of XSL formatting objects (produced by the XML parser) and produces a PDF document;

    PassiveTeX, http://users.ox.ac.uk/~rahtz/passivetex. A TeX package that formats XSL-FO output to PDF. Uses David Carlisle's xmltex XML parser;

    SAXESS Wave, http://www.saxess.com/wave/index.html. XML-Shockwave/Flash Converter;

    TeXML, http://www.alphaworks.ibm.com/tech/texml. Converts XML documents to TeX format;

    Unicorn Formatting Objects (UFO), http://www.unicorn-enterprises.com. An XSL formatting object processor written in C++. Can generate output in PostScript, PDF and other formats supported by DVI TeX drivers;

    XEP, http://www.renderx.com/F02PDF.html. An XSL-FO processor in Java that converts XSL formatting objects to PDF or PostScript.

    In this book, I'll use the FOP (formatting objects processor), which is probably the most common XSL-FO processor. The Java-based XSL-FO processor takes an XML document written to use XSL-FO formatting objects and converts it into a PDF format that can be viewed in Adobe Acrobat. While XSLT transformations are often done in HTML, this won't work for XSL-FO because it specifies every aspect of the document's presentation format down to the smallest detail, something that PDF is much better suited for.

    Formatting an XML Document

    For formatting

    We can use the XSL-FO formatting objects introduced in Chapter 12. For example, here's how we can display the name of the first planet, Mercury, using the XSL-FO formatting objects and :

    However, creating an entire document using XSL formatting objects is not an easy task, except for short documents. The W3C anticipated these difficulties, which is one of the main reasons the consortium introduced the transformation language, XSLT. Specifically, you can create a style sheet and use XSLT to transform the XML document so that it uses XSL formatting objects.

    In practice, transformations are almost always done using style sheets, and that's what we'll do in Chapters 11 and 12. All you need to do is provide an XSLT style sheet that can be used to transform your document using formatting objects. With this method, the XSLT processor does all the work, transforming the document from a representation you're comfortable working with into a representation with formatting objects, which you can then paste into a program that can process the formatting objects and display the formatted result.

    To clarify all of the above, below is an example of working with an XML document that we have already encountered in this chapter,

    : .0553 58.65 1516 .983 43.4 .815 116.75 3716 .943 66.8 1 2107 1 128.4

    In this example I'll use an XSLT stylesheet - we'll see how to create one in Chapter 11 - to transform

    so that it uses formatting objects. I then use the FOP processor to convert the new document into a PDF file. We'll also look at how a formatted document looks in Adobe Acrobat. XSLT style sheet

    Listing 1.8 shows the style sheet

    , which takes data from and formats it into a PDF file, . In this case, I use a large font for the text - 36 points. Listing 1.8. XML-XSL-FO transformation xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version= "1.0">
    margin-top="0mm" margin-bottom="10mm" margin-left="0mm" margin-right="0mm"/>
    font-weight="bold"
    font-size="36pt" line-height="48pt"
    font-size="36pt" line-height="48pt"
    font-size="36pt" line-height="48pt"
    font-size="36pt" line-height="48pt"
    font-size="36pt" line-height="48pt" Convert a document to a view with formatting objects

    In order to convert

    In a document that uses formatting objects, which I named , all you need to do is apply a style sheet. This can be done using the XSLT techniques already discussed in this chapter.

    For example, to create

    using Xalan, on Windows you first need to install: C:\>set classpath=c:\xalan\xalan-1_2_0_0\bin\xalan.jar; c:\xalan\xalan-j_2_0_0\bin\xerces.jar

    Then apply

    to generate: C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL planetsPDF.xsl -OUT planets.fo

    To set document formatting

    uses XSL formatting objects. This is what the file looks like (Listing 1.9): Listing 1.9. planets.fo margin-right="20mm" margin-left="20mm" margin-bottom="10mm" margin-top="10mm" page-width="300mm" page-height="400mm" margin-right=" 0mm" margin-left="0mm" margin-bottom="10mm" margin-top="0mm"/> font-family="sans-serif" line-height="48pt" font-size="36pt" font- weight="bold"> font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height ="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height="48pt" Distance (million miles): 43.4 font-family="sans- serif" line-height="48pt" font-size="36pt" font-weight="bold"> font-family="sans-serif" line-height="48pt" font-family="sans-serif" line -height="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height ="48pt" Distance (million miles): 66.8 font-family="sans-serif" line-height="48pt" font-size="36pt" font-weight="bold"> font-family="sans-serif " line-height="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line-height="48pt" font-family="sans-serif" line -height="48pt" font-family="sans-serif" line-height="48pt" Distance (million miles): 128.4

    So we have successfully created

    . How can we now use it to create a formatted PDF file? Create a formatted document

    To process

    to create a formatted document, I'll use James Tauber's FOP processor, which he donated to the Apache XML Project.

    Processor home page - http://xml.apache.org/fop; FOP can currently be downloaded from http://xml.apache.org/fop/download.html. The FOP package, including documentation, comes in zip format, so you need to extract it first. FOP is implemented as a Java JAR file,

    , here I will use FOP version 0.15.

    FOP can be started from the command line using the Java class, at the time of writing called

    org.apache.fop.apps.CommandLine. Need to provide an XML parser - I'll use the Xerces Java parser in the file (it comes with Xalan). Let's look at how on Windows, using Java, using FOP to convert to : in this case, I specify the variable with the key to include the file, as well as the two necessary JAR files included in the downloadable package FOP - and . (This example assumes that all , and files are located in the directory; if not, provide their full paths.) C:\planets>java -cf fop.jar:xerces.jar:w3c.jar org.apache. fop apps.CommandLine planets.fo planets.pdf

    The resulting file

    , can be viewed in the Adobe Acrobat Reader PDF file reader, as shown in Fig. 1.8. (Acrobat PDF Reader can be downloaded for free from www.adobe.com/products/acrobat/readermain.html.) The document shown in the figure is formatted according to a style sheet.

    Rice. 1.8. PDF document created using formatting objects


    PDF format - good format to display formatting objects, although it has a number of limitations - for example, it is not able to handle dynamic tables that can be expanded or collapsed on a mouse click, or clickable multicast links (both of which are included in the specification of formatting objects). Although XSL-FO is poorly supported in major browsers, this is expected to change in the future and browsers will support XSL-FO.

    brief information

    Uses .NET 3.5 XML software to transform the metadata of an ArcGIS item or any XML file using an XSLT 1.0 style sheet and save the results to an XML file.

    XSLT templates can be used to make a variety of changes to ArcGIS metadata or XML files. Some XSLT styles are provided in ArcGIS for Desktop. They can be found in the \Metadata\Stylesheets folder. These styles are described in the table below.

    The following styles are used to create HTML documents that are used to display element information on the Description tab. They extract the metadata content of an object and add HTML formatting instructions to it. These styles import various XSLT templates from other files into the ArcGIS_Imports folder; imported templates are used to perform most operations. If you are interested in creating custom display styles, additional information These styles can be found in the documentation provided with the ArcGIS Metadata Toolkit.

    The following styles, contained in the \Metadata\Stylesheets\gpTools folder, take the element's metadata, process it, and save the resulting XML document into a new XML file. The purpose of this operation is to filter the item's metadata before it is used outside of ArcGIS. The goal may also be to change the item's metadata. In this case, the resulting XML file can be saved back to source element as the latter's metadata, using the Metadata Importer tool. You can create a model or script that will first run the tool using the style to update the metadata content and then immediately save that content back into the element. The following styles are provided in ArcGIS for Desktop to perform some highly defined metadata processing tasks.

    add unique identifier.xslt

    All examples below use the XSL language standard. A more modern modification of this standard is also widely used - the XSLT language, more details about which can be read in \xml\XSLTutorial or MSDN.

    Let's look at a simple example XML file (ex01.xml). This and other examples can be found in the \xml\ XSLTForBeginers folder on disk.



    "Notes on XSL"

    If we open this file in Internet Explorer, we will see the same text as above, along with all the tags and service information. But we don't need tags and service information! We want to see only the information that is relevant to the case, and with the help of tags we want to manage appearance this information. This task can be solved easily and simply: you need to add a transformation template - an XSL file - to the XML file.

    Let's rewrite our XML file in the following form (ex01-1.xml).




    "Notes on XSL"

    And let's create an XSL file ex01-1.xsl. The file text is given below.






    The first line of the file contains the xsl:stylesheet element tag. The element's attributes are the version number and namespace reference. These xsl:stylesheet element attributes are required. In our case, the namespace is all the names of elements and their attributes that can be used in the XSL file. For XSL files, namespace reference is standard.

    Note that an XSL file is a type of XML file. It does not contain user data, but its format is the same. The file contains an element top level xsl:stylesheet, and then there is a tree of transformation rules.

    In this document, we will not explain in detail what each element in the XSL file means. We will bring various examples and show the result in each example, which will make it possible to independently compare various elements XSL file and the transformations initiated by these elements on the original XML file with user information. Note also that the attribute value select and similar ones with the meaning “select” are written in a special language XPath, which you can read about briefly on the \xml\XPathTutorial disk, and in more detail on MSDN.

    In the future, we will provide the texts of XML and XSL files in black and white. You can always open the real file and see everything in color. If necessary, comment out the link to the XSL file. The comment syntax is as follows - . The characters -- cannot be inserted into the comment text.

    In the first example, we looked at how the xsl:value-of element can be used to display the contents of the element (text enclosed between tags) in HTML format. Now we'll see how we can use the same element to display the value of an element's attribute.

    Consider the following XML file ex02-1.xml







2024 gtavrl.ru.