xsl:copy-of excluding parent
What code could I use in replace of <xsl:copy-of select="tag"/>
, that when applied to the following xml..
<tag>
content
<a>
b
</a>
</tag>
..would give the following result: ?
content
<a>
b
</a>
I wish to echo out all the content therein, but excludin开发者_开发百科g the parent tag
Basically I have several sections of content in my xml file, formatted in html, grouped in xml tags
I wish to conditionally access them & echo them out
For example: <xsl:copy-of select="description"/>
The extra parent tags generated do not affect the browser rendering, but they are invalid tags, & I would prefer to be able to remove them
Am I going about this in totally the wrong way?
Since you want to include the content
part as well, you'll need the node()
function, not the *
operator:
<xsl:copy-of select="tag/node()"/>
I've tested this on the input example and the result is the example result:
content
<a>
b
</a>
Without hard-coding the root node name, this can be:
<xsl:copy-of select="./node()" />
This is useful in situations when you are already processing the root node and want an exact copy of all elements inside, excluding the root node. For example:
<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>
Complementing Welbog's answer, which has my vote, I recommend writing separate templates, along the lines of this:
<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>
精彩评论