xsl:不包括父项的副本

Pro*_*uke 7 xslt

我可以使用什么代码替换<xsl:copy-of select="tag"/>,当应用于以下xml时...

<tag>
  content
  <a>
    b
  </a>
</tag>
Run Code Online (Sandbox Code Playgroud)

..会得到以下结果:?

content
<a>
  b
</a>
Run Code Online (Sandbox Code Playgroud)

我希望回显其中的所有内容,但不包括父标记


基本上我在我的xml文件中有几个部分内容,格式为html,分组为xml标签
我希望有条件地访问它们并回显它们
例如:<xsl:copy-of select="description"/>
生成的额外父标签不会影响浏览器呈现,但它们是无效标签,我希望能够将它们删除
我是否以完全错误的方式解决这个问题?

Wel*_*bog 12

既然你想要包含这个content部分,你需要的是node()函数,而不是*运算符:

<xsl:copy-of select="tag/node()"/>
Run Code Online (Sandbox Code Playgroud)

我在输入示例上对此进行了测试,结果是示例结果:

content
<a>
  b
</a>
Run Code Online (Sandbox Code Playgroud)

如果不对根节点名称进行硬编码,则可以是:

<xsl:copy-of select="./node()" />
Run Code Online (Sandbox Code Playgroud)

这在您已经处理根节点并且想要内部所有元素的精确副本(不包括根节点)的情况下非常有用.例如:

<xsl:variable name="head">
  <xsl:copy-of select="document('head.html')" />
</xsl:variable>
<xsl:apply-templates select="$head" mode="head" />

<!-- ... later ... -->

<xsl:template match="head" mode="head">
  <head>
  <title>Title Tag</title>
  <xsl:copy-of select="./node()" />
  </head>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)