【问题标题】:Convert virtual include to php include将虚拟包含转换为 php 包含
【发布时间】:2013-01-12 00:04:35
【问题描述】:

我正在尝试为 cmets 进行模板匹配,以便它查找虚拟包含并将其转换为 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>

非常感谢任何解决此问题的帮助。

【问题讨论】:

  • Vinit,我的回答对你有用吗,还是你还有什么问题?
  • 非常感谢。这就像一个魅力:)

标签: xml xslt xml-parsing xslt-2.0


【解决方案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>

说明

正确使用 identity rule、模板匹配模式、XPath 函数 normalize-space()starts-with(),以及xsl:processing-instruction XSLT 指令。

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2011-10-31
    • 2014-05-17
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多