Create multiple files in XSLT 2.0

Source: Internet
Author: User
Tags format variables version variable xmlns xsl xslt
Create

The first version of XSLT has great limitations, with only one input and one output (although there can be multiple template files). The 2nd edition of the standard still requires an input, but the output system is more flexible. You can now use xsl:result-document instructions to produce multiple output files. This new label has two important attributes, as shown in table 1.

Table 1. Xsl:result-document Property

Property Description
href filename of output file or fully qualified URL
format The name of the format used, as defined in the corresponding xsl:output instruction

To test the directive, I designed an input XML file that included a set of test results (see Listing 1).

Listing 1. Input XML file
<?xml version="1.0" encoding="UTF-8"?><tests>    <testrun run="test1">        <test name="foo" pass="true" />        <test name="bar" pass="true" />        <test name="baz" pass="true" />    </testrun>    <testrun run="test2">        <test name="foo" pass="true" />        <test name="bar" pass="false" />        <test name="baz" pass="false" />    </testrun>    <testrun run="test3">        <test name="foo" pass="false" />        <test name="bar" pass="true" />        <test name="baz" pass="false" />    </testrun></tests>

This piece of XML is very simple. Each test run has a set of specified tests with a pass mark to indicate whether the test was successful.

Create a file for each test
First, create a file for each test result, as shown in Listing 2 of the XSL template.

Listing 2. Code to create a file for each test
  <?xml version= "1.0" encoding= "UTF-8"? ><x Sl:stylesheet xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" version= "2.0" ><xsl:output "text" method= <xsl:output method= "html" indent= "yes" name= "HTML/><xsl:template match="/"><xsl:for-each select="// TestRun "><xsl:variable name=" filename "select=" concat (' output1/', @run, '. html ') "/><xsl:value-of Select = "$filename"/> <!--creating--><xsl:result-document href= "{$filename}" format= "html" > <HTML>&L t;body> <xsl:value-of select= "@run"/> </body> 

There are several places to note, starting with the head of the file. The properties of the stylesheet tag version are set 2.0 so that labels can be used xsl:result-document . Next, the style sheet itself is set text as the output type. This means that if you want the file to have HTML formatting, you also need to define a html second specified format for the type. I xsl:result-document use this format in the label.

Then iterate xsl:for-each through the testrun label using loops. variableCreate a new variable with the label in each label $filename , and output the directory name ( output1 ), run name, and extension. html to synthesize a path.

Then I use value-of tags and $filename variables to tell the user that the file is being created. Then use the xsl:result-document label to open the new document output HTML. Listing 3 shows the results of running the above data file.

Listing 3. Run the results of the sample data file (using Saxon)
Creating output1/test1.htmlCreating output1/test2.htmlCreating output1/test3.html    

Get a better result
xsl:result-documentThe contents of the label are calculated in the same way as other parts of the template, but it does. All variables in the context of a template can be used.

As an example, I modified the xsl:result-document code in the tag to provide more information about the test results (see listing 4).

Listing 4. Better HTML output
<xsl:result-document href="{$filename}" format="html">    

The output results are shown in Listing 5.

Listing 5. Improved HTML output Results

Create an index
Finally, you need to add an index file that points to all the test results for the output. To do this, I used another xsl:result-document tag and hard-coded the output into the index file (see listing 6).

listing 6. Code to create an index file
<!-- Creating the index --><xsl:result-document href="output3/index.html"   format="html"> 

This section is placed after the loop that creates an HTML file for each test case xsl:for-each . Listing 7 shows the resulting index file.

Listing 7. index file

Conclusion
Using xsl:result-document directives, you can output an XSL template from a single data source to multiple files. This functionality is a nonstandard extension of the XSLT 1.x, opening a new window of opportunity for XSLT template creators.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.