L
L
LastDragon2011-04-17 09:09:03
PHP
LastDragon, 2011-04-17 09:09:03

How to execute arbitrary PHP script during XSL conversion?

Given


  1. An XML document containing links to documents in some format:
    <?xml version="1.0" encoding="UTF-8"?>
    <project>
        <documentation path="/path/to/file.textile" type="textile"/>
        <documentation path="/path/to/file.html" type="html"/>
    </project>
      
  2. PHP script that is used to convert documents to HTML
  3. XSLT style to convert XML document to HTML document
  4. Ant script which is used to run the XSL transformation

Question


How to transform an XML document to replace everything <documentation/>with the corresponding HTML? (i.e. the resulting document should contain the result of executing a PHP script, to which the path to the file and its format were passed as parameters)

Solutions


  1. Add php protocol and in XSLT use e.g.document('php:....')
  2. Double transformation: (1) create an html/php document in which the tags are <documentation/>replaced by a script call; (2) execute the created document

(1) is more preferable, but it is not clear how to implement it.

Restrictions


  1. Multiplatform
  2. Using only "local" data (i.e. you can't use http://)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
LastDragon, 2011-04-24
@LastDragon

Perhaps someone will be interested - the problem was solved by writing an extension for the xslt processor and adding it to the style (written in java, used to convert text org.eclipse.mylyn.wikitext)
Class example:

package ru.lastdragon.ipb.toolkit.xslt;

public final class Converter {
    public static String toHtml(String path) {
        // .....
    }
}

Style example:
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:converter="ru.lastdragon.ipb.toolkit.xslt.Converter"
    extension-element-prefixes="converter"
    exclude-result-prefixes="converter">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
    
    <xsl:template match="/">
        <xsl:value-of select="converter:toHtml('path/to/file.textile')"/>
    </xsl:template>
</xsl:stylesheet>

If someone needs it, the code can be found ( if I have time to come up today ) in the repository of the IPB3 Toolkit (toolkit) project ( GPLv3 license ).

K
Kirill Boldyrev, 2011-04-17
@usetester

For solution 1 see www.php.net/manual/en/book.stream.php

Y
yadeveloper, 2011-04-17
@yadeveloper

Do "apply-template" on "documentation".
In the template itself, use the following functionality: ua.php.net/manual/en/xsltprocessor.registerphpfunctions.php

C
cyberklin, 2011-04-17
@cyberklin

the idea is this:
1. register your function in xslt, which will return the contents of the documentation node
2. change the XSLT style from point 3 so that it runs documentation nodes through our function before use.
if xslt transformation would be started not by ant, but, for example, by a php script, then such a trick could easily be done through registerPHPFunctions ( ru2.php.net/manual/en/xsltprocessor.registerphpfunctions.php )
you can modify the scheme to such, that ant just runs a php cli script that will do the transformation? then everything is decided through registerPHPFunctions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question