xsl-list
[Top] [All Lists]

Re: [xsl] Dumb Question - XML to XML changing only a few values

2007-11-30 15:29:30
At 2007-11-30 22:13 +0000, Mark Anderson wrote:
Is there a way to say "just copy everything to the destination XML, but any instance of <a></a> will have its contents replaced"?

It is called "the identity template" and looks like this:

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

In that template, the priority of both matches is negative 1.

Matches such as match="a" have a priority of zero, so are higher. When you add more to this, the priority is .5 (more nuanced with XSLT 2.0), so is higher still.

For example

Source XML
...
So I just want to say replace contents of <a></a> with 12345 and just copy everything else

Destination XML
...

This is a very common pattern and I have the template as part of my unedited raw stylesheet that I use to start new stylesheets.

I hope this helps.

. . . . . . . . . Ken

T:\ftemp>type mark.xml
<test>
<a>A</a>
<b>B</b>
<c>C</c>
<d>D</d>
<e>E</e>
<f>F</f>
<g>G</g>
</test>
T:\ftemp>type mark.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="a">
  <xsl:copy>12345</xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>xslt mark.xml mark.xsl con
<?xml version="1.0" encoding="utf-8"?><test>
<a>12345</a>
<b>B</b>
<c>C</c>
<d>D</d>
<e>E</e>
<f>F</f>
<g>G</g>
</test>
T:\ftemp>



--
Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>