Php xslt转换中的保留节点

Php xslt转换中的保留节点,php,xml,xslt,xpath,Php,Xml,Xslt,Xpath,预期结果 <root> <tag> <form> some html form will be here </form> </tag> <tag> some visible data </tag> <div id="page-base"> </div> <div id="page-base"> some visible data </div>

预期结果

<root>
<tag>
  <form>
   some html form will be here
  </form>

</tag>
<tag>
  some visible data
</tag>
<div id="page-base">

</div>
<div id="page-base">
 some visible data
</div>
<root>
        <tag>
          <form>
           some html form will be here
          </form>
          <tag>
            arbitrary nested tags
          </tag>    
        </tag>
        <tag>
          some visible data
        </tag>
</root>

这里会有一些html表单
任意嵌套标记
一些可见的数据
添加一个:


这将简单地复制与更具描述性的模板不匹配的任何节点。由于匹配规则的catchall特性,其他规则将优先,除非您调整其优先级,在这种情况下,您应该将此规则指定为低于任何其他规则

如果您只想复制树的某些部分,那么应该为其添加一个单独的模式,无论何时调用apply该模板,都必须指定该模式

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


如果您使用的是XSLT2.0,还可以使用进行深度复制。但上述方法仍然更灵活,因为它允许您从副本中省略或转换某些节点。

此转换:

<xsl:template match="@*|node()" mode="copy">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="copy"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="tag">
  <div id="page-base">
    <xsl:apply-templates mode="copy"/>
  </div>
</xsl:template>
当此转换应用于新提供的XML文档时:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:copy-of select="node()"/>
  </div>
 </xsl:template>
</xsl:stylesheet>
<root>
    <tag>
        <form>
   some html form will be here
        </form>
    </tag>
    <tag>
  some visible data
    </tag>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:apply-templates/>
  </div>
 </xsl:template>
</xsl:stylesheet>

这里会有一些html表单
任意嵌套标记
一些可见的数据

如果我使用
copy of
它会使用模板匹配该节点中的哪个节点吗?@user1538127,
xsl:copy of
按照它的名称执行--它会将由其
select
属性中指定的表达式选择的所有节点复制到输出中。您可以按照我答案中的链接阅读更多有关此说明的内容。@user1538127,您没有提供新案例所需的结果。请编辑问题并执行操作。@dimitre完成,请立即查看。@user1538127,是的,我在答案的末尾添加了一个更新。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:copy-of select="node()"/>
  </div>
 </xsl:template>
</xsl:stylesheet>
<root>
    <tag>
        <form>
   some html form will be here
        </form>
    </tag>
    <tag>
  some visible data
    </tag>
</root>
<div id="page-base">
   <form>
   some html form will be here
        </form>
</div>
<div id="page-base">
  some visible data
    </div>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:apply-templates/>
  </div>
 </xsl:template>
</xsl:stylesheet>
<div id="page-base">
   <form>
           some html form will be here
          </form>
   <div id="page-base">
            arbitrary nested tags
          </div>
</div>
<div id="page-base">
          some visible data
        </div>