我对 xsl:copy-of 有个小问题,因为我只想复制节点的内容,而不是节点本身:
在 XML 中:
<parent>
<node>Hello, I'm a <b>node</b>!!!</node>
</parent>
在 XSL 中:
<xsl:template match="parent">
<tr>
<td><xsl:copy-of select="node"/></td>
</tr>
</xsl:template>
结果:
<tr>
<td><node>Hello, I'm a <b>node</b>!!!</node></td>
</tr>
预期结果:
<tr>
<td>Hello, I'm a <b>node</b>!!!</td>
</tr>
问题是如果我使用 xsl:value-of , 我松了 <b></b> !!!
最佳答案
你可以使用
<xsl:copy-of select="node/node()" />
它看起来有点奇怪,因为元素名称也是 node但是node()是什么选择器所做的是从适当的节点中选择所有子元素、文本节点、注释节点和处理指令(在本例中,所有子元素在当前上下文元素中称为 node)。
node()不选择属性,所以如果你开始
<parent>
<node attr="foo">Hello, I'm a <b>node</b>!!!</node>
</parent>
然后 <td><xsl:copy-of select="node/node()"/></td>会产生
<td>Hello, I'm a <b>node</b>!!!</td>
如果你说 <td><xsl:copy-of select="node/node() | node/@*"/></td>然后你会得到
<td attr="foo">Hello, I'm a <b>node</b>!!!</td>
关于xml - xsl :copy-of just the content without the node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13362291/