Does libxslt have a feature for splitting a document into multiple documents?
Asked Answered
M

3

6

Looks like libxslt does not support XSLT 2.0, and xsl:result-document. Is there a way to mimic xsl:result-document using libxslt, or xsltproc?

Mussolini answered 2/7, 2010 at 17:41 Comment(1)
Good question (+1). See my answer for a possible solution.Lempres
F
14

Yes, there is, using exsl:document. A simple example:

==== foo.xsl ====
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <exsl:document href="toc.html" method="html">
      <html>
        <body>
          <xsl:apply-templates select=".//h1"/>
        </body>
      </html>
    </exsl:document>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

taking this as input:

==== foo.html ====
<html>
  <body>
    <h1>Hello, world!</h1>
    <p>Some longwinded text follows.</p>
  </body>
</html>

when run like this:

xsltproc foo.xsl foo.html

will yield this to stdout:

<html>
  <body>
    <h1>Hello, world!</h1>
    <p>Some longwinded text follows.</p>
  </body>
</html>

while also writing this to toc.html:

<html><body><h1>Hello, world!</h1></body></html>
Fescennine answered 2/7, 2010 at 22:31 Comment(0)
L
3

If libxslt implements EXSLT, then you could use the <exsl:document> extension element.

If not, then you have to write your own extension functions, because XSLT 1.0 does not support creating multiple result documents.

Update: As confirmed in this comment, libxslt implements EXSLT. Just grab it and use <exsl:document> .

Lempres answered 2/7, 2010 at 18:21 Comment(1)
libxslt the project does; it's provided in libexslt.a.Fescennine
P
2

And here is an example that shows how to use the extension to create an unlimited number of files as a nodeset is traversed. Again, using xsltproc (libxslt)

Sample XML Input:

<clients>
    <client id="ACME1" name="ACME Company 1"/>
    <client id="ACME2" name="ACME Company 2"/>
    <client id="ACME3" name="ACME Company 3"/>
</clients>

Sample XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl">

<xsl:output method="html" indent="yes" encoding="UTF-8" />

<xsl:template match="/">
    <xsl:for-each select="/clients/client">
    <exsl:document href="{@id}.html" method="html">
      <html>
        <body>
          <h1>Company: <xsl:apply-templates select="@name"/></h1>
        </body>
      </html>
    </exsl:document>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Will produce 3 files ACME1.html, ACME2.html, etc.

Proleg answered 6/9, 2015 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.