xsl:不包括父项的副本

时间:2009-11-18 12:32:59

标签: xslt

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

<tag>
  content
  <a>
    b
  </a>
</tag>

..会得到以下结果:?

content
<a>
  b
</a>

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


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

2 个答案:

答案 0 :(得分:12)

由于您还希望包含content部分,因此您需要node()函数,而不是*运算符:

<xsl:copy-of select="tag/node()"/>

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

content
<a>
  b
</a>

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

<xsl:copy-of select="./node()" />

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

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

答案 1 :(得分:3)

补充了Welbog的答案,我的投票,我建议编写单独的模板,按照以下方式:

<xsl:template match="/">
  <body>
    <xsl:apply-templates select="description" />
  </body>
</xsl:template>

<xsl:template match="description">
  <div class="description">
    <xsl:copy-of select="node()" />
  </div>
</xsl:template>
相关问题