Reputation: 1825
I'm trying to do a template match for comments so that it looks for virtual include and convert it to php include:
<node>
<!--#include virtual="/abc/contacts.html" -->
<!-- some random comment -->
</node>
to
<node>
<?php include($_SERVER[DOCUMENT_ROOT]."/abc/contacts.html"); ?>
<!-- some random comment -->
</node>
I'm trying to do something like:
<xsl:template match="comment()" >
<xsl:analyze-string select="." regex="^[\s\S]*<!">
<xsl:matching-substring>
<xsl:text disable-output-escaping="yes"><?php </xsl:text> <xsl:value-of select="." /> <xsl:text disable-output-escaping="yes"> ?></xsl:text>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
Any help to solve this problem is highly appreciated.
Upvotes: 1
Views: 259
Reputation: 243459
You don't need XSLT 2.0 for this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"comment()[starts-with(normalize-space(),'#include virtual=')]">
<xsl:processing-instruction name="php">
<xsl:text>include($_SERVER[DOCUMENT_ROOT].</xsl:text>
<xsl:value-of select=
"substring-after(normalize-space(),'#include virtual=')"/>
<xsl:text>);</xsl:text>
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<node>
<!--#include virtual="/abc/contacts.html" -->
<!-- some random comment -->
</node>
the wanted, correct result is produced:
<node>
<?php include($_SERVER[DOCUMENT_ROOT]."/abc/contacts.html");?>
<!-- some random comment -->
</node>
Explanation:
Proper use of the identity rule, template match patterns, the XPath functions normalize-space()
and starts-with()
, and of the xsl:processing-instruction
XSLT instruction.
Upvotes: 1