简体   繁体   中英

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?

Have a look for XSLT identity transform (eg 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>

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM