3

This is a follow up on my earlier question xslt split mp3 tag into artist and title

I'll try to phrase it in a generic terms because I think it will me allow for a better understanding of XSLT: what and how use it using the appropriate XSLT idoms.

This is what I want:

input XML -> intermediate XML -> ... -> final transformation

Or in other words: how can I pipeline various XML transformations in one XSLT document?

My command-line analogy would be to have multiple command line tools that perform parts of the solution, then have them execute in succeeding order using pipes.

In this specific case:

input XML (with element) -> intermediate XML (with separate and element) -> final XML sorted by ,

I'm limited to one XSLT document as the web-tool at hand does not even allow xsl:include or xsl:import to succeed.

Community
  • 1
  • 1
Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • Perhaps I misunderstand the question. But you can call xslt command line tools just as you like to and pipe the output from one step into the next. E.g. `xsltproc 1.xlt 1.xml | xsltproc 2.xsl -`. But I would prefer tmp files. – hr_117 Jun 23 '13 at 15:27
  • I wish I could do that here. The tool I use does not even allow for including other xsl files. – Jeroen Wiert Pluimers Jun 23 '13 at 15:30

1 Answers1

4

Three approaches that come readily to mind are:

  • Use operating-system pipelines:

    xsltproc ss1.xsl input.xml \\
      | xsltproc ss2.xsl - \\
      | xsltproc ss3.xsl - \\
      > output.xml
    

    The primary downside I'm aware of here is that not all processors have command-line interfaces that make it easy to read the main input tree on stdin. So when I do this, I sometimes end up writing temporary files; fortunately, disk space is cheap. Upside: you probably already know how to do this.

  • Use XProc pipelines.

    Primary downside: you have to learn a new technology. Primary upside: you get to learn a new technology, which is actually quite cool.

  • Define different modes for the different operations and use XSLT 2.0 (or an XSLT 1.0 processor with some form of the node-set extension) to process the data:

    <xsl:template match="/">
      <xsl:variable name="tree1">
        <xsl:apply-templates mode="mode1"/>
      </xsl:variable>
      <xsl:variable name="tree2">
        <xsl:apply-templates mode="mode2" select="$tree1"/>
      </xsl:variable>
      <xsl:apply-templates mode="mode3" select="$tree2"/>
    </xsl:template>
    

    Upside: it's all in a single stylesheet, so you never have to puzzle out how to run the process, when you come back to it six months later. (And the phrasing of your question says that this is the answer you really want.) Downside: it's all in a single stylesheet, so you have to work harder to achieve modularity and separation of concerns.

There are doubtless other approaches as well.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Given that it needs all to be in a single style-sheet, does this XSLT engine allow for libxslt "node-set() extensions"? Vendor: libxslt, Version: 1.0 – Jeroen Wiert Pluimers Jun 23 '13 at 15:49
  • Try `xsltproc --dumpextensions | grep node-set` to see. Of course, you can also use function-available() from within a stylesheet. – C. M. Sperberg-McQueen Jun 23 '13 at 17:21
  • That's the thing: xsltproc is not on the command-line. It is integrated in the icecast server, and I don't know which version they have built it with. I'll ask a new question about it. – Jeroen Wiert Pluimers Jun 23 '13 at 18:10
  • I even managed to answer that new question (: http://stackoverflow.com/questions/17265064/detect-availability-of-node-set-function-in-xslt-implementation-of-icecast-ser – Jeroen Wiert Pluimers Jun 24 '13 at 07:39