XSLT: how can I call a template when there is no input file?

I create a template that produces output based on a single line, is passed through a parameter and does not use an input XML document. xsltprocIt seems to be happy to start with the only parameter defining the stylesheet, but I don’t see a way to launch the template without an input file (for example, without an option xsltprocto launch the named template).

I would like to be able to run:

xsltproc --stringparam bar baz foo.xsl

But now I need to run, with the "main" template matching the "/":

echo '<xml/>' | xsltproc --stringparam bar baz foo.xsl -

How can I make this work? I'm sure I saw other templates in the past that were meant to run without an input document, but I don’t remember how they worked or where to find them again. :-)

+3
source share
2 answers

Actually, this was done quite often.

In XSLT 2.0, it is defined in Spec. that providing an initial node context is optional. If the source context of the node is not provided (no source XML document), then it is important to specify the name of the named template that must be executed as the entry point to the transformation.

In XSLT 1.0, you can convert your own main stylesheet file (file) as a source XML document, and, of course, the conversion can completely ignore this source XML document. This method has long been demonstrated and used by Jeni Tennison .

For example:

<?xml-stylesheet type="text/xsl" href="example.xml"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <p>Hello, world!</p>
    </xsl:template>
</xsl:stylesheet>

"example.xml", Windows, "example.xml" IE :

, !

+8

, XSLT - , , - node. XSLT- , (, ) , , , xsltproc , .

, XSLT - , .

+1

Source: https://habr.com/ru/post/1714256/


All Articles