Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

XSLT Copy node doesn't work

I'd like to copy some nodes exactly as they are using the folowing template:

<xsl:template match="example1 | ext-link | example2">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

So for the following input:

<ext-link ext-link-type="uri" xlink:href="http://www.gnuplot.info/">http://www.gnuplot.info/</ext-link>

I would get exatly te same:

<ext-link ext-link-type="uri" xlink:href="http://www.gnuplot.info/">http://www.gnuplot.info/</ext-link>

However the result is like this:

<ext-link>urihttp://www.gnuplot.info/http://www.gnuplot.info/</ext-link>

I'm using Java, Saxon.

Please help me, what am I doing wrong?

Upvotes: 0

Views: 301

Answers (3)

JLRishe
JLRishe

Reputation: 101662

If you really just want to copy the elements and their content exactly as they are , one more option would be to just use

<xsl:template match="example1 | ext-link | example2">
   <xsl:copy-of select="." />
</xsl:template>

Though hr_117's push-style answer using apply-templates is generally the preferred one as it is more easily extensible.

Upvotes: 1

Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

I have found an alternative solution, I would like to know what do you think. It suits my purposes well.

<xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates />
</xsl:copy>

Upvotes: 1

hr_117
hr_117

Reputation: 9627

Have a look for XSLT identity transform (e.g. http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT) Add a @* to your template match.
Try:

<xsl:template match="@* | example1 | ext-link | example2">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Upvotes: 2

Related Questions