1

仮想インクルードを探してphpインクルードに変換するように、コメントのテンプレートマッチを実行しようとしています:

<node>
<!--#include virtual="/abc/contacts.html" -->
<!-- some random comment -->
</node>

<node>
<?php include($_SERVER[DOCUMENT_ROOT]."/abc/contacts.html"); ?>
<!-- some random comment -->
</node>

私は次のようなことをしようとしています:

<xsl:template match="comment()" >
<xsl:analyze-string select="." regex="^[\s\S]*&lt;!">
<xsl:matching-substring>
<xsl:text disable-output-escaping="yes">&lt;?php&nbsp;</xsl:text> <xsl:value-of select="." /> <xsl:text disable-output-escaping="yes">&nbsp;?&gt;</xsl:text>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>

この問題を解決するための助けをいただければ幸いです。

4

1 に答える 1

1

これには XSLT 2.0 は必要ありません

<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>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<node>
    <!--#include virtual="/abc/contacts.html" -->
    <!-- some random comment -->
</node>

必要な正しい結果が生成されます。

<node>
    <?php include($_SERVER[DOCUMENT_ROOT]."/abc/contacts.html");?>
    <!-- some random comment -->

</node>

説明:

ID ルール、テンプレート マッチ パターン、XPath 関数normalize-space()starts-with()およびxsl:processing-instructionXSLT 命令の適切な使用。

于 2013-01-12T04:25:58.477 に答える