Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl:copy-of excluding parent

Tags:

xslt

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

like image 271
ProPuke Avatar asked Nov 18 '09 12:11

ProPuke


2 Answers

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>
like image 52
Welbog Avatar answered Sep 23 '22 08:09

Welbog


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>
like image 35
Tomalak Avatar answered Sep 23 '22 08:09

Tomalak