Subject: [xsl] Unix Philosophy Applied to XSLT Development From: "Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Sat, 23 Jul 2022 14:36:25 -0000 |
Hi Folks, In a post last week I mentioned my admiration for the Unix philosophy, which essentially is one of creating independent, reusable tools that are connected via shell operators including pipe and "more". I would now like to illustrate how to apply that Unix philosophy to XSLT development. I do so with a simple problem. Problem Statement: There is a file (economics-books.xml) containing book data. The content of that file is to be added onto another document (compiler-books.xml). Then, at the bottom of the file an element (authors-list) is to be added containing a list of all the book authors. The Unix philosophy is to identify the specific problems to be solved, generalize them, and create a tool for each. I identified two specific problems and generalized them: 1. Append: append the content of one file to another. 2. Footnote: add to the bottom of a file an element and populate it using values from the file. In the Unix philosophy a tool's input comes from standard input (stdin) and the tool's output goes to standard output (stdout). Once I implemented the two tools (append and footnote) the solution to the problem was easy: more compiler-books.xml | append economics-books.xml | footnote authors author-list "Stream the content of compiler-books.xml into the append tool, which appends the content of economics-books.xml to the input stream. The resulting output is streamed to the footnote tool, which creates an authors-list footnote element populated with the list of book authors." Interestingly, since the tools are independent we can even reverse the order of the tools: more compiler-books.xml | footnote authors author-list | append economics-books.xml Of course, applying the footnote tool to compiler-books.xml and then appending economics-books.xml produces a different result. Here is compiler-books.xml <books> <book> <title>Compilers Principles, Techniques, Tools</title> <authors>Alfred V. Aho, Ravi Sethi, Jeffrey Ullman</authors> <publisher>Addison-Wesley</publisher> <date>1986</date> </book> <book> <title>Introduction to Compiling Techniques</title> <authors>J. P. Bennett</authors> <publisher>McGraw-Hill</publisher> <date>1996</date> </book> </books> Here is economics-books.xml <books> <book> <title>Economic Facts and Fallacies</title> <authors>Thomas Sowell</authors> <publisher>Basic Books</publisher> <date>2011</date> </book> <book> <title>Economics in One Lesson</title> <authors>Henry Hazlitt</authors> <publisher>Harper & Brothers</publisher> <date>1946</date> </book> </books> This command: more compiler-books.xml | append economics-books.xml produces this result: <books> <book> <title>Compilers Principles, Techniques, Tools</title> <authors>Alfred V. Aho, Ravi Sethi, Jeffrey Ullman</authors> <publisher>Addison-Wesley</publisher> <date>1986</date> </book> <book> <title>Introduction to Compiling Techniques</title> <authors>J. P. Bennett</authors> <publisher>McGraw-Hill</publisher> <date>1996</date> </book> <book> <title>Economic Facts and Fallacies</title> <authors>Thomas Sowell</authors> <publisher>Basic Books</publisher> <date>2011</date> </book> <book> <title>Economics in One Lesson</title> <authors>Henry Hazlitt</authors> <publisher>Harper & Brothers</publisher> <date>1946</date> </book> </books> Notice that the books in economics-books.xml have been appended to the compiler books. Since the append tool is a general tool, it can be used to append any kind of data. For example, if there is a list of employees (employees.xml) and we want to append summer-interns.xml to it, we would simply write: more employees.xml | append summer-interns.xml With this command: more compiler-books.xml | append economics-books.xml | footnote authors author-list we get this result: <books> <book> <title>Compilers Principles, Techniques, Tools</title> <authors>Alfred V. Aho, Ravi Sethi, Jeffrey Ullman</authors> <publisher>Addison-Wesley</publisher> <date>1986</date> </book> <book> <title>Introduction to Compiling Techniques</title> <authors>J. P. Bennett</authors> <publisher>McGraw-Hill</publisher> <date>1996</date> </book> <book> <title>Economic Facts and Fallacies</title> <authors>Thomas Sowell</authors> <publisher>Basic Books</publisher> <date>2011</date> </book> <book> <title>Economics in One Lesson</title> <authors>Henry Hazlitt</authors> <publisher>Harper & Brothers</publisher> <date>1946</date> </book> <author-list>Alfred V. Aho, Ravi Sethi, Jeffrey Ullman, J. P. Bennett, Thomas Sowell, Henry Hazlitt</author-list> </books> If instead of authors we want the footnote to contain the list of publishers, we simply modify the arguments to the footnote tool: footnote publisher publisher-list The result is this footnote: <publisher-list>Addison-Wesley, McGraw-Hill, Basic Books, Harper & Brothers</publisher-list> To recap: Identify general tools. Implement the tools. Use the tools to solve many problems, not just one specific problem. Arrange the tools in any order. This is powerful! To my way of thinking, this is exactly the right way of solving problems. I believe the Unix people had software development right 50 years ago. /Roger Implementation: You might be wondering how an XSLT processor can take input from stdin and output to stdout. "How did you create XSLT-based tools that use stdin and stdout?" The technique is to use a shell script which saves stdin to a temp file and then invokes the XSLT processor with the temp file: more > tmp\file-in.xml java net.sf.saxon.Transform tmp\file.xml -xsl:append.xsl -o:tmp/file-out.xml filename=%1 Direct the XSLT processor to output the results to a temp file. The content of the temp file is then streamed to stdout: more tmp\file-out.xml TaDa! The XSLT processor (virtually) takes input from stdin and outputs to stdout. Unix philosophy applied to XSLT development!
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Grouping help, Imsieke, Gerrit, le- | Thread | Re: [xsl] Unix Philosophy Applied t, Norm Tovey-Walsh ndw |
Re: [xsl] Grouping help, rick@xxxxxxxxxxxxxx | Date | Re: [xsl] Unix Philosophy Applied t, Norm Tovey-Walsh ndw |
Month |